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 >:
Inheritance graph
[legend]

List of all members.


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:
      using senf::singleton<SomeClass>::instance;

      // ...
  };

  int main(int argc, char ** argv)
  {
      // At this point, the instance has already been constructed

      SomeClass::instance().doSomething();
  }
Warning:
The singleton class should not have any static data members since it cannot be guaranteed, that these members will be constructed before the singleton instance.
Implementation note:
This implementation is directly taken from boost/pool/detail/singleton.hpp. 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 87 of file singleton.hh.


Classes

struct   force_creation
  Internal.

Protected Member Functions

  singleton ()
  ~singleton ()

Static Protected Member Functions

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

Constructor & Destructor Documentation

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

Definition at line 34 of file singleton.cti.

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

Definition at line 40 of file singleton.cti.


Member Function Documentation

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

Return true, if instance ok, false otherwise.

Definition at line 58 of file singleton.cti.

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

Return singleton instance.

Definition at line 49 of file singleton.cti.


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