|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Minimal, header-only singleton helpers using the Meyers singleton pattern. More...
Go to the source code of this file.
Classes | |
| class | Singleton |
| A minimal singleton class implementation. More... | |
Macros | |
| #define | Make_Singleton(name) |
| Macro to inject Meyers singleton functionality into a class. | |
Minimal, header-only singleton helpers using the Meyers singleton pattern.
This header provides two mechanisms for implementing the Singleton design pattern:
Both mechanisms use function-local static variables (Meyers singleton). Since C++11, the initialization of function-local static variables is guaranteed to be thread-safe by the standard (§6.7 [stmt.dcl] paragraph 4). This means:
get_instance() are safe.The singleton instance is constructed on first call to get_instance() and destroyed at program termination. Destruction follows the usual static deinitialization rules: objects are destroyed in reverse order of their construction across all translation units.
Definition in file ahSingleton.H.
| #define Make_Singleton | ( | name | ) |
Macro to inject Meyers singleton functionality into a class.
This macro adds a static get_instance() method that returns a reference to the unique instance of the class, and deletes copy/move constructors and assignment operators to prevent duplication.
Place the macro at the beginning of your class definition:
get_instance() method. This typically means either:Make_Singleton(T) before the private: section containing the constructor, orprivate but place Make_Singleton(T) inside the class (it becomes a friend of the static method context).get_instance() return the same object.| name | The name of the class to make into a singleton. |
public: visibility, so any declarations after the macro will be public unless you explicitly change visibility. Definition at line 138 of file ahSingleton.H.