Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahSingleton.H File Reference

Minimal, header-only singleton helpers using the Meyers singleton pattern. More...

This graph shows which files directly or indirectly include this file:

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.
 

Detailed Description

Minimal, header-only singleton helpers using the Meyers singleton pattern.

This header provides two mechanisms for implementing the Singleton design pattern:

  1. Make_Singleton(T) macro: Injects singleton functionality into any class. This is the recommended approach for most use cases.
  2. Singleton class: A ready-to-use singleton class provided as an example and for cases where a minimal singleton is needed.

Thread Safety

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:

  • Concurrent calls to get_instance() are safe.
  • The singleton instance is created exactly once.
  • No explicit locking or synchronization is required.

Object Lifetime

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.

Warning
Be cautious of the "static initialization order fiasco" if your singleton depends on other static objects during construction or destruction.

Usage Example

// Using the macro (recommended)
class Logger
{
private:
Logger() { // initialize logger }
public:
void log(const std::string & msg) { // ... }
};
// Usage:
Logger::get_instance().log("Hello, World!");
#define Make_Singleton(name)
Macro to inject Meyers singleton functionality into a class.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4063
Author
Leandro Rabindranath León

Definition in file ahSingleton.H.

Macro Definition Documentation

◆ Make_Singleton

#define Make_Singleton (   name)
Value:
public: \
\
\
[[nodiscard]] static name & get_instance() noexcept \
{ \
static name instance; \
return instance; \
} \
\
name(const name &) = delete; \
name(name &&) = delete; \
name & operator = (const name &) = delete; \
name & operator = (name &&) = delete;

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.

Usage

Place the macro at the beginning of your class definition:

class MyClass
{
Make_Singleton(MyClass)
private:
MyClass() = default; // Constructor must be accessible from get_instance()
public:
// Public interface...
void doSomething();
};

Requirements

  • The type must be default-constructible.
  • The default constructor must be accessible from the get_instance() method. This typically means either:
    • Place Make_Singleton(T) before the private: section containing the constructor, or
    • Make the constructor private but place Make_Singleton(T) inside the class (it becomes a friend of the static method context).

Guarantees

  • Single instance: All calls to get_instance() return the same object.
  • Thread-safe initialization: Guaranteed by C++11 and later standards.
  • Non-copyable/non-movable: Copy and move operations are explicitly deleted.
Parameters
nameThe name of the class to make into a singleton.
Note
The macro expands to public: visibility, so any declarations after the macro will be public unless you explicitly change visibility.

Definition at line 138 of file ahSingleton.H.