singleton.h (585B)
1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us> 2 // See LICENSE for license details. 3 4 #ifndef __SINGLETON_H 5 #define __SINGLETON_H 6 7 /** 8 * Basic singleton template used by various controller classes of WMI. 9 */ 10 template <class T> class Singleton 11 { 12 13 public: 14 15 static T *instance() { 16 if (instance_ == 0) { 17 // use default constructor 18 instance_ = new T; 19 } 20 return instance_; 21 } 22 23 private: 24 25 // the singleton instance 26 static T *instance_; 27 }; 28 29 template <class T> T* Singleton<T>::instance_ = 0; 30 31 #endif // __SINGLETON_H