ISIS Core Library 0.7.2 (api 3.0.0)
|
00001 #ifndef SINGLETONS_HPP_INCLUDED 00002 #define SINGLETONS_HPP_INCLUDED 00003 00004 #include <map> 00005 #include <boost/noncopyable.hpp> 00006 #include <string> 00007 #include <iostream> 00008 #include <typeinfo> 00009 00010 namespace isis 00011 { 00012 namespace util 00013 { 00014 00031 class Singletons 00032 { 00033 template <typename C> class Singleton 00034 { 00035 static void destruct() { 00036 if( _instance )delete _instance; 00037 00038 _instance = 0; 00039 } 00040 static C *_instance; 00041 Singleton () { } 00042 public: 00043 friend class Singletons; 00044 }; 00045 00046 typedef void( *destructer )(); 00047 typedef std::multimap<int, destructer> prioMap; 00048 00049 prioMap map; 00050 Singletons(); 00051 virtual ~Singletons(); 00052 static Singletons &getMaster(); 00053 public: 00059 template<typename T, int PRIO> static T &get() { 00060 if ( !Singleton<T>::_instance ) { 00061 Singleton<T>::_instance = new T(); 00062 prioMap &map = getMaster().map; 00063 map.insert( map.find( PRIO ), std::make_pair( PRIO, Singleton<T>::destruct ) ); 00064 } 00065 00066 return *Singleton<T>::_instance; 00067 } 00068 }; 00069 template <typename C> C *Singletons::Singleton<C>::_instance = 0; 00070 00071 } 00072 } 00073 #endif //SINGLETONS_HPP_INCLUDED