Inkscape
Vector Graphics Editor
|
The following system provides a way of dealing with statics/singletons with unusual lifetime requirements, specifically the requirement that they be destroyed before the end of main(). More...
#include <statics.h>
Public Member Functions | |
void | destroy () |
Static Public Member Functions | |
static StaticsBin & | get () |
Private Member Functions | |
~StaticsBin () | |
Private Attributes | |
StaticHolderBase * | head = nullptr |
Friends | |
class | StaticHolderBase |
The following system provides a way of dealing with statics/singletons with unusual lifetime requirements, specifically the requirement that they be destroyed before the end of main().
This isn't guaranteed by the usual static initialisation idiom
X &get() { static X x; return x; }
because X will be destroyed just after main() exits. And sometimes that's a deal-breaker!
To use the system with a singleton class X, derive it from EnableSingleton<X>:
class X : public EnableSingleton<X> { ...
This endows it with a get() method that initialises and returns the static instance.
Warning: get() is not safe against concurrent initialisation, unlike the idiom above. So only use it in single-threaded code.
To ensure that X is outlived by another singleton Y, pass in the dependency using Depends:
class X : public EnableSingleton<X, Depends<Y>> { ...
Multiple dependencies can be specified. Then Y will be destructed after X.
Note: Y will still be lazily-initialised, for startup efficiency. So X's lifetime isn't necessarily completely contained in Y's lifetime.
Note: As with the above idiom, dependency loops are detected at runtime on glibc.
StaticsBin::get().destroy();They will be recreated again if re-accessed. This function should be called at the end of main(). If it isn't, it will be detected at runtime by an assertion in StaticsBin::~StaticsBin(). Maintains the list of statics that need to be destroyed, destroys them, and complains if it's not asked to do so in time.
|
private |
Definition at line 21 of file statics.cpp.
References head.
void Inkscape::Util::StaticsBin::destroy | ( | ) |
|
static |
Definition at line 8 of file statics.cpp.
Referenced by main().
|
friend |
|
private |
Definition at line 70 of file statics.h.
Referenced by destroy(), and ~StaticsBin().