ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/GenericFactory.hpp
Revision: 1665
Committed: Tue Nov 22 20:38:56 2011 UTC (13 years, 5 months ago) by gezelter
File size: 8480 byte(s)
Log Message:
updated copyright notices

File Contents

# User Rev Content
1 gezelter 507 /*
2 gezelter 246 * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 tim 94 *
4 gezelter 246 * 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 gezelter 1390 * 1. Redistributions of source code must retain the above copyright
10 gezelter 246 * notice, this list of conditions and the following disclaimer.
11     *
12 gezelter 1390 * 2. Redistributions in binary form must reproduce the above copyright
13 gezelter 246 * 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 gezelter 1390 *
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 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 tim 94 */
42 gezelter 246
43 tim 94 /**
44 tim 148 * @file GenericFactory.hpp
45 tim 94 * @author Teng Lin
46 tim 148 * @date 10/24/2004
47 tim 94 * @version 1.0
48     */
49     #ifndef UTIL_GENERICFACTORY_HPP
50     #define UTIL_GENERICFACTORY_HPP
51 tim 150 #include <cassert>
52 tim 94 #include <map>
53     #include <string>
54 tim 148 #include <vector>
55    
56 gezelter 1390 namespace OpenMD {
57 tim 148
58 gezelter 507 /**
59     * @class GenericFactory GenericFactory.hpp "utils/GenericFactory.hpp"
60     * @brief GenericFactory is a template based Object Factory
61     * Factory pattern is used to define an interface for creating an object.
62     *
63     * @param Object the base class of the hierarchy for which you provide the object factory.
64     * @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(),
65     * taking no parameters and returning a pointer to Object. Default type is function pointer.
66     *
67     * Usage:
68     * @code
69     * //Shape class
70     * class Shape {
71     * ...
72     * };
73     *
74     * //instantiating a new object factory
75     * typedef GenericFactory<Shape> ShapeFactory;
76     *
77     * //Line class
78     * class Line : public Shape{
79     * ...
80     * };
81     *
82     * //declare function to create Line
83     * Shape* createLine() {
84     * return new Line;
85     * }
86     *
87     * //register createLine
88     * //note: must put ShapeFactory::getInstance()->registerCreator("Line", createLine) on the right
89     * //hand side, otherwise the compiler will consider it as a function declaration
90     * const bool registeredLine = ShapeFactory::getInstance()->registerCreator("Line", createLine);
91     *
92     * //Circle class
93     * class Circle : public Shape{
94     * ...
95     * };
96     *
97     * //declare function to create Circle
98     * Shape* createCircle() {
99     * return new Circle;
100     * }
101     *
102     * //register createCircle
103     * const bool registeredCircle = ShapeFactory::getInstance()->registerCreator("Circle", createCircle);
104     *
105     * //create object by ident
106     * Line* line = ShapeFactory::getInstance()->createObject("Line");
107     * Circle* circle = ShapeFactory::getInstance()->createObject("Circle");
108     * @endcode
109     *
110     * Or the user can use predefined macro DECLARE_CREATOR and REGISTER_CREATOR
111     * @code
112     * //Shape class
113     * class Shape {
114     * ...
115     * };
116     *
117     * //instantiating a new object factory
118     * typedef GenericFactory<Shape> ShapeFactory;
119     *
120     * //Line class
121     * class Line : public Shape{
122     * ...
123     * };
124     *
125     * //declare function using macro
126     * DECLARE_CREATOR(Shape, Line)
127     *
128     * //register using macro
129     * REGISTER_CREATOR(ShapeFactory, "Line", Line);
130 tim 150
131 gezelter 507 * //Circle class
132     * class Circle : public Shape{
133     * ...
134     * };
135     *
136     * //declare function using macro
137     * DECLARE_CREATOR(Shape, Circle)
138     *
139     * //register using macro
140     * REGISTER_CREATOR(ShapeFactory, "Circle", Circle);
141     * @endcode
142     */
143     template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
144     class GenericFactory {
145     public:
146     typedef GenericFactory<Object, IdentType, Creator> FactoryType;
147     typedef std::map<IdentType, Creator> CreatorMapType;
148 tim 94
149 gezelter 507 /**
150     * Returns an instance of object factory
151     * @return an instance of object factory
152     */
153     static FactoryType* getInstance(){
154     if (instance_ == NULL)
155     instance_ = new FactoryType;
156     return instance_;
157     }
158 tim 94
159 gezelter 507 /**
160     * Registers a creator with a type identifier
161     * @return true if registration is succeed, otherwise return false
162     * @id the identification of the concrete object
163     * @creator the object responsible to create the concrete object
164     */
165     bool registerCreator(const IdentType& id, Creator creator) {
166     return creatorMap_.insert(
167     CreatorMapType::value_type(id, creator)).second;
168     }
169 tim 94
170 gezelter 507 /**
171     * Unregisters the creator for the given type identifier. If the type identifier
172     * was previously registered, the function returns true.
173     * @return truethe type identifier was previously registered and the creator is removed,
174     * otherwise return false
175     * @id the identification of the concrete object
176     */
177     bool unregisterCreator(const IdentType& id) {
178     return creatorMap_.erase(id) == 1;
179     }
180 tim 94
181 gezelter 507 /**
182     * Looks up the type identifier in the internal map. If it is found, it invokes the
183     * corresponding creator for the type identifier and returns its result.
184     * @return a pointer of the concrete object, return NULL if no creator is registed for
185     * creating this concrete object
186     * @param id the identification of the concrete object
187     */
188     Object* createObject(const IdentType& id) {
189     typename CreatorMapType::iterator i = creatorMap_.find(id);
190     if (i != creatorMap_.end()) {
191     //invoke functor to create object
192     return (i->second)();
193     } else {
194     return NULL;
195     }
196     }
197 tim 99
198 gezelter 507 /**
199     * Returns all of the registed type identifiers
200     * @return all of the registed type identifiers
201     */
202     std::vector<IdentType> getIdents() {
203     std::vector<IdentType> idents;
204     typename CreatorMapType::iterator i;
205 tim 99
206 gezelter 507 for (i = creatorMap_.begin(); i != creatorMap_.end(); ++i) {
207     idents.push_back(i->first);
208     }
209 tim 148
210 gezelter 507 return idents;
211     }
212 tim 148
213 gezelter 507 public:
214     static FactoryType* instance_;
215     CreatorMapType creatorMap_;
216     };
217 tim 94
218 gezelter 507 /** write out all of the type identifiers to an output stream */
219     template<typename O, typename I, typename C>
220     std::ostream& operator <<(std::ostream& o, GenericFactory<O, I, C>& factory) {
221 tim 148 std::vector<I> idents;
222     std::vector<I>::iterator i;
223 tim 94
224 tim 148 idents = factory.getIdents();
225    
226     o << "Avaliable type identifiers in this factory: " << std::endl;
227     for (i = idents.begin(); i != idents.end(); ++i) {
228 gezelter 507 o << *i << std::endl;
229 tim 99 }
230    
231 tim 148 return o;
232 gezelter 507 }
233 tim 148
234 gezelter 507 //static template class member
235     template<class Object, typename IdentType,typename Creator>
236     GenericFactory<Object,IdentType,Creator>* GenericFactory<Object,IdentType,Creator>::instance_ ;
237 tim 148
238 tim 150
239 tim 148 #define DECLARE_CREATOR(abstractObject, concreteObject) \
240 gezelter 507 inline abstractObject* create##concreteObject(){ \
241     return new concreteObject; \
242     }
243 tim 148
244 gezelter 507 #define REGISTER_CREATOR(factory, ident, concreteObject) \
245     const bool registered##concreteObject = factory::getInstance()->registerCreator(ident, create##concreteObject);
246 tim 150
247    
248 gezelter 1390 }//namespace OpenMD
249 tim 99 #endif //UTIL_GENERICFACTORY_HPP
250    

Properties

Name Value
svn:keywords Author Id Revision Date