Quantum++  v1.0-rc4
A modern C++11 quantum computing library
qpp::internal::Singleton< T > Class Template Reference

Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurring template pattern) More...

#include <internal/classes/singleton.h>

Static Public Member Functions

static T & get_instance () noexcept(std::is_nothrow_constructible< T >::value)
 
static T & get_thread_local_instance () noexcept(std::is_nothrow_constructible< T >::value)
 

Protected Member Functions

 Singleton () noexcept=default
 
 Singleton (const Singleton &)=delete
 
Singletonoperator= (const Singleton &)=delete
 
virtual ~Singleton ()=default
 

Detailed Description

template<typename T>
class qpp::internal::Singleton< T >

Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurring template pattern)

To implement a singleton, derive your class from qpp::internal::Singleton, make qpp::internal::Singleton a friend of your class, then declare the constructor and destructor of your class as private. To get an instance, use the static member function qpp::internal::Singleton::get_instance() (qpp::internal::Singleton::get_thread_local_instance()), which returns a reference (thread_local reference) to your newly created singleton (thread-safe in C++11).

Example:

class MySingleton: public qpp::internal::Singleton<MySingleton>
{
friend class qpp::internal::Singleton<MySingleton>;
public:
// Declare all public members here
private:
MySingleton()
{
// Implement the constructor here
}
~MySingleton()
{
// Implement the destructor here
}
};
MySingleton& mySingleton = MySingleton::get_instance(); // Get an instance
thread_local MySingleton& tls = MySingleton::get_thread_local_instance();
// Get a thread_local instance
See also
Code of qpp::Codes, qpp::Gates, qpp::Init, qpp::RandomDevices, qpp::States or qpp.h for real world examples of usage.

Constructor & Destructor Documentation

◆ Singleton() [1/2]

template<typename T>
qpp::internal::Singleton< T >::Singleton ( )
protecteddefaultnoexcept

◆ Singleton() [2/2]

template<typename T>
qpp::internal::Singleton< T >::Singleton ( const Singleton< T > &  )
protecteddelete

◆ ~Singleton()

template<typename T>
virtual qpp::internal::Singleton< T >::~Singleton ( )
protectedvirtualdefault

Member Function Documentation

◆ get_instance()

template<typename T>
static T& qpp::internal::Singleton< T >::get_instance ( )
inlinestaticnoexcept

◆ get_thread_local_instance()

template<typename T>
static T& qpp::internal::Singleton< T >::get_thread_local_instance ( )
inlinestaticnoexcept

◆ operator=()

template<typename T>
Singleton& qpp::internal::Singleton< T >::operator= ( const Singleton< T > &  )
protecteddelete

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