Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
Inkscape::Util::StaticsBin Class Reference

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 StaticsBinget ()
 

Private Member Functions

 ~StaticsBin ()
 

Private Attributes

StaticHolderBasehead = nullptr
 

Friends

class StaticHolderBase
 

Detailed Description

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.

  • To destruct all singletons at any time, call
    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.

Definition at line 60 of file statics.h.

Constructor & Destructor Documentation

◆ ~StaticsBin()

Inkscape::Util::StaticsBin::~StaticsBin ( )
private

Definition at line 21 of file statics.cpp.

References head.

Member Function Documentation

◆ destroy()

void Inkscape::Util::StaticsBin::destroy ( )

Definition at line 14 of file statics.cpp.

References head.

Referenced by main().

◆ get()

StaticsBin & Inkscape::Util::StaticsBin::get ( )
static

Definition at line 8 of file statics.cpp.

Referenced by main().

Friends And Related Symbol Documentation

◆ StaticHolderBase

friend class StaticHolderBase
friend

Definition at line 72 of file statics.h.

Member Data Documentation

◆ head

StaticHolderBase* Inkscape::Util::StaticsBin::head = nullptr
private

Definition at line 70 of file statics.h.

Referenced by destroy(), and ~StaticsBin().


The documentation for this class was generated from the following files: