#ifndef SINGLETON_H
#define SINGLETON_H #include <cassert>
#include <memory>
#include <mutex> #define DECLARE_SINGLETON_CLASS(T) friend Singleton<T> template <typename T>
class Singleton
{
public:
using PT = std::shared_ptr<T>; Singleton() = delete;
~Singleton() = delete; public:
template <typename... Args>
static PT getInstance(Args&&... args)
{
// std::call_once(m_flag, create, std::forward<Args>(args)...);
// error: no matching function for call call_once... <unresolved overloaded function type>
// couldn't deduce template parameter '_Callable'
// why ?
std::call_once(m_flag, [&]() {
create(std::forward<Args>(args)...);
});
assert(m_instance);
return m_instance;
} private:
template <typename... Args>
static void create(Args&&... args)
{
m_instance = std::shared_ptr<T>{new T(std::forward<Args>(args)...), destroy};
} static void destroy(T* t)
{
delete t;
} private:
static std::once_flag m_flag;
static PT m_instance;
}; template <typename T>
std::once_flag Singleton<T>::m_flag; template <typename T>
typename Singleton<T>::PT Singleton<T>::m_instance{}; #endif // SINGLETON_H
#include "Singleton.h"
#include <iostream>
#include <string> using namespace std; #define print() cout << "[" << __func__ << ":" << __LINE__ << "]"
#define print_position() print() << endl
#define print_class() print() << " " << typeid(*this).name() << " " #if 1 class Bundle
{
public:
Bundle()
{
print_class() << "construct" << endl;
} ~Bundle()
{
print_class() << "destruct" << endl;
} Bundle(const Bundle& )
{
print_class() << "copy construct" << endl;
} Bundle(Bundle&&)
{
print_class() << "move construct" << endl;
} Bundle& operator=(Bundle&)
{
print_class() << "copy operator assign" << endl;
return *this;
} Bundle& operator=(Bundle&&)
{
print_class() << "move operator assign" << endl;
return *this;
}
}; class SingleInstanceKlass
{
DECLARE_SINGLETON_CLASS(SingleInstanceKlass); private:
SingleInstanceKlass()
{
print_class() << "default construct" << endl;
}
SingleInstanceKlass(int)
{
print_class() << "int construct" << endl;
}
SingleInstanceKlass(const string&)
{
print_class() << "string construct" << endl;
}
SingleInstanceKlass(const Bundle&)
{
print_class() << "Bundle construct" << endl;
}
~SingleInstanceKlass()
{
print_class() << "destruct" << endl;
} public:
void run()
{
print_position();
}
}; template <typename... Args>
void unused(Args...)
{
} void onExit()
{
print_position();
} int main(int argc, char *argv[])
{
unused(argc, argv); atexit(onExit); print_position();
{
Singleton<SingleInstanceKlass>::getInstance(Bundle{});
Singleton<SingleInstanceKlass>::getInstance(.);
Singleton<SingleInstanceKlass>::getInstance();
Singleton<SingleInstanceKlass>::getInstance("")->run();
}
print_position(); return ;
} #endif

设计模式之单例模式实现(C++)的更多相关文章

  1. 设计模式之单例模式(Singleton)

    设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...

  2. GJM : C#设计模式(1)——单例模式

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  3. java设计模式之单例模式(几种写法及比较)

    概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...

  4. 每天一个设计模式-4 单例模式(Singleton)

    每天一个设计模式-4 单例模式(Singleton) 1.实际生活的例子 有一天,你的自行车的某个螺丝钉松了,修车铺离你家比较远,而附近的五金店有卖扳手:因此,你决定去五金店买一个扳手,自己把螺丝钉固 ...

  5. 设计模式之单例模式的简单demo

    /* * 设计模式之单例模式的简单demo */ class Single { /* * 创建一个本类对象. * 和get/set方法思想一样,类不能直接调用对象 * 所以用private限制权限 * ...

  6. 设计模式之单例模式——Singleton

                        设计模式之单例模式--Singleton 设计意图: 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有 ...

  7. 10月27日PHP加载类、设计模式(单例模式和工厂模式)、面向对象的六大原则

    加载类可以使用include.require.require_once三种中的任意一种,每个关键字都有两种方法,但是这种方法的缺点是需要加载多少个php文件,就要写多少个加载类的方法.一般也就需要加载 ...

  8. java 23 - 2 设计模式之单例模式

    单例模式:保证类在内存中只有一个对象. 如何保证类在内存中只有一个对象呢?  A:把构造方法私有  B:在成员位置自己创建一个对象  C:通过一个公共的方法提供访问 单例模式之饿汉式: (一进来就造对 ...

  9. [转]JAVA设计模式之单例模式

    原文地址:http://blog.csdn.net/jason0539/article/details/23297037 概念: java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主 ...

  10. python_way,day8 面向对象【多态、成员--字段 方法 属性、成员修饰符、特殊成员、异常处理、设计模式之单例模式、模块:isinstance、issubclass】

    python_way day8 一.面向对象三大特性: 多态 二.面向对象中的成员 字段.方法属性 三.成员修饰符 四.特殊成员 __init__.__doc__.__call__.__setitem ...

随机推荐

  1. Thunder团队--Beta发布用户使用报告

    Thunder爱阅app Beta 发布用户使用报告 用户数量:14人 以下为用户评论:(注:为了保护用户的姓名权,以下用户名以昵称形式给出.) 序号 昵称 个人信息 获得软件途径 使用次数 用户评论 ...

  2. C# Linq找不到行或已更改

    前段时间工作中的一个新需求,有机会用到了Linq to SQL.使用后的第一感觉,就是方便很多,也为整个项目节约了一大把的开发时间,甚至代码量也少了很多.不过在程序的实际运行中,始终会遇到一些莫名其妙 ...

  3. Leetcode题库——17.电话号码的字母组合

    @author: ZZQ @software: PyCharm @file: letterCombinations.py @time: 2018/10/18 18:33 要求:给定一个仅包含数字 2- ...

  4. HDU 2159 FATE 完全背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2159 FATE Time Limit: 2000/1000 MS (Java/Others)Memo ...

  5. 信安实践——CSRF攻击与防御

    1.实验原理 CSRF(Cross-Site Request Forgery,跨站点伪造请求)是一种网络攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在未授权的 ...

  6. 16_常用API_第16天(正则表达式、Date、DateFormat、Calendar)_讲义

    今日内容介绍 1.正则表达式的定义及使用 2.Date类的用法 3.Calendar类的用法 ==========================================第一阶段======= ...

  7. 用css 实现凹陷的线条

    box-shadow: 0 1px 0 rgba(255,255,255,0.2) inset,0 -1px 0 rgba(0,0,0,.2) inset; 因为颜色为透明颜色,所以颜色是什么样的,不 ...

  8. D3.js 入门学习(二) V4的改动

    //d3.scan /* 新的d3.scan方法对数组进行线性扫描,并根据指定的比较函数返回至少一个元素的索引. 这个方法有点类似于d3.min和d3.max. 而d3.scan可以得到极值的索引而不 ...

  9. XHTML和HTML、CSS 验证器

    XHTML 验证器和 CSS 验证器.需要这些工具去验证你的页面是否符合 XHTML 和 CSS 标准,并且可以使用它查出奇正错误的地方. XHTML 验证器 地址:http://validator. ...

  10. [51CTO]给您介绍Windows10各大版本之间区别

    给您介绍Windows10各大版本之间区别 随着win10的不断普及和推广,越来越多的朋友想安装win10系统了,但是很多朋友不知道win10哪个版本好用,为了让大家能够更好的选择win10系统版本, ...