- Timestamp:
- Sep 2, 2010, 3:16:08 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/doc/src/libraries/util/ScopedSingletonManager.h
r7284 r7323 26 26 * 27 27 */ 28 29 /** 30 @file 31 @ingroup Util SingletonScope 32 @brief Definition of orxonox::ScopedSingletonManager, orxonox::ClassScopedSingletonManager, and the ManageScopedSingleton macro. 33 34 ScopedSingletonManager is used to create and destroy Singletons that belong to 35 a given Scope. For each one of these singletons, the macro ManageScopedSingleton() 36 has to be called to register the singleton with orxonox::ScopedSingletonManager. 37 38 See @ref SingletonExample "this code" for an example. 39 40 @see orxonox::Singleton 41 @see orxonox::Scope 42 */ 28 43 29 44 #ifndef __ScopedSingletonManager_H__ … … 38 53 #include "util/Singleton.h" 39 54 55 /** 56 @brief Registers an orxonox::Singleton with orxonox::ScopedSingletonManager. 57 @param className The name of the singleton class 58 @param scope The scope in which the singleton should exist 59 @param allowedToFail If true, the singleton is allowed to fail and thus a try-catch block is used when creating the singleton. 60 61 If this macro is called for a singleton, it is registered with ScopedSingletonManager 62 and will thus be created if its scope becomes active and destroyed if is deactivated. 63 */ 40 64 #define ManageScopedSingleton(className, scope, allowedToFail) \ 41 65 className* className::singletonPtr_s = NULL; \ … … 46 70 class OrxonoxClass; 47 71 72 /** 73 @brief Base class of ClassScopedSingletonManager, implements some static functions 74 used to dispatch calls to preUpdate and postUpdate to all instances of this class. 75 It also keeps track of all existing ScopedSingletonManagers and stores them in a 76 map, sorted by the scope they belong to. 77 */ 48 78 class _UtilExport ScopedSingletonManager 49 79 { 50 80 public: 81 /// Constructor: Initializes all the values 51 82 ScopedSingletonManager(const std::string& className, ScopeID::Value scope) 52 83 : className_(className) … … 54 85 { } 55 86 virtual ~ScopedSingletonManager() { } 87 88 /// Adds a new instance of ScopedSingletonManager to the map. 56 89 static void addManager(ScopedSingletonManager* manager); 57 90 91 /// Calls preUpdate in all instances of ScopedSingletonManager that are registered in the map. 58 92 template<ScopeID::Value scope> 59 93 static void preUpdate(const Clock& time) … … 64 98 } 65 99 virtual void preUpdate(const Clock& time) = 0; 100 101 /// Calls postUpdate in all instances of ScopedSingletonManager that are registered in the map. 66 102 template<ScopeID::Value scope> 67 103 static void postUpdate(const Clock& time) … … 78 114 79 115 protected: 80 const std::string className_; 81 const ScopeID::Value scope_; 116 const std::string className_; ///< The name of the scoped singleton class that is managed by this object 117 const ScopeID::Value scope_; ///< The scope of the singleton that is managed by this object 82 118 }; 83 119 120 /** 121 @anchor ClassScopedSingletonManager 122 123 @brief Manages a scoped singleton for a given scope. 124 @param T The managed singleton class 125 @param scope The scope in which the singleton @a T should be active 126 @param allowedToFail If true, a specialization of this template is used, that uses try-catch blocks to handle possible failures. 127 128 This class inherits from ScopeListener for the given scope and thus its functions 129 activated() and deactivated() are called whenever the Scope changes its state. 130 131 If the Scope is activated, a new instance of @a T (which must be a singleton) is created. 132 If the Scope is deactivated, the singleton is destroyed. 133 134 @see Singleton 135 */ 84 136 template <class T, ScopeID::Value scope, bool allowedToFail> 85 137 class ClassScopedSingletonManager : public ScopedSingletonManager, public ScopeListener 86 138 { 87 139 public: 140 //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonManager and ScopeListener 88 141 ClassScopedSingletonManager(const std::string& className) 89 142 : ScopedSingletonManager(className, scope) … … 113 166 } 114 167 168 //! Destroys the singleton instance - overloaded for OrxonoxClass, calls OrxonoxClass::destroy() 115 169 void destroy(OrxonoxClass*) 116 170 { 117 171 singletonPtr_->destroy(); 118 172 } 173 //! Destroys the singleton instance - overloaded for all other pointers, calls delete 119 174 void destroy(void*) 120 175 { … … 139 194 140 195 private: 141 T* singletonPtr_; 196 T* singletonPtr_; ///< Unique instance of the singleton class @a T 142 197 }; 143 198 199 /** 200 @brief This class partially spezializes ClassScopedSingletonManager for classes @a T that are allowed to fail. 201 @param T The managed singleton class 202 @param scope The scope in which the singleton @a T should be active 203 204 Because @a T could fail when being created, this partial spezialization of ClassScopedSingletonManager 205 uses a try-catch block to handle exceptions. 206 207 See @ref ClassScopedSingletonManager for a full documentation of the basis template. 208 */ 144 209 template <class T, ScopeID::Value scope> 145 210 class ClassScopedSingletonManager<T, scope, true> : public ScopedSingletonManager, public ScopeListener 146 211 { 147 212 public: 213 //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonManager and ScopeListener 148 214 ClassScopedSingletonManager(const std::string& className) 149 215 : ScopedSingletonManager(className, scope) … … 180 246 } 181 247 248 //! Destroys the singleton instance - overloaded for OrxonoxClass, calls OrxonoxClass::destroy() 182 249 void destroy(OrxonoxClass* ptr) 183 250 { 184 251 singletonPtr_->destroy(); 185 252 } 253 //! Destroys the singleton instance - overloaded for void*, calls delete 186 254 void destroy(void* ptr) 187 255 { … … 208 276 209 277 private: 210 T* singletonPtr_; 278 T* singletonPtr_; ///< Unique instance of the singleton class @a T 211 279 }; 212 280 }
Note: See TracChangeset
for help on using the changeset viewer.