| 1 | /**
|
|---|
| 2 | * @file factory.hpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Tue Apr 5 20:40:41 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Factory class that holds commands and arbitrary objects.
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #ifndef FACTORY_HPP_
|
|---|
| 12 | #define FACTORY_HPP_
|
|---|
| 13 |
|
|---|
| 14 | #include <map>
|
|---|
| 15 | #include <string>
|
|---|
| 16 |
|
|---|
| 17 | #include "base/object.hpp"
|
|---|
| 18 |
|
|---|
| 19 | namespace VMG
|
|---|
| 20 | {
|
|---|
| 21 |
|
|---|
| 22 | class Factory
|
|---|
| 23 | {
|
|---|
| 24 | public:
|
|---|
| 25 | Factory();
|
|---|
| 26 | virtual ~Factory();
|
|---|
| 27 |
|
|---|
| 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);
|
|---|
| 31 |
|
|---|
| 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);
|
|---|
| 35 |
|
|---|
| 36 | void Delete(std::string id); ///< Deletes an object
|
|---|
| 37 |
|
|---|
| 38 | void PrintAvailableObjects(); ///< Prints the name of all objects that have been registered to the factory.
|
|---|
| 39 |
|
|---|
| 40 | private:
|
|---|
| 41 | std::map<std::string, Object*> object_map;
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | template <class T>
|
|---|
| 45 | T& Factory::RegisterObjectStorage(std::string id, const T& val)
|
|---|
| 46 | {
|
|---|
| 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();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | template <class T>
|
|---|
| 59 | T& Factory::GetObjectStorageVal(std::string id)
|
|---|
| 60 | {
|
|---|
| 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();
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | #endif /* FACTORY_HPP_ */
|
|---|