48 #ifndef _MIRA_SINGLETON_H_ 49 #define _MIRA_SINGLETON_H_ 52 #include <boost/noncopyable.hpp> 53 #include <boost/type_traits/remove_cv.hpp> 54 #include <boost/thread/mutex.hpp> 82 static void* getSharedLibrarySingleton(
const std::type_info&
type);
83 static void setSharedLibrarySingleton(
const std::type_info&
type,
86 static void* getSharedLibrarySingleton(
const std::type_info&
type)
90 static void setSharedLibrarySingleton(
const std::type_info&
type,
97 typedef void (*atexit_lifetime_fn) (void);
105 template <
typename T,
106 template <
class>
class I,
107 template <
class>
class C,
108 template <
class>
class L,
110 struct SingletonTrait
114 typedef I<SingletonTrait> InstantiationPolicy;
115 typedef C<Type> CreationPolicy;
116 typedef L<Type> LifetimePolicy;
117 typedef M LockPolicy;
126 template <
typename Trait>
127 class SingletonBase :
public SingletonSharedLibraryHelper, boost::noncopyable
130 typedef typename Trait::InstantiationPolicy InstantiationPolicy;
131 typedef typename Trait::CreationPolicy CreationPolicy;
132 typedef typename Trait::LifetimePolicy LifetimePolicy;
133 typedef typename Trait::LockPolicy LockPolicy;
134 typedef typename Trait::Type Type;
139 static TypePtr sInstance;
140 static bool sDestroyed;
145 template <
typename Trait>
146 typename SingletonBase<Trait>::TypePtr SingletonBase<Trait>::sInstance = NULL;
149 template <
typename Trait>
150 bool SingletonBase<Trait>::sDestroyed =
false;
159 namespace singleton {
173 template <
typename T>
189 template <
typename T>
193 # define MIRA_CREATESTATIC_ALIGN __declspec(align(16)) 195 # define MIRA_CREATESTATIC_ALIGN __attribute__((aligned(16))) 201 return new(&staticMemory) T;
206 #undef MIRA_CREATESTATIC_ALIGN 220 template <
template <
class>
class Alloc>
222 template <
typename T>
225 static T*
create() {
return new (alloc.allocate(1)) T; }
228 alloc.deallocate(p,1);
242 template <
typename T>
256 template <
typename T>
273 template <
typename Trait>
276 typedef Private::SingletonBase<Trait> Base;
279 typedef typename Base::Type
Type;
284 return *((
Type*)Base::sInstance);
292 static typename Base::LockPolicy::Mutex sMutex;
295 typename Base::LockPolicy::Lock lock(sMutex);
300 if(!Base::sInstance) {
303 << typeName<Type>() <<
"' was already destroyed!");
305 Type* p =
static_cast<Type*
>(Base::getSharedLibrarySingleton(
typeid(
Type)));
307 p = Base::CreationPolicy::create();
308 Base::LifetimePolicy::scheduleDestruction(p,&destroyInstance);
309 Base::setSharedLibrarySingleton(
typeid(
Type),p);
314 assert(Base::sInstance);
315 return (
Type*)Base::sInstance;
319 if (!Base::sDestroyed) {
320 Base::CreationPolicy::destroy((
Type*)Base::sInstance);
321 Base::sInstance=NULL;
322 Base::sDestroyed=
true;
338 template <
typename Trait>
344 typedef typename Base::Type
Type;
347 if(!sInstantiationHelper)
348 sInstantiationHelper = Base::makeInstance();
350 assert(sInstantiationHelper);
351 return *sInstantiationHelper;
355 static void use(
const Type*) {}
356 static Type* forceInstantiation() {
357 Type* inst = Base::makeInstance();
360 use(sInstantiationHelper);
364 static Type* sInstantiationHelper;
366 template <
typename Trait>
367 typename EagerInstantiation<Trait>::Type* EagerInstantiation<Trait>::sInstantiationHelper = EagerInstantiation<Trait>::forceInstantiation();
384 template <
typename Trait>
387 typedef Private::SingletonBase<Trait> Base;
390 typedef typename Base::Type
Type;
393 if(Base::sInstance!=NULL || Base::getSharedLibrarySingleton(
typeid(
Type))!=NULL)
394 MIRA_THROW(XSingleton,
"There should be just one object of '" 395 << typeName<Type>() <<
"', you tried to initialize another one!");
396 Base::sInstance =
static_cast<Type*
>(
this);
397 Base::setSharedLibrarySingleton(
typeid(
Type),
this);
398 Base::sDestroyed=
false;
406 Base::sInstance = NULL;
407 Base::setSharedLibrarySingleton(
typeid(
Type), NULL);
408 Base::sDestroyed=
true;
413 if(Base::sInstance==NULL) {
414 Type* p =
static_cast<Type*
>(Base::getSharedLibrarySingleton(
typeid(
Type)));
417 MIRA_THROW(XSingleton,
"No instance of the explicit singleton '" 418 << typeName<Type>() <<
"' created, " 419 "or instance was already destroyed. " 420 "You must create one object of this class before " 421 "you can start using it!");
425 return *((
Type*)Base::sInstance);
444 template <
typename T>
461 typedef boost::mutex::scoped_lock
Lock;
464 template <
typename T>
526 template <
typename T,
531 class Singleton :
public TInstantiationPolicy<Private::SingletonTrait<T,TInstantiationPolicy,TCreationPolicy,TLifetimePolicy,TLockPolicy>>
533 typedef TInstantiationPolicy<Private::SingletonTrait<T,TInstantiationPolicy,TCreationPolicy,TLifetimePolicy,TLockPolicy>> Base;
537 typedef typename Base::Type
Type;
545 return Base::instance();
553 return Base::sDestroyed;
563 template <
typename T>
571 template <
typename T>
579 template <
typename T>
587 template <
typename T>
589 singleton::CreateUsingNew, singleton::NormalLifetime,
590 singleton::NoLock> {};
597 template <
typename T>
599 singleton::CreateUsingNew, singleton::NormalLifetime,
600 singleton::NoLock> {};
ExplicitInstantiation()
Definition: Singleton.h:392
#define MIRA_DEFINE_EXCEPTION(Ex, Base)
Macro for easily defining a new compatible exception class.
Definition: Exception.h:170
Provided for convenience.
Definition: Singleton.h:598
Definition: Singleton.h:445
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:174
static void destroy(T *p)
Definition: Singleton.h:203
Implementation of the LockPolicy that is used by the Singleton template.
Definition: Singleton.h:458
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:544
Implementation of the LifetimePolicy that is used by the Singleton template.
Definition: Singleton.h:243
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
static Type * makeInstance()
Definition: Singleton.h:289
static Type & instance()
Definition: Singleton.h:281
Implementation of the InstantiationPolicy that is used by the Singleton template. ...
Definition: Singleton.h:385
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:221
Definition: Singleton.h:223
boost::mutex Mutex
Definition: Singleton.h:460
Provided for convenience.
Definition: Singleton.h:572
static void destroy(T *p)
Definition: Singleton.h:176
Provided for convenience.
Definition: Singleton.h:564
Get compiler and platform independent typenames.
static bool isDestroyed()
Returns true, if the singleton was already destroyed.
Definition: Singleton.h:552
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
static void destroyInstance(void)
Definition: Singleton.h:318
static T * create()
Definition: Singleton.h:225
Base::Type Type
Definition: Singleton.h:390
static T * create()
Definition: Singleton.h:198
static Type & instance()
Definition: Singleton.h:346
Implementation of the InstantiationPolicy that is used by the Singleton template. ...
Definition: Singleton.h:339
Provided for convenience.
Definition: Singleton.h:588
A singleton template class that can be freely configured using policies that control the instantiatio...
Definition: Singleton.h:531
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Definition: Singleton.h:441
Provided for convenience.
Definition: Singleton.h:580
Definition: Singleton.h:465
~ExplicitInstantiation()
Destroys and removes the shared singleton instance.
Definition: Singleton.h:403
static void scheduleDestruction(T *, Private::atexit_lifetime_fn fun)
Definition: Singleton.h:245
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:190
Lock(int)
Definition: Singleton.h:442
static void scheduleDestruction(T *, Private::atexit_lifetime_fn fun)
Definition: Singleton.h:259
volatile T type
Definition: Singleton.h:466
#define MIRA_CREATESTATIC_ALIGN
Definition: Singleton.h:195
static void destroy(T *p)
Definition: Singleton.h:226
Base::Type Type
Definition: Singleton.h:279
boost::mutex::scoped_lock Lock
Definition: Singleton.h:461
Base::Type Type
Definition: Singleton.h:344
Implementation of the InstantiationPolicy that is used by the Singleton template. ...
Definition: Singleton.h:274
static T * create()
Definition: Singleton.h:175
int Mutex
Definition: Singleton.h:440
Base::Type Type
Definition: Singleton.h:537
static Type & instance()
Definition: Singleton.h:411
Implementation of the LockPolicy that is used by the Singleton template.
Definition: Singleton.h:438
Implementation of the LifetimePolicy that is used by the Singleton template.
Definition: Singleton.h:257
T type
Definition: Singleton.h:446
static Alloc< T > alloc
Definition: Singleton.h:224