前言
单例模式用模板实现的话比较优雅方便,基本不需要写额外的代码。但也有个问题,就是封装成库之后,比如windows下是封装成DLL,那么DLL内的单例和外部单例将会是2个不同的实例对象,可以做实验发现2个指针是不一样的地址。这篇文章就是总结一下用模板实现单例,并且可以跨库是怎么做到的。
一、单例模板范式
代码实例也都是从网上抄来的,没什么好解释的,就一点要说明下,模板的实现全部放头文件(包括单例中的静态成员变量),不然会报错:
template <typename T>
class Singleton
{
public:
static std::shared_ptr<T> instance()
{
static std::once_flag flag;
std::call_once(flag, []() {
m_instance.reset(new T);
});
return m_instance;
}
private:
Singleton() = default;
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
static std::shared_ptr<T> m_instance;
};
template<typename T>
std::shared_ptr<T> Singleton<T>::m_instance;
二、使用宏定义声明单例类
1.宏定义
这个宏的作用是禁止外部使用构造函数声明对象,并且添加instance()接口。大家可能会觉得有点像脱裤子放屁,但这都是为了跨库使用,并且尽可能的优美,看到最后就明白了。如果不考虑跨库使用,上面的单例模板已经做到了,
#define DECL_SINGLETON(T)\
private:\
explicit T() {}\
friend class Singleton<T>;\
public:\
static std::shared_ptr<T> instance()\
{\
return Singleton<T>::instance();\
}
2.声明单例类
代码如下(示例):
class ClassificationInterface
{
DECL_SINGLETON(ClassificationInterface)
public:
void func(); //do some thing
};
这样看起来是不是很简洁,使用的时候这个类就是单例了直接可以这样:
ClassificationInterface::instance()->func();
增加导出符号
最后,我们将这个类导出,就可以跨库使用了,原理是:我们宏定义的instance()函数会被导出,这个函数本身不是模板,而是将模板实例化了,是有唯一性的。关于导出符号不懂的搜索下吧。
#ifndef TEST_WITHOUT_WRAP_API
#if WIN32
#ifdef EXPORT_WRAP_API
#define WRAP_API __declspec(dllexport)
#else
#define WRAP_API __declspec(dllimport)
#endif
#else
#define WRAP_API __attribute__ ((visibility ("default")))
#endif
#else
#define WRAP_API
#endif
class WRAP_API ClassificationInterface
{
DECL_SINGLETON(ClassificationInterface)
public:
void func(); //do some thing
};
总结
总结了单例模式的模板跨库范式,最后在类中增加一个宏定义即可完成单例类声明。个人感觉实现较为优雅。有兴趣的可以做个实验,看看单例指针是不是唯一的。