singleton_template.hpp
Go to the documentation of this file.
1 /*
2  * SocialLedge.com - Copyright (C) 2013
3  *
4  * This file is part of free software framework for embedded processors.
5  * You can use it and/or distribute it as long as this copyright header
6  * remains unmodified. The code is free for personal use and requires
7  * permission to use in a commercial product.
8  *
9  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
10  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
12  * I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
13  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
14  *
15  * You can reach the author of this software at :
16  * p r e e t . w i k i @ g m a i l . c o m
17  */
18 
27 #ifndef SINGLETONTEMPLATE_HPP_
28 #define SINGLETONTEMPLATE_HPP_
29 
30 
31 
41 #define STATIC_INSTANCE 1
42 #define STATIC_POINTER_INSTANCE 2
43 #define STATIC_PRIVATE_POINTER_INSTANCE 3
44 #define SINGLETON_INIT_METHOD STATIC_PRIVATE_POINTER_INSTANCE
45 
46 
47 
54 template <typename classType>
56 {
57 public:
59  static classType& getInstance();
60 
61 protected:
62  #if (SINGLETON_INIT_METHOD == STATIC_PRIVATE_POINTER_INSTANCE)
64  #endif
65 
71 
72 private:
73  /* No Private Members */
74 };
75 
76 
77 
78 
79 
80 
81 
82 #if (SINGLETON_INIT_METHOD == STATIC_POINTER_INSTANCE)
83  template <typename classType>
85  {
86  static classType* mpSingletonInstance = new classType();
87  return *mpSingletonInstance;
88  }
89 #elif (SINGLETON_INIT_METHOD == STATIC_INSTANCE)
90  template <typename classType>
92  {
93  static classType singletonInstance;
94  return singletonInstance;
95  }
96 #elif (SINGLETON_INIT_METHOD == STATIC_PRIVATE_POINTER_INSTANCE)
97  template <typename classType>
99 
100  template <typename classType>
102  {
103  if(0 == mpSingletonInstance)
104  {
105  mpSingletonInstance = new classType();
106  }
107  return *(classType*)mpSingletonInstance;
108  }
109 #else
110  #error SINGLETON_INIT_METHOD contains invalid value
111 #endif
112 
113 
114 #endif /* SINGLETONTEMPLATE_HPP_ */
static classType & getInstance()
Public member to get instance of this SINGLETON class.
Definition: singleton_template.hpp:101
SingletonTemplate()
Definition: singleton_template.hpp:70
static SingletonTemplate * mpSingletonInstance
Definition: singleton_template.hpp:63
Definition: singleton_template.hpp:55