1 #ifndef ___HGL___singleton___
2 #define ___HGL___singleton___
6 そのプログラムで、一つしかオブジェクトが存在しないことを
18 virtual ~singleton(){release();}
22 オブジェクトが確保されている場合、それを削除する
35 最初にアクセスしたときに、そのオブジェクトをnewしつつ、
41 const_cast<T*>(static_cast<const singleton<T>*>(this)->get());
44 const T* get() const {
45 singleton<T>* p = const_cast<singleton<T>*>(this);
46 if (p->m_pObj == NULL) {
49 return static_cast<T*>(m_pObj);
52 //ポインターの振りをするための演算子オーバーロード
53 T* operator->() {return get();}
54 T& operator*() {return *get();}
55 const T* operator->() const {return get();}
56 const T& operator*() const {return *get();}
58 operator const T*() const {return get();}
62 static T* m_pObj; //静的に保持するオブジェクト。
63 //初期は、必ずNULLであることが保証されている。
67 template<class T> T* singleton<T>::m_pObj = 0;