说明上其实很明白,支持多线程,防止重复创建,同时支持如果删除以后就不在创建,利用局部静态变量进行标记。挺通用,看来下次写个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的更多相关文章

  1. Qt 中使用Singleton模式需小心

    在qt中,使用Singleton模式时一定要小心.因为Singleton模式中使用的是静态对象,静态对象是直到程序结束才被释放的,然而,一旦把该静态对象纳入了Qt的父子对象体系,就会导致不明确的行为. ...

  2. [原] blade中C++ singleton的实现

    最近看了龚大大KalyGE中的singleton, 觉得非常不错(C++中线程安全并且高效的singleton). 可惜blade的代码都是C++03的, 没有使用C++11的任何特性. 笔者对于si ...

  3. juce中的BailOutChecker

    界面库中值得注意的一点就是对象响应事件的时候自身被删除了,那么后续的访问自然就会出问题,所以需要在响应事件之后先添加引用,相关处理之后再查看自身是否已经被删除,如果已经被删除那么就直接退出.juce中 ...

  4. juce 中的WeakReference分析

    juce中的WeakReference设计得比较巧妙,巧妙就是使用delete之后就可以通知道WeakReference,原理其实也很间单,其实就是在对象里添加了一个子对象masterReferenc ...

  5. juce中的timer

    juce中timer总体说还是比较好用的,使用时只需继承timer类, 重写callback然后调用start就可以了,juce的timer比较特别,自己通过线程实现,starttimer的时候会创建 ...

  6. C#中实例Singleton

    [C#中实例Singleton] 1.经典方案: using System; public class Singleton { private static Singleton instance; p ...

  7. juce中的引用计数

    这个类提供了最基本的引用计数管理,界面库中,经常都需要消息发送,而带来的后果就是不知道消息中包含的对象是否还存在,如果不能很好管理的话就容易出现访问销毁了的对象这样的情况,所以,juce的界面无素也基 ...

  8. 面试中的Singleton

      引子 “请写一个Singleton.”面试官微笑着和我说. “这可真简单.”我心里想着,并在白板上写下了下面的Singleton实现: 1 class Singleton 2 { 3 public ...

  9. C++面试中的singleton类

    引子 “请写一个Singleton.”面试官微笑着和我说. “这可真简单.”我心里想着,并在白板上写下了下面的Singleton实现: 1 class Singleton 2 { 3 public: ...

随机推荐

  1. android XML解析之DOM解析方式

    DOM 解析方式步骤: 第一步:首选需要获得DOM解析器工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ...

  2. 改变页面选择文字颜色和背景颜色----selection伪元素

    div::selection{color:#fff;background: #E83E84;text-shadow:none}  

  3. My97DatePicker使用的问题

    我在iframe中使用My97DatePicker时,发现第一次点击左边的菜单时,在右边的页面可以弹出日期框: 当我第二次点击菜单时,右边的日期文本框却弹出了页面的内容,而不是日期选择框: 首先怀疑是 ...

  4. jQuery 方法

    方法 描述 animate() 对被选元素应用"自定义"的动画 clearQueue() 对被选元素移除所有排队函数(仍未运行的) delay() 对被选元素的所有排队函数(仍未运 ...

  5. python 数据类型之数float

    1.float 对象有一个重要的方法is_intger.如果这个float对应在转化为int时不会有精度的丢失就返回True,不然就返回False #!/usr/bin/python #!coding ...

  6. Macbook使用技巧

     Mac  OSX下 safari 常用快捷键盘     Command + R  刷新页面     Command + T  新建一个标签      Command + Shift+ 左右方向键   ...

  7. Inno Setup 网页显示插件 webctrl (V2.1 版本)

    原文 http://restools.hanzify.org/article.asp?id=90 Inno Setup网页显示插件 webctrl能够显示所有 IE 中能够显示的东西.  引用内容 ; ...

  8. Ruby小例子

    1.ruby定义函数与执行函数案例 def fact(n) ) end end print fact() 结果: 24 2.一个小例子 words = [)] print "guess?\n ...

  9. 重新mysql-server

    apt-get purge mysql-server │apt-get purge mysql-common │rm -rf /var/log/mysql │rm -rf /var/log/mysql ...

  10. android 读取txt文件内容

    Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问. 比如我们 ...