码迷,mamicode.com
首页 > 其他好文 > 详细

juce中的Singleton

时间:2016-05-05 08:22:13      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/csxy/p/5460429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!