1 // -*- c++ -*- 2 //------------------------------------------------------------------------------ 3 // Singleton.h 4 //------------------------------------------------------------------------------ 5 // Copyright (C) 1997-2002,2005 Vladislav Grinchenko 6 // 7 // This library is free software; you can redistribute it and/or 8 // modify it under the terms of the GNU Library General Public 9 // License as published by the Free Software Foundation; either 10 // version 2 of the License, or (at your option) any later version. 11 //------------------------------------------------------------------------------ 12 // Created: 02/22/99 13 //------------------------------------------------------------------------------ 14 #ifndef _Singleton_h 15 #define _Singleton_h 16 17 #include "Destroyer.h" 18 19 namespace ASSA { 20 21 /** @file Singleton.h 22 23 Singleton template class allows to turn any new or existing class T 24 into Singleton Pattern. It is accomplished by deriving class T from 25 class Singleton. 26 27 It is assumed that Singleton objects are long-lived. Often they exist 28 for the life of the program. You delete them not so much to reclaim space 29 but to shut down in the orderly manner (such as return whatever 30 resources derived class holds in its ownership back to the system). 31 C++ deletes static objects automatically. Although, it doesn't guarantee 32 the calling order. In other words, destructors of Singleton class are not 33 order-dependent. 34 35 To force destruction order, Singleton class transfers ownership of 36 object T to Destroyer class. When the program exits, the Destroyer will 37 be destroyed, and the object T along with it. Singleton destructor is 38 now implicit. 39 */ 40 41 template <class T> 42 class Singleton 43 { 44 public: 45 /// Return an instance of templated class T 46 get_instance()47 static T* get_instance () { 48 if (m_instance == 0) { 49 m_instance = new T; 50 m_destroyer.setGuard (m_instance); 51 } 52 return m_instance; 53 } 54 55 protected: 56 /// Protected Constructor Singleton()57 Singleton() {} 58 59 friend class Destroyer<T>; 60 61 /// Virtual Destructor ~Singleton()62 virtual ~Singleton () {} 63 64 private: 65 /// Pointer to the object T instance 66 static T* m_instance; 67 68 /// Destroyer that owns object T 69 static Destroyer<T> m_destroyer; 70 }; 71 72 } // end namespace ASSA 73 74 75 /** @def ASSA_DECL_SINGLETON(K) 76 77 ASSA_DECL_SINGLETON macro inserts static member declarations 78 mandated by the Singleton class. 79 80 'assa-genesis' utility generates appropriate file templated by default. 81 */ 82 #define ASSA_DECL_SINGLETON(K) \ 83 template <> K* ASSA::Singleton<K>::m_instance = NULL; \ 84 template <class T> ASSA::Destroyer<T> ASSA::Singleton<T>::m_destroyer; \ 85 template ASSA::Destroyer<K> ASSA::Singleton<K>::m_destroyer; 86 87 #endif 88