Changeset dfed1c for src/base/factory.hpp
- Timestamp:
- Nov 22, 2011, 9:22:10 PM (14 years ago)
- Children:
- facba0
- Parents:
- 66f24d
- File:
-
- 1 edited
-
src/base/factory.hpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/base/factory.hpp
r66f24d rdfed1c 3 3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de> 4 4 * @date Tue Apr 5 20:40:41 2011 5 * 5 * 6 6 * @brief Factory class that holds commands and arbitrary objects. 7 * 8 * 7 * 8 * 9 9 */ 10 10 11 #ifndef COMMAND_FACTORY_HPP_12 #define COMMAND_FACTORY_HPP_11 #ifndef FACTORY_HPP_ 12 #define FACTORY_HPP_ 13 13 14 #include <iostream>15 14 #include <map> 16 15 #include <string> 17 16 18 17 #include "base/object.hpp" 19 #include "base/proxy.hpp"20 18 21 19 namespace VMG 22 20 { 23 21 24 class Object;25 26 22 class Factory 27 23 { 28 24 public: 29 typedef std::map<std::string, CommandProxyBase*>::iterator command_iterator;30 typedef std::map<std::string, Command*>::iterator instance_iterator;31 typedef std::map<std::string, Object*>::iterator object_iterator;32 33 25 Factory(); 34 26 virtual ~Factory(); 35 27 36 void Destroy(); 28 void Register(Object* object); ///< Registers an object 29 template <class T> T& RegisterObjectStorage(std::string id, const T& val); 30 template <class T> T* RegisterObjectStorageArray(std::string id, const vmg_int& size); 37 31 38 void RegisterCommand(CommandProxyBase* command); ///< Register a command 39 void RegisterObject(Object* object); ///< Register an object 32 Object* Get(std::string id); ///< Returns an object. 33 template <class T> T& GetObjectStorageVal(std::string id); 34 template <class T> T* GetObjectStorageArray(std::string id); 40 35 41 /** 42 * Register an arbitrary object that is not derived from Object. 43 * Similar functionality can be achieved by calling 44 * "new ObjectStorage<T>(id, data);" 45 */ 46 template <class T> void RegisterObjectStorage(std::string id, const T& data); 47 template <class T> T& GetObjectStorageVal(std::string id); 36 void Delete(std::string id); ///< Deletes an object 48 37 49 void DeleteCommand(std::string id); ///< Delete a command50 void DeleteObject(std::string id); ///< Delete an object51 52 Command* GetCommand(std::string id); ///< Returns a command.53 Object* GetObject(std::string id); ///< Returns an object.54 55 /**56 * Checks for the correct number of arguments. This check is57 * automatically performed, whenever a command is executed from58 * a command list (if the library has not been compiled with NDEBUG).59 */60 bool CheckNumberOfArguments(std::string id, const int& num_arguments);61 62 void PrintAvailableCommands(); ///< Prints the name of all commands that have been registered to the factory.63 38 void PrintAvailableObjects(); ///< Prints the name of all objects that have been registered to the factory. 64 39 65 40 private: 66 std::map<std::string, CommandProxyBase*> command_map;67 std::map<std::string, Command*> command_instance_map;68 41 std::map<std::string, Object*> object_map; 69 42 }; 70 43 71 44 template <class T> 72 void Factory::RegisterObjectStorage(std::string id, const T& data)45 T& Factory::RegisterObjectStorage(std::string id, const T& val) 73 46 { 74 new ObjectStorage<T>(id, data); 47 Object* object = new ObjectStorage<T>(id, val); 48 return object->Cast< ObjectStorage<T> >()->Val(); 49 } 50 51 template <class T> 52 T* Factory::RegisterObjectStorageArray(std::string id, const vmg_int& size) 53 { 54 Object* object = new ObjectStorageArray<T>(id, size); 55 return object->Cast< ObjectStorage<T*> >()->Val(); 75 56 } 76 57 … … 78 59 T& Factory::GetObjectStorageVal(std::string id) 79 60 { 80 return GetObject(id)->Cast< ObjectStorage<T> >()->Val(); 61 return Get(id)->Cast< ObjectStorage<T> >()->Val(); 62 } 63 64 template <class T> 65 T* Factory::GetObjectStorageArray(std::string id) 66 { 67 return Get(id)->Cast< ObjectStorage<T*> >()->Val(); 81 68 } 82 69 83 70 } 84 71 85 #endif /* COMMAND_FACTORY_HPP_ */72 #endif /* FACTORY_HPP_ */
Note:
See TracChangeset
for help on using the changeset viewer.
