]> git.mxchange.org Git - simgear.git/blob - simgear/structure/Singleton.hxx
Merge branch 'topic/gcintersect' into next
[simgear.git] / simgear / structure / Singleton.hxx
1 #ifndef SIMGEAR_SINGLETON_HXX
2 #define SIMGEAR_SINGLETON_HXX 1
3
4 #include <boost/pool/detail/singleton.hpp>
5
6 #include <osg/Referenced>
7 #include <osg/ref_ptr>
8
9 namespace simgear
10 {
11 /**
12  * Class that supplies the address of a singleton instance. This class
13  * can be inherited by its Class argument in order to support the
14  * instance() method in that class.
15  */
16 template <typename Class>
17 class Singleton
18 {
19 protected:
20     Singleton() {}
21 public:
22     static Class* instance()
23     {
24         Class& singleton
25             = boost::details::pool::singleton_default<Class>::instance();
26         return &singleton;
27     }
28 };
29
30 template <typename RefClass>
31 class SingletonRefPtr
32 {
33 public:
34     SingletonRefPtr()
35     {
36         ptr = new RefClass;
37     }
38     static RefClass* instance()
39     {
40         SingletonRefPtr& singleton
41             = boost::details::pool::singleton_default<SingletonRefPtr>::instance();
42         return singleton.ptr.get();
43     }
44 private:
45     osg::ref_ptr<RefClass> ptr;
46 };
47
48 template <typename RefClass>
49 class ReferencedSingleton : public virtual osg::Referenced
50 {
51 public:
52     static RefClass* instance()
53     {
54         return SingletonRefPtr<RefClass>::instance();
55     }
56 };
57 }
58 #endif