Adding new commiter to ODU-High repo
[o-du/l2.git] / src / o1 / Singleton.hpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file constains a singleton class template */
20
21 #ifndef __SINGLETON_HPP__
22 #define __SINGLETON_HPP__
23
24 template<class T>
25 class Singleton
26 {
27   public:
28   static T& instance();
29   static T  *instancePtr();
30
31   protected:
32   Singleton() { } 
33   ~Singleton() { }
34
35   private:
36   static T* mPtr;
37 };
38
39 /* Define and initialize the static instance pointer */
40 template<class T>
41 T* Singleton<T>::mPtr = 0;
42
43
44 /********************************************************************** 
45    Description : Check if instance already exists.
46                  Create a new instance if none exists  
47    Params[In]  : None
48    Return      : Reference to the instance 
49 ***********************************************************************/
50 template<class T>
51 T& Singleton<T>::instance()
52 {
53     
54   if( mPtr == 0)
55     mPtr = new T();
56
57   return *mPtr;
58 }
59
60
61 /********************************************************************** 
62    Description : Check if instance already exists.
63                  Create a new instance if none exists  
64    Params[In]  : None
65    Return      : Pointer to the instance 
66 ***********************************************************************/
67 template<class T>
68 T *Singleton<T>::instancePtr()
69 {
70     return &(instance());
71 }
72
73 #endif
74
75 /**********************************************************************
76          End of file
77 **********************************************************************/
78