单例模式C++11泛型实现

// file: singleton.h

#ifndef _LOKI_SINGLETON_H
#define _LOKI_SINGLETON_H 1

#include
#include
#include
#include

#ifdef _MSC_VER
 #define LOKI_CDECL __cdecl // C调用约定
#else
 #define LOKI_CDECL
#endif

namespace loki {

/////////////////////////////////////////////单件模版/////////////////////////////////////////////
template
class singleton_holder {
public:
 typedef T ObjType;
 static T& instance();

private:
 singleton_holder();

 static void makeInstance();
 static void LOKI_CDECL destroySingleton();

 typedef T* InstancePtr;
 static InstancePtr  _spInstance;
 static std::once_flag _sOnceFlag;
};

//
template
typename singleton_holder::InstancePtr singleton_holder::_spInstance = nullptr;

template
std::once_flag singleton_holder::_sOnceFlag;

template
inline T& singleton_holder::instance() {
 std::call_once(_sOnceFlag, &makeInstance);
 return *_spInstance;
}

template
void singleton_holder::makeInstance() {
 _spInstance = new T();
 std::atexit(&destroySingleton);
}

template
void LOKI_CDECL singleton_holder::destroySingleton() {
 delete _spInstance;
 _spInstance = nullptr;
}

} //namespace loki

#define NULL_EXPORT

// no namwspace
#define LOKI_SINGLETON_DEF_H(SINGLETON_EXPORT) \
template        \
class SINGLETON_EXPORT singleton {    \
public:           \
 static T& instance();      \
};

#define LOKI_SINGLETON_DEF_CPP(SINGLETON_HOLDER)         \
template<>                   \
SINGLETON_HOLDER::ObjType& singleton::instance() {  \
 return SINGLETON_HOLDER::instance();           \
}

// use namespace
#define LOKI_NAMESPACE_SINGLETON_DEF_H(NS, SINGLETON_EXPORT) \
namespace NS {             \
template            \
class SINGLETON_EXPORT singleton {        \
public:               \
 static T& instance();          \
};                \
}


#define LOKI_NAMESPACE_SINGLETON_DEF_CPP(NS, SINGLETON_HOLDER)      \
namespace NS {                  \
template<>                   \
SINGLETON_HOLDER::ObjType& singleton::instance() {  \
 return SINGLETON_HOLDER::instance();           \
}                     \
}

#endif //_LOKI_SINGLETON_H

你可能感兴趣的:(C++编程)