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

Comparing trunk/OOPSE-4/src/utils/GenericFactory.hpp (file contents):
Revision 1641 by tim, Sun Oct 24 07:55:48 2004 UTC vs.
Revision 1643 by tim, Mon Oct 25 17:03:56 2004 UTC

# Line 31 | Line 31
31   */
32   #ifndef UTIL_GENERICFACTORY_HPP
33   #define UTIL_GENERICFACTORY_HPP
34 + #include <cassert>
35   #include <map>
36   #include <string>
37   #include <vector>
# Line 47 | Line 48 | namespace oopse {
48   * @param Creator  the callable entity that creates objects. This type must support operator(),
49   * taking no parameters and returning a pointer to Object. Default type is function pointer.
50   *
51 + * Usage:
52   * @code
53   * //Shape class
54   * class Shape {
# Line 67 | Line 69 | namespace oopse {
69   * }
70   *
71   * //register createLine
72 < * ShapeFactory::getInstance()->registerCreator("Line", createLine);
72 > * //note: must put ShapeFactory::getInstance()->registerCreator("Line", createLine) on the right
73 > * //hand side, otherwise the compiler will consider it as a function declaration
74 > * const bool registeredLine = ShapeFactory::getInstance()->registerCreator("Line", createLine);
75   *
76   * //Circle class
77   * class Circle : public Shape{
# Line 80 | Line 84 | namespace oopse {
84   * }
85   *
86   * //register createCircle
87 < * ShapeFactory::getInstance()->registerCreator("Circle", createCircle);
87 > * const bool registeredCircle = ShapeFactory::getInstance()->registerCreator("Circle", createCircle);
88   *
89   * //create object by ident
90   * Line* line = ShapeFactory::getInstance()->createObject("Line");
91   * Circle* circle = ShapeFactory::getInstance()->createObject("Circle");
92   * @endcode
93 + *
94 + * Or the user can use predefined macro DECLARE_CREATOR and REGISTER_CREATOR
95 + * @code
96 + * //Shape class
97 + * class Shape {
98 + * ...
99 + * };
100 + *
101 + * //instantiating a new object factory
102 + * typedef GenericFactory<Shape> ShapeFactory;
103 + *
104 + * //Line class
105 + * class Line : public Shape{
106 + * ...
107 + * };
108 + *
109 + * //declare function using macro
110 + * DECLARE_CREATOR(Shape, Line)
111 + *
112 + * //register using macro
113 + * REGISTER_CREATOR(ShapeFactory, "Line", Line);
114 +
115 + * //Circle class
116 + * class Circle : public Shape{
117 + * ...
118 + * };
119 + *
120 + * //declare function using macro
121 + * DECLARE_CREATOR(Shape, Circle)
122 + *
123 + * //register using macro
124 + * REGISTER_CREATOR(ShapeFactory, "Circle", Circle);
125 + * @endcode
126   */
127   template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
128   class GenericFactory {
# Line 130 | Line 167 | class GenericFactory {
167           * corresponding creator for the type identifier and returns its result.
168           * @return a pointer of the concrete object, return NULL if no creator is registed for
169           * creating this concrete object
170 <         * @id the identification of the concrete object
170 >         * @param id the identification of the concrete object
171           */
172          Object* createObject(const IdentType& id) {
173              typename CreatorMapType::iterator i = creatorMap_.find(id);
174              if (i != creatorMap_.end()) {
175 +                //invoke functor to create object
176                  return (i->second)();
177              } else {
178                  return NULL;
# Line 181 | Line 219 | GenericFactory<Object,IdentType,Creator>* GenericFacto
219   template<class Object, typename IdentType,typename Creator>
220   GenericFactory<Object,IdentType,Creator>* GenericFactory<Object,IdentType,Creator>::instance_ ;
221  
222 +
223   #define DECLARE_CREATOR(abstractObject, concreteObject) \
224 <    abstractObject* create##concreteObject(){\
224 >    inline abstractObject* create##concreteObject(){\
225          return new concreteObject;\
226      }
227  
228 + #define REGISTER_CREATOR(factory, ident, concreteObject) \
229 +        const bool registered##concreteObject = factory::getInstance()->registerCreator(ident, create##concreteObject);
230 +
231 +
232   }//namespace oopse
233   #endif //UTIL_GENERICFACTORY_HPP
234  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines