首页 / C++ / C++可继承的单例基类模板
C++可继承的单例基类模板
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++可继承的单例基类模板,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2647字,纯文字阅读大概需要4分钟。
内容图文
目录
一、介绍
最近在写一个项目,其中用到好几个单例,类本身的设计不是很复杂,但是如果每个都写一遍单例又觉得有点冗余;所以查资料写了一个单例基类模板,只要一个类继承这个基类并且以自身作为模板参数就可以实现一个单例;关于单例本身的介绍在这里不重点介绍。
特点:
- RAII,使用 std::shared_ptr来管理资源
- 线程安全,加了锁
- 以上特性基于C++11
二、代码
// bridf: a singleton base class offering an easy way to create singleton
#include <memory> // shard_ptr
#include <mutex>
#include <iostream>
template<typename T>
class Singleton{
typedef std::shared_ptr<T> Ptr;
protected:
Singleton() noexcept=default;
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
virtual ~Singleton() = default;
static Ptr instance_ptr;
static std::mutex singleton_mutex;
public:
static Ptr get_instance() noexcept(std::is_nothrow_constructible<T>::value)
{
if(instance_ptr==nullptr){
std::lock_guard<std::mutex> lk(singleton_mutex);
if(instance_ptr==nullptr){
instance_ptr = std::shared_ptr<T>(new T());
}
}
return instance_ptr;
}
};
template <typename T>
std::shared_ptr<T> Singleton<T>::instance_ptr=nullptr;
template <typename T>
std::mutex Singleton<T>::singleton_mutex;
/********************************************/
// Example:
// 1.friend class declaration is requiered!
// 2.constructor should be private
class DerivedSingle:public Singleton<DerivedSingle>{
// !!!! attention!!!
// needs to be friend in order to
// access the private constructor/destructor
friend class Singleton<DerivedSingle>;
int value;
public:
void info(){
std::cout<<"value: \n";
std::cout<<value<<std::endl;
}
void set_value(int i){
value = i;
}
int get_value(){
return value;
}
private:
DerivedSingle():value(0){}
DerivedSingle(const DerivedSingle&)=delete;
DerivedSingle& operator =(const DerivedSingle&)= delete;
};
int main(int argc, char* argv[]){
std::shared_ptr<DerivedSingle> instance = DerivedSingle::get_instance();
instance->info();
instance->set_value(1);
std::shared_ptr<DerivedSingle> instance2 = DerivedSingle::get_instance();
instance2->info();
return 0;
}
以上代码放在了 我的github中: https://github.com/sunchaothu/AdvancedCpp/tree/master/singleton
三、关键处
- 子类需要把自己作为模板参数,如
class DerivedSingle:public Singleton<DerivedSingle>;
这里用到的是CRTP(Curiously recurring template pattern) 递归循环模板模式 - 在子类中需要把基类
Singleton
声明为友元;这样才能访问私有构造函数
四、使用限制
继承了这个类的代码不可以作为基类再被继承。
五、参考资料
以上代码参考了以下链接;如果有纰漏之处,还请指正,以免误人子弟!
- https://stackoverflow.com/questions/34519073/inherit-singleton?r=SearchResults
- https://www.cnblogs.com/lidabo/p/3949226.html
内容总结
以上是互联网集市为您收集整理的C++可继承的单例基类模板全部内容,希望文章能够帮你解决C++可继承的单例基类模板所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。