Fabcoin Core  0.16.2
P2P Digital Currency
Public Member Functions | Private Attributes | List of all members
Singleton< T, F, instance > Class Template Reference

Restricts the instantiation of a class to one static object without locks. More...

#include <misc.h>

Public Member Functions

 Singleton (F objectFactory=F())
 
CRYPTOPP_NOINLINE const TRef (CRYPTOPP_NOINLINE_DOTDOTDOT) const
 Return a reference to the inner Singleton object. More...
 

Private Attributes

F m_objectFactory
 

Detailed Description

template<class T, class F = NewObject<T>, int instance = 0>
class Singleton< T, F, instance >

Restricts the instantiation of a class to one static object without locks.

Template Parameters
Tthe class or type
Fthe object factory for T
instancethe initiali instance count

This class safely initializes a static object in a multithreaded environment. For C++03 and below it will do so without using locks for portability. If two threads call Ref() at the same time, they may get back different references, and one object may end up being memory leaked. This is by design and it avoids a subltle initialization problem ina multithreaded environment with thread local storage on early Windows platforms, like Windows XP and Windows 2003.

For C++11 and above, a standard double-checked locking pattern with thread fences are used. The locks and fences are standard and do not hinder portability.

See also
Double-Checked Locking is Fixed In C++11

Definition at line 274 of file misc.h.

Constructor & Destructor Documentation

template<class T, class F = NewObject<T>, int instance = 0>
Singleton< T, F, instance >::Singleton ( F  objectFactory = F())
inline

Definition at line 277 of file misc.h.

Member Function Documentation

template<class T , class F , int instance>
const T & Singleton< T, F, instance >::Ref ( CRYPTOPP_NOINLINE_DOTDOTDOT  ) const

Return a reference to the inner Singleton object.

Ref() is used to create the object using the object factory. The object is only created once with the limitations discussed in the class documentation.

See also
Double-Checked Locking is Fixed In C++11

Definition at line 318 of file misc.h.

Member Data Documentation

template<class T, class F = NewObject<T>, int instance = 0>
F Singleton< T, F, instance >::m_objectFactory
private

Definition at line 283 of file misc.h.


The documentation for this class was generated from the following file: