| 1 |
/* |
| 2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
| 3 |
* |
| 4 |
* The University of Notre Dame grants you ("Licensee") a |
| 5 |
* non-exclusive, royalty free, license to use, modify and |
| 6 |
* redistribute this software in source and binary code form, provided |
| 7 |
* that the following conditions are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the |
| 15 |
* distribution. |
| 16 |
* |
| 17 |
* This software is provided "AS IS," without a warranty of any |
| 18 |
* kind. All express or implied conditions, representations and |
| 19 |
* warranties, including any implied warranty of merchantability, |
| 20 |
* fitness for a particular purpose or non-infringement, are hereby |
| 21 |
* excluded. The University of Notre Dame and its licensors shall not |
| 22 |
* be liable for any damages suffered by licensee as a result of |
| 23 |
* using, modifying or distributing the software or its |
| 24 |
* derivatives. In no event will the University of Notre Dame or its |
| 25 |
* licensors be liable for any lost revenue, profit or data, or for |
| 26 |
* direct, indirect, special, consequential, incidental or punitive |
| 27 |
* damages, however caused and regardless of the theory of liability, |
| 28 |
* arising out of the use of or inability to use software, even if the |
| 29 |
* University of Notre Dame has been advised of the possibility of |
| 30 |
* such damages. |
| 31 |
* |
| 32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
| 33 |
* research, please cite the appropriate papers when you publish your |
| 34 |
* work. Good starting points are: |
| 35 |
* |
| 36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
| 37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
| 38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
| 39 |
* [4] Vardeman & Gezelter, in progress (2009). |
| 40 |
*/ |
| 41 |
|
| 42 |
/** |
| 43 |
* @file GenericFactory.hpp |
| 44 |
* @author Teng Lin |
| 45 |
* @date 10/24/2004 |
| 46 |
* @version 1.0 |
| 47 |
*/ |
| 48 |
#ifndef UTIL_GENERICFACTORY_HPP |
| 49 |
#define UTIL_GENERICFACTORY_HPP |
| 50 |
#include <cassert> |
| 51 |
#include <map> |
| 52 |
#include <string> |
| 53 |
#include <vector> |
| 54 |
|
| 55 |
namespace OpenMD { |
| 56 |
|
| 57 |
/** |
| 58 |
* @class GenericFactory GenericFactory.hpp "utils/GenericFactory.hpp" |
| 59 |
* @brief GenericFactory is a template based Object Factory |
| 60 |
* Factory pattern is used to define an interface for creating an object. |
| 61 |
* |
| 62 |
* @param Object the base class of the hierarchy for which you provide the object factory. |
| 63 |
* @param IdentType the object that identifies the type of the concrete object. Default type is std::string * @param Creator the callable entity that creates objects. This type must support operator(), |
| 64 |
* taking no parameters and returning a pointer to Object. Default type is function pointer. |
| 65 |
* |
| 66 |
* Usage: |
| 67 |
* @code |
| 68 |
* //Shape class |
| 69 |
* class Shape { |
| 70 |
* ... |
| 71 |
* }; |
| 72 |
* |
| 73 |
* //instantiating a new object factory |
| 74 |
* typedef GenericFactory<Shape> ShapeFactory; |
| 75 |
* |
| 76 |
* //Line class |
| 77 |
* class Line : public Shape{ |
| 78 |
* ... |
| 79 |
* }; |
| 80 |
* |
| 81 |
* //declare function to create Line |
| 82 |
* Shape* createLine() { |
| 83 |
* return new Line; |
| 84 |
* } |
| 85 |
* |
| 86 |
* //register createLine |
| 87 |
* //note: must put ShapeFactory::getInstance()->registerCreator("Line", createLine) on the right |
| 88 |
* //hand side, otherwise the compiler will consider it as a function declaration |
| 89 |
* const bool registeredLine = ShapeFactory::getInstance()->registerCreator("Line", createLine); |
| 90 |
* |
| 91 |
* //Circle class |
| 92 |
* class Circle : public Shape{ |
| 93 |
* ... |
| 94 |
* }; |
| 95 |
* |
| 96 |
* //declare function to create Circle |
| 97 |
* Shape* createCircle() { |
| 98 |
* return new Circle; |
| 99 |
* } |
| 100 |
* |
| 101 |
* //register createCircle |
| 102 |
* const bool registeredCircle = ShapeFactory::getInstance()->registerCreator("Circle", createCircle); |
| 103 |
* |
| 104 |
* //create object by ident |
| 105 |
* Line* line = ShapeFactory::getInstance()->createObject("Line"); |
| 106 |
* Circle* circle = ShapeFactory::getInstance()->createObject("Circle"); |
| 107 |
* @endcode |
| 108 |
* |
| 109 |
* Or the user can use predefined macro DECLARE_CREATOR and REGISTER_CREATOR |
| 110 |
* @code |
| 111 |
* //Shape class |
| 112 |
* class Shape { |
| 113 |
* ... |
| 114 |
* }; |
| 115 |
* |
| 116 |
* //instantiating a new object factory |
| 117 |
* typedef GenericFactory<Shape> ShapeFactory; |
| 118 |
* |
| 119 |
* //Line class |
| 120 |
* class Line : public Shape{ |
| 121 |
* ... |
| 122 |
* }; |
| 123 |
* |
| 124 |
* //declare function using macro |
| 125 |
* DECLARE_CREATOR(Shape, Line) |
| 126 |
* |
| 127 |
* //register using macro |
| 128 |
* REGISTER_CREATOR(ShapeFactory, "Line", Line); |
| 129 |
|
| 130 |
* //Circle class |
| 131 |
* class Circle : public Shape{ |
| 132 |
* ... |
| 133 |
* }; |
| 134 |
* |
| 135 |
* //declare function using macro |
| 136 |
* DECLARE_CREATOR(Shape, Circle) |
| 137 |
* |
| 138 |
* //register using macro |
| 139 |
* REGISTER_CREATOR(ShapeFactory, "Circle", Circle); |
| 140 |
* @endcode |
| 141 |
*/ |
| 142 |
template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()> |
| 143 |
class GenericFactory { |
| 144 |
public: |
| 145 |
typedef GenericFactory<Object, IdentType, Creator> FactoryType; |
| 146 |
typedef std::map<IdentType, Creator> CreatorMapType; |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns an instance of object factory |
| 150 |
* @return an instance of object factory |
| 151 |
*/ |
| 152 |
static FactoryType* getInstance(){ |
| 153 |
if (instance_ == NULL) |
| 154 |
instance_ = new FactoryType; |
| 155 |
return instance_; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Registers a creator with a type identifier |
| 160 |
* @return true if registration is succeed, otherwise return false |
| 161 |
* @id the identification of the concrete object |
| 162 |
* @creator the object responsible to create the concrete object |
| 163 |
*/ |
| 164 |
bool registerCreator(const IdentType& id, Creator creator) { |
| 165 |
return creatorMap_.insert( |
| 166 |
CreatorMapType::value_type(id, creator)).second; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Unregisters the creator for the given type identifier. If the type identifier |
| 171 |
* was previously registered, the function returns true. |
| 172 |
* @return truethe type identifier was previously registered and the creator is removed, |
| 173 |
* otherwise return false |
| 174 |
* @id the identification of the concrete object |
| 175 |
*/ |
| 176 |
bool unregisterCreator(const IdentType& id) { |
| 177 |
return creatorMap_.erase(id) == 1; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Looks up the type identifier in the internal map. If it is found, it invokes the |
| 182 |
* corresponding creator for the type identifier and returns its result. |
| 183 |
* @return a pointer of the concrete object, return NULL if no creator is registed for |
| 184 |
* creating this concrete object |
| 185 |
* @param id the identification of the concrete object |
| 186 |
*/ |
| 187 |
Object* createObject(const IdentType& id) { |
| 188 |
typename CreatorMapType::iterator i = creatorMap_.find(id); |
| 189 |
if (i != creatorMap_.end()) { |
| 190 |
//invoke functor to create object |
| 191 |
return (i->second)(); |
| 192 |
} else { |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Returns all of the registed type identifiers |
| 199 |
* @return all of the registed type identifiers |
| 200 |
*/ |
| 201 |
std::vector<IdentType> getIdents() { |
| 202 |
std::vector<IdentType> idents; |
| 203 |
typename CreatorMapType::iterator i; |
| 204 |
|
| 205 |
for (i = creatorMap_.begin(); i != creatorMap_.end(); ++i) { |
| 206 |
idents.push_back(i->first); |
| 207 |
} |
| 208 |
|
| 209 |
return idents; |
| 210 |
} |
| 211 |
|
| 212 |
public: |
| 213 |
static FactoryType* instance_; |
| 214 |
CreatorMapType creatorMap_; |
| 215 |
}; |
| 216 |
|
| 217 |
/** write out all of the type identifiers to an output stream */ |
| 218 |
template<typename O, typename I, typename C> |
| 219 |
std::ostream& operator <<(std::ostream& o, GenericFactory<O, I, C>& factory) { |
| 220 |
std::vector<I> idents; |
| 221 |
std::vector<I>::iterator i; |
| 222 |
|
| 223 |
idents = factory.getIdents(); |
| 224 |
|
| 225 |
o << "Avaliable type identifiers in this factory: " << std::endl; |
| 226 |
for (i = idents.begin(); i != idents.end(); ++i) { |
| 227 |
o << *i << std::endl; |
| 228 |
} |
| 229 |
|
| 230 |
return o; |
| 231 |
} |
| 232 |
|
| 233 |
//static template class member |
| 234 |
template<class Object, typename IdentType,typename Creator> |
| 235 |
GenericFactory<Object,IdentType,Creator>* GenericFactory<Object,IdentType,Creator>::instance_ ; |
| 236 |
|
| 237 |
|
| 238 |
#define DECLARE_CREATOR(abstractObject, concreteObject) \ |
| 239 |
inline abstractObject* create##concreteObject(){ \ |
| 240 |
return new concreteObject; \ |
| 241 |
} |
| 242 |
|
| 243 |
#define REGISTER_CREATOR(factory, ident, concreteObject) \ |
| 244 |
const bool registered##concreteObject = factory::getInstance()->registerCreator(ident, create##concreteObject); |
| 245 |
|
| 246 |
|
| 247 |
}//namespace OpenMD |
| 248 |
#endif //UTIL_GENERICFACTORY_HPP |
| 249 |
|