본문 바로가기

Native/C++

singleton

[code]
/**
* @file CDSingleton.h
* @author cheol-dong choi <aucd29@gmail.com>
* @version 1.0
* @date April 16, 2009 16:48:21
* @section LICENSE
*
* Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @section DESCRIPTION
* <b>sample code</b>
* @code
*
*     class COtherClass : public CDSingletonT<COtherClass>
*     {
*     public:
*             void Print() { printf("Hello world\n"); }
*     }
*
* @endcode
*
* @section CHANGE_LOG
* - doxygen 형태로 주석 변경 2010-08-12 (18:26:19)
*/


#ifndef __CD_SINGLETON_H__
#define __CD_SINGLETON_H__

#include <memory>         // auto_ptr
#include <stdio.h>

/**
* @class CDSingleton
* @brief 싱글톤 템플릿
*/
template<class T>
class CDSingletonT
{
protected:
    CDSingletonT() {};
    ~CDSingletonT() {};

public:
    static std::auto_ptr<T> _xInstance;

public:
    /**
     * 원하는 클래스를 스테틱으로 인스턴스 한다.
     *
     * @return 원하는 클래스 pointer
     */
    static T* getInstance()
    {
        if(!_xInstance.get())
        {
            _xInstance.reset(new T);
        }

        return _xInstance.get();
    }
};
template<class T>
std::auto_ptr<T> CDSingletonT<T>::_xInstance;

#endif
[/code]

'Native > C++' 카테고리의 다른 글

ccw  (0) 2013.10.02
expat static complie  (0) 2013.10.02
extern  (0) 2013.10.02
STLPort compile  (0) 2013.10.02
STLport  (0) 2013.10.02