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
{
SomeClass();
public:
};
int main(
int argc,
char ** argv)
{
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.