

/*!
  @author d金魚
  @since 2003/8/21
	@note
	licence:BSD Licence
*/


#ifndef _dktl_utility__h_
#define _dktl_utility__h_

#include <dkutil/config/dkutilUserConfig.h>

namespace dkutil{

template <class T,class UTIL=DKConfigUtility>
class singleton {

//やねうらお氏のYTL/singleton.hを参考にしました。
public:
	singleton(){
		//ODS("OK singleton\n");
	}
	virtual ~singleton() { Release(); }
	///	ポインタのふりをするための仕掛け　(なるほど！これは便利です。
	T& operator*() { return *get(); }
	T* operator->() { return get();	}
	T* get() { CheckInstance(); return m_lpObj; }
	bool	isNull() const { return m_lpObj == NULL; }
	void Release(){
		if (m_lpObj!=NULL) {
			DKUTIL_LOCKTHREAD(m_util);
			if(m_lpObj!=NULL){		
				delete m_lpObj;
				m_lpObj = NULL;
			}
			DKUTIL_UNLOCKTHREAD(m_util);
		}
	}
	void CheckInstance(){
		if (m_lpObj==NULL) {
			DKUTIL_LOCKTHREAD(m_util);
			if(m_lpObj==NULL){
				m_lpObj = new T;
			}
			DKUTIL_UNLOCKTHREAD(m_util);
		}
	}
BOOST_PROTECTED:
	static T*	m_lpObj;
	UTIL m_util;
};

//	static なオブジェクト
template <class T,class UTIL> T* singleton<T,UTIL>::m_lpObj = 0;


}//end of dkutil namepsace

#endif //end of include once