TuttleOFX
1
|
00001 #ifndef StaticSingleton_HPP 00002 #define StaticSingleton_HPP 00003 00004 /** 00005 * @brief StaticSingleton<ClassStaticSingleton> Can transform a class into static Singleton (by inheriting this class) 00006 * 00007 * @par Purpose 00008 * Forcing (limit) the presence of a single instance of a class 00009 * 00010 * @par Usage 00011 * The class T, to use as static Singleton, must derive from this class (: public StaticSingleton <T>). 00012 * The macro MAKE_StaticSingleton(T) create necessary elements (including constructors). 00013 */ 00014 template <class T> 00015 class StaticSingleton 00016 { 00017 private: 00018 static T inst; 00019 00020 private: 00021 StaticSingleton( const StaticSingleton& ) {} 00022 StaticSingleton& operator=( const StaticSingleton& ) {} 00023 00024 protected: 00025 StaticSingleton() {} 00026 virtual ~StaticSingleton() = 0; 00027 00028 public: 00029 /** 00030 * @brief return the unique instance of StaticSingleton<T> class 00031 * @return T the unique instance of StaticSingleton<T> class 00032 */ 00033 static T& instance() 00034 { 00035 return inst; 00036 } 00037 00038 }; 00039 00040 template <class T> 00041 T StaticSingleton<T>::inst; 00042 template <class T> 00043 StaticSingleton<T>::~StaticSingleton() {} 00044 00045 ///macro to implement StaticSingleton. Use it in derived class declaration 00046 #define MAKE_StaticSingleton( Class ) \ 00047 public: \ 00048 friend class StaticSingleton < Class >; \ 00049 private: \ 00050 Class() {} \ 00051 ~Class() {} 00052 00053 #endif