48 #ifndef _MIRA_SINGLETON_H_ 49 #define _MIRA_SINGLETON_H_ 52 #include <type_traits> 53 #include <boost/thread/mutex.hpp> 68 typedef void (*atexit_lifetime_fn) (void);
77 template <
class>
class I,
78 template <
class>
class C,
79 template <
class>
class L,
85 typedef I<SingletonTrait> InstantiationPolicy;
86 typedef C<Type> CreationPolicy;
87 typedef L<Type> LifetimePolicy;
97 template <
typename Trait>
101 typedef typename Trait::InstantiationPolicy InstantiationPolicy;
102 typedef typename Trait::CreationPolicy CreationPolicy;
103 typedef typename Trait::LifetimePolicy LifetimePolicy;
104 typedef typename Trait::LockPolicy LockPolicy;
105 typedef typename Trait::Type Type;
108 SingletonBase() =
default;
109 SingletonBase(
const SingletonBase&) =
delete;
110 SingletonBase& operator=(
const SingletonBase&) =
delete;
114 static TypePtr sInstance;
115 static bool sDestroyed;
120 template <
typename Trait>
121 typename SingletonBase<Trait>::TypePtr SingletonBase<Trait>::sInstance = NULL;
124 template <
typename Trait>
125 bool SingletonBase<Trait>::sDestroyed =
false;
134 namespace singleton {
148 template <
typename T>
164 template <
typename T>
168 # define MIRA_CREATESTATIC_ALIGN __declspec(align(16)) 170 # define MIRA_CREATESTATIC_ALIGN __attribute__((aligned(16))) 176 return new(&staticMemory) T;
181 #undef MIRA_CREATESTATIC_ALIGN 195 template <
template <
class>
class Alloc>
197 template <
typename T>
200 static T*
create() {
return new (alloc.allocate(1)) T; }
203 alloc.deallocate(p,1);
217 template <
typename T>
231 template <
typename T>
248 template <
typename Trait>
251 typedef Private::SingletonBase<Trait> Base;
254 typedef typename Base::Type
Type;
259 return *((
Type*)Base::sInstance);
267 static typename Base::LockPolicy::Mutex sMutex;
270 typename Base::LockPolicy::Lock lock(sMutex);
275 if(!Base::sInstance) {
278 << typeName<Type>() <<
"' was already destroyed!");
280 Type* p = Base::CreationPolicy::create();
281 Base::LifetimePolicy::scheduleDestruction(p,&destroyInstance);
285 assert(Base::sInstance);
286 return (
Type*)Base::sInstance;
290 if (!Base::sDestroyed) {
291 Base::CreationPolicy::destroy((
Type*)Base::sInstance);
292 Base::sInstance=NULL;
293 Base::sDestroyed=
true;
309 template <
typename Trait>
315 typedef typename Base::Type
Type;
318 if(!sInstantiationHelper)
319 sInstantiationHelper = Base::makeInstance();
321 assert(sInstantiationHelper);
322 return *sInstantiationHelper;
326 static void use(
const Type*) {}
327 static Type* forceInstantiation() {
328 Type* inst = Base::makeInstance();
331 use(sInstantiationHelper);
335 static Type* sInstantiationHelper;
337 template <
typename Trait>
338 typename EagerInstantiation<Trait>::Type* EagerInstantiation<Trait>::sInstantiationHelper = EagerInstantiation<Trait>::forceInstantiation();
355 template <
typename Trait>
358 typedef Private::SingletonBase<Trait> Base;
361 typedef typename Base::Type
Type;
364 if(Base::sInstance!=NULL)
365 MIRA_THROW(XSingleton,
"There should be just one object of '" 366 << typeName<Type>() <<
"', you tried to initialize another one!");
367 Base::sInstance =
static_cast<Type*
>(
this);
368 Base::sDestroyed=
false;
376 Base::sInstance = NULL;
377 Base::sDestroyed=
true;
382 if(Base::sInstance==NULL) {
383 MIRA_THROW(XSingleton,
"No instance of the explicit singleton '" 384 << typeName<Type>() <<
"' created, " 385 "or instance was already destroyed. " 386 "You must create one object of this class before " 387 "you can start using it!");
389 return *((
Type*)Base::sInstance);
408 template <
typename T>
425 typedef boost::mutex::scoped_lock
Lock;
428 template <
typename T>
490 template <
typename T,
495 class Singleton :
public TInstantiationPolicy<Private::SingletonTrait<T,TInstantiationPolicy,TCreationPolicy,TLifetimePolicy,TLockPolicy>>
497 typedef TInstantiationPolicy<Private::SingletonTrait<T,TInstantiationPolicy,TCreationPolicy,TLifetimePolicy,TLockPolicy>> Base;
501 typedef typename Base::Type
Type;
509 return Base::instance();
517 return Base::sDestroyed;
527 template <
typename T>
535 template <
typename T>
543 template <
typename T>
551 template <
typename T>
553 singleton::CreateUsingNew, singleton::NormalLifetime,
554 singleton::NoLock> {};
561 template <
typename T>
563 singleton::CreateUsingNew, singleton::NormalLifetime,
564 singleton::NoLock> {};
ExplicitInstantiation()
Definition: Singleton.h:363
#define MIRA_DEFINE_EXCEPTION(Ex, Base)
Macro for easily defining a new compatible exception class.
Definition: Exception.h:166
Provided for convenience.
Definition: Singleton.h:562
Definition: Singleton.h:409
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:149
static void destroy(T *p)
Definition: Singleton.h:178
Implementation of the LockPolicy that is used by the Singleton template.
Definition: Singleton.h:422
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:508
Implementation of the LifetimePolicy that is used by the Singleton template.
Definition: Singleton.h:218
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
static Type * makeInstance()
Definition: Singleton.h:264
static Type & instance()
Definition: Singleton.h:256
Implementation of the InstantiationPolicy that is used by the Singleton template. ...
Definition: Singleton.h:356
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:196
Definition: Singleton.h:198
boost::mutex Mutex
Definition: Singleton.h:424
Provided for convenience.
Definition: Singleton.h:536
static void destroy(T *p)
Definition: Singleton.h:151
Provided for convenience.
Definition: Singleton.h:528
Get compiler and platform independent typenames.
static bool isDestroyed()
Returns true, if the singleton was already destroyed.
Definition: Singleton.h:516
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
static void destroyInstance(void)
Definition: Singleton.h:289
static T * create()
Definition: Singleton.h:200
Base::Type Type
Definition: Singleton.h:361
static T * create()
Definition: Singleton.h:173
static Type & instance()
Definition: Singleton.h:317
Implementation of the InstantiationPolicy that is used by the Singleton template. ...
Definition: Singleton.h:310
Provided for convenience.
Definition: Singleton.h:552
A singleton template class that can be freely configured using policies that control the instantiatio...
Definition: Singleton.h:495
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Definition: Singleton.h:405
Provided for convenience.
Definition: Singleton.h:544
Definition: Singleton.h:429
~ExplicitInstantiation()
Destroys and removes the shared singleton instance.
Definition: Singleton.h:373
static void scheduleDestruction(T *, Private::atexit_lifetime_fn fun)
Definition: Singleton.h:220
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:165
Lock(int)
Definition: Singleton.h:406
static void scheduleDestruction(T *, Private::atexit_lifetime_fn fun)
Definition: Singleton.h:234
volatile T type
Definition: Singleton.h:430
#define MIRA_CREATESTATIC_ALIGN
Definition: Singleton.h:170
static void destroy(T *p)
Definition: Singleton.h:201
Base::Type Type
Definition: Singleton.h:254
boost::mutex::scoped_lock Lock
Definition: Singleton.h:425
Base::Type Type
Definition: Singleton.h:315
Implementation of the InstantiationPolicy that is used by the Singleton template. ...
Definition: Singleton.h:249
static T * create()
Definition: Singleton.h:150
int Mutex
Definition: Singleton.h:404
Base::Type Type
Definition: Singleton.h:501
static Type & instance()
Definition: Singleton.h:380
Implementation of the LockPolicy that is used by the Singleton template.
Definition: Singleton.h:402
Implementation of the LifetimePolicy that is used by the Singleton template.
Definition: Singleton.h:232
T type
Definition: Singleton.h:410
static Alloc< T > alloc
Definition: Singleton.h:199