ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/utils/GenericFactory.hpp
(Generate patch)

Comparing trunk/src/utils/GenericFactory.hpp (file contents):
Revision 150 by tim, Mon Oct 25 17:03:56 2004 UTC vs.
Revision 1879 by gezelter, Sun Jun 16 15:15:42 2013 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project
3 < *
4 < * Contact: oopse@oopse.org
5 < *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU Lesser General Public License
8 < * as published by the Free Software Foundation; either version 2.1
9 < * of the License, or (at your option) any later version.
10 < * All we ask is that proper credit is given for our work, which includes
11 < * - but is not limited to - adding the above copyright notice to the beginning
12 < * of your source code files, and to any copyright notice that you may distribute
13 < * with programs based on this work.
14 < *
15 < * This program is distributed in the hope that it will be useful,
16 < * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 < * GNU Lesser General Public License for more details.
19 < *
20 < * You should have received a copy of the GNU Lesser General Public License
21 < * along with this program; if not, write to the Free Software
22 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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, 234107 (2008).          
39 + * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 + * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42 <
42 >
43   /**
44   * @file GenericFactory.hpp
45   * @author Teng Lin
# Line 36 | Line 53
53   #include <string>
54   #include <vector>
55  
56 < namespace oopse {
56 > namespace OpenMD {
57  
58 < /**
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 string
65 < * @param Creator  the callable entity that creates objects. This type must support operator(),
66 < * taking no parameters and returning a pointer to Object. Default type is function pointer.
67 < *
68 < * Usage:
69 < * @code
70 < * //Shape class
71 < * class Shape {
72 < * ...
73 < * };
74 < *
75 < * //instantiating a new object factory
76 < * typedef GenericFactory<Shape> ShapeFactory;
77 < *
78 < * //Line class
79 < * class Line : public Shape{
80 < * ...
81 < * };
82 < *
83 < * //declare function to create Line
84 < * Shape* createLine() {
85 < *   return new Line;
86 < * }
87 < *
88 < * //register createLine
89 < * //note: must put ShapeFactory::getInstance()->registerCreator("Line", createLine) on the right
90 < * //hand side, otherwise the compiler will consider it as a function declaration
91 < * const bool registeredLine = ShapeFactory::getInstance()->registerCreator("Line", createLine);
92 < *
93 < * //Circle class
94 < * class Circle : public Shape{
95 < * ...
96 < * };
97 < *
98 < * //declare function to create Circle
99 < * Shape* createCircle() {
100 < *   return new Circle;
101 < * }
102 < *
103 < * //register createCircle
104 < * const bool registeredCircle = ShapeFactory::getInstance()->registerCreator("Circle", createCircle);
105 < *
106 < * //create object by ident
107 < * Line* line = ShapeFactory::getInstance()->createObject("Line");
108 < * Circle* circle = ShapeFactory::getInstance()->createObject("Circle");
109 < * @endcode
110 < *
111 < * Or the user can use predefined macro DECLARE_CREATOR and REGISTER_CREATOR
112 < * @code
113 < * //Shape class
114 < * class Shape {
115 < * ...
116 < * };
117 < *
118 < * //instantiating a new object factory
119 < * typedef GenericFactory<Shape> ShapeFactory;
120 < *
121 < * //Line class
122 < * class Line : public Shape{
123 < * ...
124 < * };
125 < *
126 < * //declare function using macro
127 < * DECLARE_CREATOR(Shape, Line)
128 < *
129 < * //register using macro
113 < * REGISTER_CREATOR(ShapeFactory, "Line", Line);
58 >  /**
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  
131 < * //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;
131 >   * //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          
149 <        /**
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 <        }
149 >    /**
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  
159 <        /**
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 <        }
159 >    /**
160 >     * Registers a creator with a type identifier
161 >     * @return true if registration is succeed, otherwise return false
162 >     * @param id the identification of the concrete object
163 >     * @param 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  
170 <        /**
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 <        }
170 >    /**
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 >     * @param id the identification of the concrete object
176 >     */
177 >    bool unregisterCreator(const IdentType& id) {
178 >      return creatorMap_.erase(id) == 1;
179 >    }
180  
181 <        /**
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 <        }
181 >    /**
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  
198 <        /**
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;
198 >    /**
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  
206 <            for (i = creatorMap_.begin(); i != creatorMap_.end(); ++i) {
207 <                idents.push_back(i->first);
208 <            }
206 >      for (i = creatorMap_.begin(); i != creatorMap_.end(); ++i) {
207 >        idents.push_back(i->first);
208 >      }
209              
210 <            return idents;
211 <        }
210 >      return idents;
211 >    }
212  
213 <    public:
214 <        static FactoryType* instance_;
215 <        CreatorMapType creatorMap_;
216 < };
213 >  public:
214 >    static FactoryType* instance_;
215 >    CreatorMapType creatorMap_;
216 >  };
217  
218 < /** write out all of the type identifier to a output stream */
219 < template<typename O, typename I, typename C>
220 < std::ostream& operator <<(std::ostream& o, GenericFactory<O, I, C>& factory) {
218 >  /** 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      std::vector<I> idents;
222      std::vector<I>::iterator i;
223  
# Line 209 | Line 225 | std::ostream& operator <<(std::ostream& o, GenericFact
225  
226      o << "Avaliable type identifiers in this factory: " << std::endl;
227      for (i = idents.begin(); i != idents.end(); ++i) {
228 <        o << *i << std::endl;
228 >      o << *i << std::endl;
229      }
230  
231      return o;
232 < }
232 >  }
233  
234 < //static template class member
235 < template<class Object, typename IdentType,typename Creator>
236 < GenericFactory<Object,IdentType,Creator>* GenericFactory<Object,IdentType,Creator>::instance_ ;
234 >  //static template class member
235 >  template<class Object, typename IdentType,typename Creator>
236 >  GenericFactory<Object,IdentType,Creator>* GenericFactory<Object,IdentType,Creator>::instance_ ;
237  
238  
239   #define DECLARE_CREATOR(abstractObject, concreteObject) \
240 <    inline abstractObject* create##concreteObject(){\
241 <        return new concreteObject;\
242 <    }
240 >  inline abstractObject* create##concreteObject(){      \
241 >    return new concreteObject;                          \
242 >  }
243  
244 < #define REGISTER_CREATOR(factory, ident, concreteObject) \
245 <        const bool registered##concreteObject = factory::getInstance()->registerCreator(ident, create##concreteObject);
244 > #define REGISTER_CREATOR(factory, ident, concreteObject)                \
245 >  const bool registered##concreteObject = factory::getInstance()->registerCreator(ident, create##concreteObject);
246  
247  
248 < }//namespace oopse
248 > }//namespace OpenMD
249   #endif //UTIL_GENERICFACTORY_HPP
250  

Comparing trunk/src/utils/GenericFactory.hpp (property svn:keywords):
Revision 150 by tim, Mon Oct 25 17:03:56 2004 UTC vs.
Revision 1879 by gezelter, Sun Jun 16 15:15:42 2013 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines