Files
TCPlayerbotCore/dep/include/zthread/Singleton.h
T

251 lines
5.9 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (c) 2005, Eric Crahen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ZTSINGLETON_H__
#define __ZTSINGLETON_H__
#include "zthread/Guard.h"
#include "zthread/FastMutex.h"
#include <assert.h>
namespace ZThread {
//
// This policy controls how an object is instantiated
// as well as how and when its destroyed. Phoenix-style
2009-02-09 08:16:34 -05:00
// singletons are not supported easily with type of policy,
2008-10-02 16:23:55 -05:00
// this is intentional since I do not believe that is in
// the true spirit of a singleton.
//
// InstantiationPolicContract {
//
// create(pointer_type&)
//
// }
/**
* @class LocalStaticInstantiation
* @author Eric Crahen <http://www.code-foo.com>
* @date <2003-07-16T17:57:45-0400>
2009-02-09 08:16:34 -05:00
* @version 2.2.0
2008-10-02 16:23:55 -05:00
*
2009-02-09 08:16:34 -05:00
* The LocalStaticInstantiation policy allows the creation
2008-10-02 16:23:55 -05:00
* and lifetime of an instance of a particular type
* to be managed using static local values. This will
* abide by the standard C++ rules for static objects
* lifetimes.
*/
class LocalStaticInstantiation {
protected:
/**
* Create an instance of an object, using a local static. The
* object will be destroyed by the system.
*
2009-02-09 08:16:34 -05:00
* @param ptr reference to location to receive the address
2008-10-02 16:23:55 -05:00
* of the allocated object
*/
template <class T>
static void create(T*& ptr) {
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
static T instance;
ptr = &instance;
}
};
//! Helper class
template <class T>
class StaticInstantiationHelper {
//! Friend class
friend class StaticInstantiation;
//! Holder
static T instance;
};
template <class T>
T StaticInstantiationHelper<T>::instance;
/**
* @class StaticInstantiation
* @author Eric Crahen <http://www.code-foo.com>
* @date <2003-07-16T17:57:45-0400>
2009-02-09 08:16:34 -05:00
* @version 2.2.0
2008-10-02 16:23:55 -05:00
*
2009-02-09 08:16:34 -05:00
* The StaticInstantiation policy allows the creation
2008-10-02 16:23:55 -05:00
* and lifetime of an instance of a particular type
* to be managed using static instantiation. This will
* abide by the standard C++ rules for static objects
* lifetimes.
*/
class StaticInstantiation {
protected:
/**
* Create an instance of an object using by simply allocating it statically.
*
2009-02-09 08:16:34 -05:00
* @param ptr reference to location to receive the address
2008-10-02 16:23:55 -05:00
* of the allocated object
*/
template <class T>
static void create(T*& ptr) {
ptr = &StaticInstantiationHelper<T>::instance;
}
};
//! SingletonDestroyer
template <class T>
class Destroyer {
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
T* doomed;
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
public:
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
Destroyer(T* q) : doomed(q) {
assert(doomed);
}
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
~Destroyer();
};
template <class T>
Destroyer<T>::~Destroyer() {
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
try {
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
if(doomed)
delete doomed;
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
} catch(...) { }
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
doomed = 0;
2009-02-09 08:16:34 -05:00
}
2008-10-02 16:23:55 -05:00
/**
* @class LazyInstantiation
* @author Eric Crahen <http://www.code-foo.com>
* @date <2003-07-16T17:57:45-0400>
* @version 2.2.0
*
2009-02-09 08:16:34 -05:00
* The LazyInstantiation policy allows the creation
2008-10-02 16:23:55 -05:00
* and lifetime of an instance of a particular type
* to be managed using dynamic allocation and a singleton
2009-02-09 08:16:34 -05:00
* destroyer. This will abide by the standard C++ rules
2008-10-02 16:23:55 -05:00
* for static objects lifetimes.
*/
class LazyInstantiation {
protected:
/**
* Create an instance of an object, using new, that will be
* destroyed when an associated Destroyer object (allocated
* statically) goes out of scope.
*
2009-02-09 08:16:34 -05:00
* @param ptr reference to location to receive the address
2008-10-02 16:23:55 -05:00
* of the allocated object
*/
template <class T>
static void create(T*& ptr) {
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
ptr = new T;
static Destroyer<T> destroyer(ptr);
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
}
};
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
/**
* @class Singleton
* @author Eric Crahen <http://www.code-foo.com>
* @date <2003-07-16T17:57:45-0400>
2009-02-09 08:16:34 -05:00
* @version 2.2.0
2008-10-02 16:23:55 -05:00
*
* Based on the work of John Vlissidles in his book 'Pattern Hatching'
* an article by Douglas Schmidtt on double-checked locking and policy
* templates described by Andrei Alexandrescu.
*
2009-02-09 08:16:34 -05:00
* This is a thread safe wrapper for creating Singleton classes. The
2008-10-02 16:23:55 -05:00
* synchronization method and instantiation methods can be changed
2009-02-09 08:16:34 -05:00
* easily by specifying different policy implementations as the
2008-10-02 16:23:55 -05:00
* templates parameters.
*
* @code
*
* // Most common Singleton
* Singletion<LonesomeType>
*
2009-02-09 08:16:34 -05:00
* // Singleton that uses static storage
2008-10-02 16:23:55 -05:00
* Singletion<LonesomeType, StaticInstantiation>
*
* // Single-threaded singleton that uses static storage (Meyers-like)
* Singletion<LonesomeType, LocalStaticInstantiation, NotLocked>
*
* @endcode
*/
template <class T, class InstantiationPolicy=LazyInstantiation, class LockType=FastMutex>
class Singleton : private InstantiationPolicy, private NonCopyable {
public:
/**
2009-02-09 08:16:34 -05:00
* Provide access to the single instance through double-checked locking
2008-10-02 16:23:55 -05:00
*
2009-02-09 08:16:34 -05:00
* @return T* single instance
2008-10-02 16:23:55 -05:00
*/
static T* instance();
};
template <class T, class InstantiationPolicy, class LockType>
T* Singleton<T, InstantiationPolicy, LockType>::instance() {
// Uses local static storage to avoid static construction
// sequence issues. (regaring when the lock is created)
2009-02-09 08:16:34 -05:00
static T* ptr = 0;
2008-10-02 16:23:55 -05:00
static LockType lock;
if(!ptr) {
Guard<LockType, LockedScope> g(lock);
2009-02-09 08:16:34 -05:00
if(!ptr)
2008-10-02 16:23:55 -05:00
InstantiationPolicy::create(ptr);
}
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
return const_cast<T*>(ptr);
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
}
};
#endif
2009-02-17 20:07:49 -06:00