juce中的Singleton
说明上其实很明白,支持多线程,防止重复创建,同时支持如果删除以后就不在创建,利用局部静态变量进行标记。挺通用,看来下次写个c11版本的
//==============================================================================
/**
Macro to declare member variables and methods for a singleton class. To use this, add the line juce_DeclareSingleton (MyClass, doNotRecreateAfterDeletion)
to the class's definition. Then put a macro juce_ImplementSingleton (MyClass) along with the class's
implementation code. It's also a very good idea to also add the call clearSingletonInstance() in your class's
destructor, in case it is deleted by other means than deleteInstance() Clients can then call the static method MyClass::getInstance() to get a pointer
to the singleton, or MyClass::getInstanceWithoutCreating() which will return nullptr if
no instance currently exists. e.g. @code class MySingleton
{
public:
MySingleton()
{
} ~MySingleton()
{
// this ensures that no dangling pointers are left when the
// singleton is deleted.
clearSingletonInstance();
} juce_DeclareSingleton (MySingleton, false)
}; juce_ImplementSingleton (MySingleton) // example of usage:
MySingleton* m = MySingleton::getInstance(); // creates the singleton if there isn't already one. ... MySingleton::deleteInstance(); // safely deletes the singleton (if it's been created). @endcode If doNotRecreateAfterDeletion = true, it won't allow the object to be created more
than once during the process's lifetime - i.e. after you've created and deleted the
object, getInstance() will refuse to create another one. This can be useful to stop
objects being accidentally re-created during your app's shutdown code. If you know that your object will only be created and deleted by a single thread, you
can use the slightly more efficient juce_DeclareSingleton_SingleThreaded() macro instead
of this one. @see juce_ImplementSingleton, juce_DeclareSingleton_SingleThreaded
*/
#define juce_DeclareSingleton(classname, doNotRecreateAfterDeletion) \
\
static classname* _singletonInstance; \
static juce::CriticalSection _singletonLock; \
\
static classname* JUCE_CALLTYPE getInstance() \
{ \
if (_singletonInstance == nullptr) \
{\
const juce::ScopedLock sl (_singletonLock); \
\
if (_singletonInstance == nullptr) \
{ \
static bool alreadyInside = false; \
static bool createdOnceAlready = false; \
\
const bool problem = alreadyInside || ((doNotRecreateAfterDeletion) && createdOnceAlready); \
jassert (! problem); \
if (! problem) \
{ \
createdOnceAlready = true; \
alreadyInside = true; \
classname* newObject = new classname(); /* (use a stack variable to avoid setting the newObject value before the class has finished its constructor) */ \
alreadyInside = false; \
\
_singletonInstance = newObject; \
} \
} \
} \
\
return _singletonInstance; \
} \
\
static inline classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept\
{ \
return _singletonInstance; \
} \
\
static void JUCE_CALLTYPE deleteInstance() \
{ \
const juce::ScopedLock sl (_singletonLock); \
if (_singletonInstance != nullptr) \
{ \
classname* const old = _singletonInstance; \
_singletonInstance = nullptr; \
delete old; \
} \
} \
\
void clearSingletonInstance() noexcept\
{ \
if (_singletonInstance == this) \
_singletonInstance = nullptr; \
} //==============================================================================
/** This is a counterpart to the juce_DeclareSingleton macro. After adding the juce_DeclareSingleton to the class definition, this macro has
to be used in the cpp file.
*/
#define juce_ImplementSingleton(classname) \
\
classname* classname::_singletonInstance = nullptr; \
juce::CriticalSection classname::_singletonLock;
juce中的Singleton的更多相关文章
- Qt 中使用Singleton模式需小心
在qt中,使用Singleton模式时一定要小心.因为Singleton模式中使用的是静态对象,静态对象是直到程序结束才被释放的,然而,一旦把该静态对象纳入了Qt的父子对象体系,就会导致不明确的行为. ...
- [原] blade中C++ singleton的实现
最近看了龚大大KalyGE中的singleton, 觉得非常不错(C++中线程安全并且高效的singleton). 可惜blade的代码都是C++03的, 没有使用C++11的任何特性. 笔者对于si ...
- juce中的BailOutChecker
界面库中值得注意的一点就是对象响应事件的时候自身被删除了,那么后续的访问自然就会出问题,所以需要在响应事件之后先添加引用,相关处理之后再查看自身是否已经被删除,如果已经被删除那么就直接退出.juce中 ...
- juce 中的WeakReference分析
juce中的WeakReference设计得比较巧妙,巧妙就是使用delete之后就可以通知道WeakReference,原理其实也很间单,其实就是在对象里添加了一个子对象masterReferenc ...
- juce中的timer
juce中timer总体说还是比较好用的,使用时只需继承timer类, 重写callback然后调用start就可以了,juce的timer比较特别,自己通过线程实现,starttimer的时候会创建 ...
- C#中实例Singleton
[C#中实例Singleton] 1.经典方案: using System; public class Singleton { private static Singleton instance; p ...
- juce中的引用计数
这个类提供了最基本的引用计数管理,界面库中,经常都需要消息发送,而带来的后果就是不知道消息中包含的对象是否还存在,如果不能很好管理的话就容易出现访问销毁了的对象这样的情况,所以,juce的界面无素也基 ...
- 面试中的Singleton
引子 “请写一个Singleton.”面试官微笑着和我说. “这可真简单.”我心里想着,并在白板上写下了下面的Singleton实现: 1 class Singleton 2 { 3 public ...
- C++面试中的singleton类
引子 “请写一个Singleton.”面试官微笑着和我说. “这可真简单.”我心里想着,并在白板上写下了下面的Singleton实现: 1 class Singleton 2 { 3 public: ...
随机推荐
- WPS页面设置
以前使用WPS的时候遇到一些问题: 比如我输入一个英文的时候它总是自动的给我首字母大写,但是某些情况下我不想这样: 从VS中复制代码的时候不希望他吧那些颜色复制下来: 还有我输入1回车后它自动给我输入 ...
- asp.net 常用的3中身份验证
1. windows验证: IIS根据应用程序的设置来进行身份验证,要使用这中验证方式,必须禁止使用匿名用户登录. 2. Forms验证: 通过Cookies来保存用户凭证,对未登录的用户 重定向到自 ...
- sql server的两个类型转换函数
今天遇到一个sql的问题,条件中有个去当前月第一天(2013-8-23 0:00:00),很简单CAST(DATEADD(dd,-DAY(GETDATE())+1,GETDATE()) AS DATE ...
- 【转】关于ios10中ATS的问题
原文连接:https://onevcat.com/2016/06/ios-10-ats/ WWDC 15 提出的 ATS (App Transport Security) 是 Apple 在推进网络通 ...
- ps 网页配图设计
网站配图设计 蒙太奇 品科软件---网页页面 1橡皮擦来画两图 容合 大橡皮擦擦出来自然 2图放到一个色块中 用剪贴蒙版 3调色阶 装饰下图片 矩形工具 形状 填充 画彩条 超出本框的怎么去掉多 ...
- (转)发现两个有用的C函数_alloca()、_msize()
转自: http://blog.csdn.net/pony12/article/details/8678071 (1)_alloca()alloca也是用来分配存储空间的,它和malloc的区别是它是 ...
- 奇葩问题:spring+mybaits项目突然出现其中一些Mapper类找不到
一.问题现象 1,No bean named 'bomManageMapper' found in org.springframework.beans.factory.support.DefaultL ...
- C#导出Word文档开源组件DocX
1.帮助文档,这东西找了很久,而且它版本很旧,还是英文,W8.1系统上打不开 http://download.csdn.net/detail/zuofangyouyuan/7673573 2.开源网址 ...
- python Tkinter 全屏显示
#! /usr/bin/env python # -*- coding: utf-8 -*- import Tkinter as tk class FullScreenApp(object): def ...
- Sql Server专题三:SQL操作与技巧
一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...