Files
simgear/simgear/structure/Singleton.hxx
T
Mathias Froehlich 80274596df Use simgear internal stuff for the singleton class.
This removes the other still present dependency on osg
in the future sgcore library.
It also remove the need for the NO_OPENSCENEGRAPH_INTERFACE
compile time definition.
2012-03-03 14:39:39 +01:00

60 lines
1.1 KiB
C++

#ifndef SIMGEAR_SINGLETON_HXX
#define SIMGEAR_SINGLETON_HXX 1
#include "singleton.hpp"
#include "SGReferenced.hxx"
#include "SGSharedPtr.hxx"
namespace simgear
{
/**
* Class that supplies the address of a singleton instance. This class
* can be inherited by its Class argument in order to support the
* instance() method in that class.
*/
template <typename Class>
class Singleton
{
protected:
Singleton() {}
public:
static Class* instance()
{
Class& singleton
= boost::details::pool::singleton_default<Class>::instance();
return &singleton;
}
};
template <typename RefClass>
class SingletonRefPtr
{
public:
SingletonRefPtr()
{
ptr = new RefClass;
}
static RefClass* instance()
{
SingletonRefPtr& singleton
= boost::details::pool::singleton_default<SingletonRefPtr>::instance();
return singleton.ptr.get();
}
private:
SGSharedPtr<RefClass> ptr;
};
template <typename RefClass>
class ReferencedSingleton : public virtual SGReferenced
{
public:
static RefClass* instance()
{
return SingletonRefPtr<RefClass>::instance();
}
};
}
#endif