#include <Pt/Singleton.h>
Process-wide single instance of T. More...
Inherits NonCopyable.
Static Public Member Functions | |
| static T & | instance () |
| Returns the process-wide instance, creating it on first call. | |
Protected Member Functions | |
| Singleton () | |
| Constructor. | |
| ~Singleton () | |
| Destructor. | |
Singleton<T> owns one T for the process. instance() creates that object on the first call and returns the same object on every later call. The instance is a function-local static created by create(); it lives until the program exits.
Derive from Singleton<T> with T equal to the derived class, and befriend the base so it can construct T:
The derived constructor stays protected so callers cannot build a second instance. instance() is the only public way to get the object. Singleton is NonCopyable, so the instance cannot be copied either.
Construction is not synchronized. The first call to instance() must happen before other threads call it, or the program must otherwise guarantee a single initializing call. There is no destructor hook and no way to replace the instance. Use this type for a process-wide service that is created on demand, not for objects whose lifetime the caller must control.