Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00026
00027 #ifndef ICL_CORE_SINGLETON_HPP_INCLUDED
00028 #define ICL_CORE_SINGLETON_HPP_INCLUDED
00029
00030 #include "Singleton.h"
00031
00032 namespace icl_core {
00033
00034 template
00035 <class T, template <class> class TCreationPolicy,
00036 template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00037 T& Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::instance()
00038 {
00039 T *temp = m_instance;
00040 TThreadingModel<T>::memoryBarrier();
00041 if (temp == NULL)
00042 {
00043 typename TThreadingModel<T>::Guard guard(m_lock);
00044 temp = m_instance;
00045 if (temp == NULL)
00046 {
00047 if (m_destroyed)
00048 {
00049 TLifetimePolicy<T>::onDeadReference();
00050 m_destroyed = false;
00051 }
00052 temp = TCreationPolicy<T>::create();
00053 TThreadingModel<T>::memoryBarrier();
00054 m_instance = temp;
00055 TLifetimePolicy<T>::scheduleDestruction(&destroySingleton);
00056 }
00057 }
00058 return *m_instance;
00059 }
00060
00061 template
00062 <class T, template <class> class TCreationPolicy,
00063 template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00064 void Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::destroySingleton()
00065 {
00066 TCreationPolicy<T>::destroy(m_instance);
00067 m_instance = NULL;
00068 m_destroyed = true;
00069 }
00070
00071 template
00072 <class T, template <class> class TCreationPolicy,
00073 template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00074 T *Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::m_instance = NULL;
00075
00076 template
00077 <class T, template <class> class TCreationPolicy,
00078 template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00079 bool Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::m_destroyed = false;
00080
00081 template
00082 <class T, template <class> class TCreationPolicy,
00083 template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00084 typename TThreadingModel<T>::Lock Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::m_lock;
00085
00086 }
00087
00088 #endif