QuantLib: a free/open-source library for quantitative finance
Reference manual - version 1.40
Loading...
Searching...
No Matches
Singleton< T, Global > Class Template Reference

Basic support for the singleton pattern. More...

#include <ql/patterns/singleton.hpp>

Public Member Functions

 Singleton (const Singleton &)=delete
 Singleton (Singleton &&)=delete
Singletonoperator= (const Singleton &)=delete
Singletonoperator= (Singleton &&)=delete

Static Public Member Functions

static T & instance ()
 access to the unique instance

Detailed Description

template<class T, class Global = std::integral_constant<bool, false>>
class QuantLib::Singleton< T, Global >

Basic support for the singleton pattern.

The typical use of this class is:

class Foo : public Singleton<Foo> {
friend class Singleton<Foo>;
private:
Foo() {}
public:
...
};

which, albeit sub-optimal, frees one from the concerns of creating and managing the unique instance and can serve later as a single implemementation point should synchronization features be added.

Global can be used to distinguish Singletons that are local to a session (Global = false) or that are global across all sessions (B = true). This is only relevant if QL_ENABLE_SESSIONS is enabled.

Notice that the creation and retrieval of (local or global) singleton instances through instance() is thread safe, but obviously subsequent operations on the singleton have to be synchronized within the singleton implementation itself.