00001 #ifndef SIMGEAR_SINGLETON_HXX
00002 #define SIMGEAR_SINGLETON_HXX 1
00003
00004 #include <boost/pool/detail/singleton.hpp>
00005
00006 #include <osg/Referenced>
00007 #include <osg/ref_ptr>
00008
00009 namespace simgear
00010 {
00016 template <typename Class>
00017 class Singleton
00018 {
00019 protected:
00020 Singleton() {}
00021 public:
00022 static Class* instance()
00023 {
00024 Class& singleton
00025 = boost::details::pool::singleton_default<Class>::instance();
00026 return &singleton;
00027 }
00028 };
00029
00030 template <typename RefClass>
00031 class SingletonRefPtr
00032 {
00033 public:
00034 SingletonRefPtr()
00035 {
00036 ptr = new RefClass;
00037 }
00038 static RefClass* instance()
00039 {
00040 SingletonRefPtr& singleton
00041 = boost::details::pool::singleton_default<SingletonRefPtr>::instance();
00042 return singleton.ptr.get();
00043 }
00044 private:
00045 osg::ref_ptr<RefClass> ptr;
00046 };
00047
00048 template <typename RefClass>
00049 class ReferencedSingleton : public virtual osg::Referenced
00050 {
00051 public:
00052 static RefClass* instance()
00053 {
00054 return SingletonRefPtr<RefClass>::instance();
00055 }
00056 };
00057 }
00058 #endif