33 |
|
#define UTIL_GENERICFACTORY_HPP |
34 |
|
#include <map> |
35 |
|
#include <string> |
36 |
< |
|
37 |
< |
template<class Product, typename Creator, typename ProductIdentType = std::string> |
36 |
> |
namespace oopse { |
37 |
> |
template<class Product, typename ProductIdentType = std::string, typename Creator = Product* (*)() > |
38 |
|
class GenericFactory{ |
39 |
|
public: |
40 |
|
|
41 |
< |
typedef map<ProductIdentType, Creator*> CreatorMapType; |
42 |
< |
~GenericFactory(); |
41 |
> |
typedef std::map<ProductIdentType, Creator*> CreatorMapType; |
42 |
> |
typedef GenericFactory<Product, ProductIdentType, Creator> SelfType; |
43 |
> |
static SelfType* getInstance() { |
44 |
> |
if (instance_ == NULL) { |
45 |
> |
instance_ = new GenericFactory<Product, ProductIdentType, Creator>(); |
46 |
> |
} |
47 |
|
|
48 |
< |
static GenericFactory* getInstance(); |
48 |
> |
return instance_; |
49 |
> |
} |
50 |
> |
|
51 |
> |
//bool register( const ProductIdentType& id, Creator creator) { |
52 |
|
|
53 |
< |
bool register(CreatorType* creator); |
53 |
> |
//insert method in std::map will return a pair<iterator, bool>. the second |
54 |
> |
//element of this pair indicates whether the result of the operation |
55 |
> |
//return creatorMap_.insert(CreatorMapType::value_type(id, creator)).second; |
56 |
> |
//} |
57 |
|
|
58 |
< |
bool unregister(); |
58 |
> |
bool unregister(const ProductIdentType& id) { |
59 |
> |
|
60 |
> |
return creatorMap_->erase(id) == 1; |
61 |
> |
|
62 |
> |
} |
63 |
|
|
64 |
< |
bool hasCreator( const ProductIdentType& id ); |
64 |
> |
bool hasCreator( const ProductIdentType& id ) { |
65 |
> |
CreatorMapType::iterator i; |
66 |
|
|
67 |
< |
const std::string toString(); |
67 |
> |
i = creatorMap_.find(id); |
68 |
|
|
69 |
< |
Product* createProduct( const ProductIdentType& id ); |
69 |
> |
if (i != creatorMap_.end()) { |
70 |
> |
return true; |
71 |
> |
} else { |
72 |
> |
return false; |
73 |
> |
} |
74 |
> |
} |
75 |
> |
|
76 |
> |
//const std::string toString() { |
77 |
> |
|
78 |
|
|
79 |
+ |
//} |
80 |
+ |
|
81 |
+ |
Product* createProduct( const ProductIdentType& id ) { |
82 |
+ |
CreatorMapType::iterator i; |
83 |
+ |
|
84 |
+ |
i = creatorMap_.find(id); |
85 |
+ |
|
86 |
+ |
if (i != creatorMap_.end()) { |
87 |
+ |
//call the function to create the product |
88 |
+ |
return (i->second)(); |
89 |
+ |
} else { |
90 |
+ |
return NULL; |
91 |
+ |
} |
92 |
+ |
} |
93 |
+ |
|
94 |
|
private: |
95 |
|
GenericFactory(){} |
96 |
< |
static GenericFactory* instance_; |
97 |
< |
map<string, Creator*> creatorMap_; |
96 |
> |
static SelfType* instance_; |
97 |
> |
CreatorMapType creatorMap_; |
98 |
|
}; |
99 |
|
|
62 |
– |
typedef GenericFactory<ForceFieldParser, > ForceFiledParserFactory; |
63 |
– |
#endif |
100 |
|
|
101 |
+ |
#define REGISTER_CREATOR(factory, product, id) \ |
102 |
+ |
product * create##product() {\ |
103 |
+ |
return new product(); \ |
104 |
+ |
} |
105 |
+ |
|
106 |
+ |
}//namespace oopse |
107 |
+ |
#endif //UTIL_GENERICFACTORY_HPP |
108 |
+ |
|