Changeset 10624 for code/trunk/src/libraries/util/Singleton.h
- Timestamp:
- Oct 4, 2015, 9:12:21 PM (10 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
-
code/trunk/src/libraries/util/Singleton.h
r8858 r10624 52 52 public: 53 53 TestSingleton(); // public constructor because we may want to manage this singleton 54 // with an orxonox::ScopedSingleton Manager (see below)54 // with an orxonox::ScopedSingletonWrapper 55 55 virtual ~TestSingleton(); // public destructor 56 56 … … 68 68 TestSingleton* TestSingleton::singletonPtr_s = NULL; 69 69 @endcode 70 71 Usually a singleton gets created automatically when it is first used, but it will never72 be destroyed (unless the singleton explicitly deletes itself). To allow controlled73 construction and destruction, the singleton can be put within a virtual scope. This is74 done by registering the singleton class with orxonox::ScopedSingletonManager. To75 do so, the ManageScopedSingleton() macro has to be called:76 77 @code78 ManageScopedSingleton(TestSingleton, ScopeID::Graphics, false); // muste be called in a source (*.cc) file79 @endcode80 81 @b Important: If you call ManageScopedSingleton(), you don't have to initialize singletonPtr_s anymore,82 because that's already done by the macro.83 84 Now the singleton TestSingleton gets automatically created if the scope Graphics becomes85 active and also gets destroyed if the scope is deactivated.86 87 Note that not all singletons must register with a scope, but it's recommended.88 70 89 71 If a class inherits from orxonox::Singleton, it also inherits its functions. The most important … … 112 94 #include "UtilPrereqs.h" 113 95 114 #include <cassert>115 96 #include <cstring> 97 #include <typeinfo> 98 99 #include "OrxAssert.h" 116 100 117 101 namespace orxonox … … 134 118 static T& getInstance() 135 119 { 136 assert(T::singletonPtr_s != NULL);120 OrxVerify(T::singletonPtr_s != NULL, "T=" << typeid(T).name()); 137 121 return *T::singletonPtr_s; 138 122 } … … 144 128 } 145 129 146 //! Update method called by ClassSingletonManager (if used)147 void preUpdateSingleton(const Clock& time) { static_cast<T*>(T::singletonPtr_s)->preUpdate(time); }148 //! Empty update method for the static polymorphism149 void preUpdate(const Clock& time) { }150 //! Update method called by ClassSingletonManager (if used)151 void postUpdateSingleton(const Clock& time) { static_cast<T*>(T::singletonPtr_s)->postUpdate(time); }152 //! Empty update method for the static polymorphism153 void postUpdate(const Clock& time) { }154 155 130 protected: 156 131 //! Constructor sets the singleton instance pointer 157 132 Singleton() 158 133 { 159 assert(T::singletonPtr_s == NULL);134 OrxVerify(T::singletonPtr_s == NULL, "T=" << typeid(T).name()); 160 135 T::singletonPtr_s = static_cast<T*>(this); 161 136 } … … 164 139 virtual ~Singleton() 165 140 { 166 assert(T::singletonPtr_s != NULL);141 OrxVerify(T::singletonPtr_s != NULL, "T=" << typeid(T).name()); 167 142 T::singletonPtr_s = NULL; 168 143 }
Note: See TracChangeset
for help on using the changeset viewer.