senf::singleton< Self > Class Template Reference

Mark a class as singleton and provide singleton accessor. More...

#include <senf/Utils/singleton.hh>

Inheritance diagram for senf::singleton< Self >:

Protected Member Functions

 singleton ()
 
 ~singleton ()
 

Static Protected Member Functions

static Self & instance ()
 Return singleton instance. More...
 
static bool alive ()
 Return true, if instance ok, false otherwise. More...
 

Detailed Description

template<class Self>
class senf::singleton< Self >

Mark a class as singleton and provide singleton accessor.

This mixin class will mark a class as singleton and provide an accessor function to access this singleton instance. The following preconditions must be met for this class to work as advertised:

  • There must be only a single thread executing before main() starts. (This should always be the case)
  • There must be only a single thread executing after main() ends. (This is always important, otherwise global object destruction might fail)
  • The singleton class must have a non throwing default constructor and destructor

If these conditions are met, this mixin will ensure that the singleton is constructed before main even starts executing. If static construction code calls the instance() member, it is ensured, that the instance is constructed no later than the first call to instance().

Usage example:

class SomeClass
: public senf::singleton<SomeClass>
{
// Must have default constructor
SomeClass();
// Give singleton access to the constructor
friend class senf::singleton<SomeClass>;
public:
// By default 'instance()' is protected. If you want, you may make it public:
// ...
};
int main(int argc, char ** argv)
{
// At this point, the instance has already been constructed
SomeClass::instance().doSomething();
}
    \warning The singleton class should \e not have any static data members since it cannot be
        guaranteed, that these members will be constructed before the singleton instance.

    \par "Implementation note:" This implementation is directly taken from
        <tt>boost/pool/detail/singleton.hpp</tt>. See that file for a description of the
        technique. The only difference is, that I prefer to advertise this class as a mixin
        (though it may be used the same way as the original too).

Definition at line 78 of file singleton.hh.

Constructor & Destructor Documentation

◆ singleton()

template<class Self >
senf::singleton< Self >::singleton ( )
protected

◆ ~singleton()

template<class Self >
senf::singleton< Self >::~singleton ( )
protected

Member Function Documentation

◆ alive()

template<class Self >
static bool senf::singleton< Self >::alive ( )
staticprotected

Return true, if instance ok, false otherwise.

◆ instance()

template<class Self >
static Self& senf::singleton< Self >::instance ( )
staticprotected

Return singleton instance.


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