[3] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * @file singleton.hh
|
---|
| 9 | * @author Kornel Kisielewicz epyon@chaosforge.org
|
---|
| 10 | * @brief singleton pattern
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #ifndef NV_SINGLETON_HH
|
---|
| 14 | #define NV_SINGLETON_HH
|
---|
| 15 |
|
---|
| 16 | #include <cassert>
|
---|
| 17 |
|
---|
| 18 | namespace nv
|
---|
| 19 | {
|
---|
[110] | 20 | /**
|
---|
| 21 | *singleton
|
---|
| 22 | *@brief Represents an accessible static object that will only have one instance.
|
---|
| 23 | */
|
---|
[3] | 24 | template <class T>
|
---|
| 25 | class singleton
|
---|
| 26 | {
|
---|
| 27 | private:
|
---|
[110] | 28 | static T *singleton_; ///< Pointer to the instance of this object type.
|
---|
[3] | 29 |
|
---|
| 30 | protected:
|
---|
[110] | 31 |
|
---|
| 32 | /**
|
---|
| 33 | *Creates the single instance if one doesn't already exist.
|
---|
| 34 | */
|
---|
[3] | 35 | singleton()
|
---|
| 36 | {
|
---|
| 37 | assert(!singleton_);
|
---|
| 38 | singleton_ = static_cast<T*>(this);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[110] | 41 | /**
|
---|
| 42 | *Destroys the instance.
|
---|
| 43 | */
|
---|
[3] | 44 | ~singleton()
|
---|
| 45 | {
|
---|
| 46 | assert(singleton_);
|
---|
| 47 | singleton_ = 0;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public:
|
---|
[110] | 51 | /**
|
---|
| 52 | *Checks to see if the instance exists.
|
---|
| 53 | *
|
---|
| 54 | *@returns True if this singleton has an instance assigned, false otherwise.
|
---|
| 55 | */
|
---|
[3] | 56 | static bool is_valid()
|
---|
| 57 | {
|
---|
| 58 | return singleton_ != 0;
|
---|
| 59 | }
|
---|
[110] | 60 |
|
---|
| 61 | /**
|
---|
| 62 | *Returns the pointer to the instance.
|
---|
| 63 | *
|
---|
| 64 | *@returns The pointer to the instance.
|
---|
| 65 | */
|
---|
[3] | 66 | static T *pointer()
|
---|
| 67 | {
|
---|
| 68 | assert(singleton_);
|
---|
| 69 | return singleton_;
|
---|
| 70 | }
|
---|
[110] | 71 | /**
|
---|
| 72 | *Returns the object referenced by this singleton.
|
---|
| 73 | */
|
---|
[3] | 74 | static T &reference()
|
---|
| 75 | {
|
---|
| 76 | assert(singleton_);
|
---|
| 77 | return *singleton_;
|
---|
| 78 | }
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | template <class T>
|
---|
| 82 | T* singleton<T>::singleton_ = 0;
|
---|
| 83 |
|
---|
[110] | 84 |
|
---|
| 85 | /**
|
---|
| 86 | *auto_singleton
|
---|
| 87 | *@brief Represents a singleton that automatically creates an instance if one doesn't already exist.
|
---|
| 88 | */
|
---|
[3] | 89 | template <class T>
|
---|
| 90 | class auto_singleton : public singleton<T>
|
---|
| 91 | {
|
---|
| 92 | public:
|
---|
[110] | 93 | /**
|
---|
| 94 | *Returns the pointer to the instance. Makes an instance if one doesn't already exist.
|
---|
| 95 | */
|
---|
[3] | 96 | static T *pointer()
|
---|
| 97 | {
|
---|
| 98 | if ( !singleton<T>::is_valid() )
|
---|
| 99 | {
|
---|
| 100 | new T();
|
---|
| 101 | }
|
---|
| 102 | return singleton<T>::pointer();
|
---|
| 103 | }
|
---|
[110] | 104 |
|
---|
| 105 | /**
|
---|
| 106 | *Returns the object referenced by this singleton. Makes an instance if one doesn't already exist.
|
---|
| 107 | */
|
---|
[3] | 108 | static T &reference()
|
---|
| 109 | {
|
---|
| 110 | if ( !singleton<T>::is_valid() )
|
---|
| 111 | {
|
---|
| 112 | new T();
|
---|
| 113 | }
|
---|
| 114 | return singleton<T>::reference();
|
---|
| 115 | }
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | } // namespace nv
|
---|
| 119 |
|
---|
| 120 | #endif // NV_SINGLETON_HH
|
---|