Rev | Line | |
---|
[22] | 1 | /** |
---|
| 2 | * @file Factory.h |
---|
| 3 | * Defines the Factory class template |
---|
| 4 | * @author Alexandre Berge |
---|
| 5 | * @version 1.0 |
---|
| 6 | * @date 24/07/2009 |
---|
| 7 | */ |
---|
| 8 | #ifndef FACTORY_H |
---|
| 9 | #define FACTORY_H |
---|
| 10 | |
---|
| 11 | #include <map> |
---|
| 12 | #include <string> |
---|
| 13 | |
---|
| 14 | template <class Object, class Key=std::string> class Factory { |
---|
| 15 | |
---|
| 16 | public: |
---|
| 17 | void Register(Key key, Object *obj) { |
---|
| 18 | if(m_map.find(key) == m_map.end()) { |
---|
| 19 | m_map[key] = obj; |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | Object * Create(const Key& key) { |
---|
| 24 | Object * tmp = 0; |
---|
| 25 | typename std::map<Key, Object*>::iterator it = m_map.find(key); |
---|
| 26 | |
---|
| 27 | if(it != m_map.end()) |
---|
| 28 | tmp = ((*it).second)->Clone(); |
---|
| 29 | |
---|
| 30 | return tmp; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | private: |
---|
| 34 | std::map<Key, Object *> m_map; |
---|
| 35 | |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | template <class Object, class Key=std::string> class SingletonFactory { |
---|
| 39 | |
---|
| 40 | public: |
---|
| 41 | void Register(Key key, Object *obj) { |
---|
| 42 | if(m_map.find(key) == m_map.end()) { |
---|
| 43 | m_map[key] = obj; |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | Object * Get(const Key& key) { |
---|
| 48 | Object * tmp = 0; |
---|
| 49 | typename std::map<Key, Object*>::iterator it = m_map.find(key); |
---|
| 50 | |
---|
| 51 | if(it != m_map.end()) |
---|
| 52 | tmp = (*it).second; |
---|
| 53 | |
---|
| 54 | return tmp; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | private: |
---|
| 58 | std::map<Key, Object *> m_map; |
---|
| 59 | |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.