[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 | /**
|
---|
[132] | 21 | * singleton
|
---|
| 22 | * @brief Represents an accessible static object that will only have one instance.
|
---|
[110] | 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 | /**
|
---|
[132] | 33 | * Creates the single instance if one doesn't already exist.
|
---|
[110] | 34 | */
|
---|
[3] | 35 | singleton()
|
---|
| 36 | {
|
---|
| 37 | assert(!singleton_);
|
---|
| 38 | singleton_ = static_cast<T*>(this);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[110] | 41 | /**
|
---|
[132] | 42 | * Destroys the instance.
|
---|
[110] | 43 | */
|
---|
[3] | 44 | ~singleton()
|
---|
| 45 | {
|
---|
| 46 | assert(singleton_);
|
---|
| 47 | singleton_ = 0;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public:
|
---|
[110] | 51 | /**
|
---|
[132] | 52 | * Checks to see if the instance exists.
|
---|
[110] | 53 | *
|
---|
[132] | 54 | * @returns True if this singleton has an instance assigned, false otherwise.
|
---|
[110] | 55 | */
|
---|
[3] | 56 | static bool is_valid()
|
---|
| 57 | {
|
---|
| 58 | return singleton_ != 0;
|
---|
| 59 | }
|
---|
[110] | 60 |
|
---|
| 61 | /**
|
---|
[132] | 62 | * Returns the pointer to the instance.
|
---|
[110] | 63 | *
|
---|
[132] | 64 | * @returns The pointer to the instance.
|
---|
[110] | 65 | */
|
---|
[3] | 66 | static T *pointer()
|
---|
| 67 | {
|
---|
| 68 | assert(singleton_);
|
---|
| 69 | return singleton_;
|
---|
| 70 | }
|
---|
[110] | 71 | /**
|
---|
[132] | 72 | * Returns the object referenced by this singleton.
|
---|
[110] | 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 | /**
|
---|
[132] | 86 | * auto_singleton
|
---|
| 87 | * @brief Represents a singleton that automatically creates an instance if one doesn't already exist.
|
---|
[110] | 88 | */
|
---|
[3] | 89 | template <class T>
|
---|
| 90 | class auto_singleton : public singleton<T>
|
---|
| 91 | {
|
---|
| 92 | public:
|
---|
[110] | 93 | /**
|
---|
[132] | 94 | * Returns the pointer to the instance. Makes an instance if one doesn't already exist.
|
---|
[110] | 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 | /**
|
---|
[132] | 106 | * Returns the object referenced by this singleton. Makes an instance if one doesn't already exist.
|
---|
[110] | 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
|
---|