sftools  2.0 dev
Bunch of tools for SFML application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Static Public Member Functions | List of all members
sftools::Singleton< T, F > Class Template Reference

Non intruisive and customizable singleton system. More...

#include <Singleton.hpp>

+ Collaboration diagram for sftools::Singleton< T, F >:

Static Public Member Functions

static T & getInstance ()
 Get the unique instance.
 
static void create (bool force=false, F const &factory=F())
 Initialise the instance.
 
static void destroy ()
 Destroy the instance if it exists.
 
static bool exists ()
 Tells if the instance was created.
 

Detailed Description

template<typename T, typename F = priv::DefaultNew<T>>
class sftools::Singleton< T, F >

Non intruisive and customizable singleton system.

Basic usage example :

typedef sftools::Singleton<MyClass> MySingleton;
MyClass& mySingleton = MySingleton::getInstance();
mySingleton.foo();
// Or
MySingleton::getInstance().foo();

Usually you don't have to specify a type for F; the default type should be enough. However if T can't be instanciated with new T(); (e.g. it need some parameters) you can create a factory type that :

Sometimes your might want to use a factory with custom parameters (i.e. using the default constructor for the factory doesn't fit your plan). In that case you can do as in the following example

#include <iostream>
// Class to be singletonized
class Foo
{
int const m_bar;
public:
// No default constructor here
Foo(int bar) : m_bar(bar) { }
void print() const { std::cout << "Foo:" << m_bar << std::endl; }
};
// Factory
class FooFactory
{
int const m_param;
public:
// We need a default constructor
FooFactory() : m_param(0) { }
// But we can also use a customized constructor and pass a factory
// object to Singleton::create
FooFactory(int param) : m_param(param) { }
// And we need a factory method : operator()
Foo* operator()() const { return new Foo(m_param); }
};
void f()
{
// Initialize our singleton with a custom factory
FooFactory factory(58);
SingletonFoo::create(false, factory);
SingletonFoo::getInstance().print(); // prints: "Foo:58"
}
Todo:
With C++11 we could do even better : by using variadic template we can customize more easily the construction of the unique instance and probably even inherit from T so the singleton object could be used everywhere T is used. However there might be issues with NonCopyable and NonInstanceable.
Template Parameters
TType to be "singletonized"
FFactory type used to create an instance of T.

Definition at line 128 of file Singleton/Singleton.hpp.

Member Function Documentation

template<typename T , typename F >
void sftools::Singleton< T, F >::create ( bool  force = false,
F const &  factory = F() 
)
static

Initialise the instance.

F is used to generate a object of type T.

Parameters
forceif force is true then the instance is destroyed and recreated; otherwise the instance is only created if it doesn't exist yet.
factoryfactory object used to create the instance.
See Also
destroy
exists

Definition at line 45 of file Singleton.tpp.

template<typename T , typename F >
void sftools::Singleton< T, F >::destroy ( )
static

Destroy the instance if it exists.

See Also
exists

Definition at line 59 of file Singleton.tpp.

template<typename T , typename F >
bool sftools::Singleton< T, F >::exists ( )
static

Tells if the instance was created.

See Also
create

Definition at line 69 of file Singleton.tpp.

template<typename T , typename F >
T & sftools::Singleton< T, F >::getInstance ( )
static

Get the unique instance.

Calls create (with its default parameters) if the instance doesn't exist yet.

See Also
create

Definition at line 34 of file Singleton.tpp.

Referenced by sftools::loader::ResourceLoader< R >::operator()().


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