| 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. | 
| 23 | * | 
| 24 | */ | 
| 25 |  | 
| 26 | /** | 
| 27 | * @file GenericFactory.hpp | 
| 28 | * @author Teng Lin | 
| 29 | * @date 10/24/2004 | 
| 30 | * @version 1.0 | 
| 31 | */ | 
| 32 | #ifndef UTIL_GENERICFACTORY_HPP | 
| 33 | #define UTIL_GENERICFACTORY_HPP | 
| 34 | #include <map> | 
| 35 | #include <string> | 
| 36 | #include <vector> | 
| 37 |  | 
| 38 | namespace oopse { | 
| 39 |  | 
| 40 | /** | 
| 41 | * @class GenericFactory GenericFactory.hpp "utils/GenericFactory.hpp" | 
| 42 | * @brief GenericFactory is a template based Object Factory | 
| 43 | * Factory pattern is used to define an interface for creating an object. | 
| 44 | * | 
| 45 | * @param Object the base class of the hierarchy for which you provide the object factory. | 
| 46 | * @param IdentType the object that identifies the type of the concrete object. Default type is string | 
| 47 | * @param Creator  the callable entity that creates objects. This type must support operator(), | 
| 48 | * taking no parameters and returning a pointer to Object. Default type is function pointer. | 
| 49 | * | 
| 50 | * @code | 
| 51 | * //Shape class | 
| 52 | * class Shape { | 
| 53 | * ... | 
| 54 | * }; | 
| 55 | * | 
| 56 | * //instantiating a new object factory | 
| 57 | * typedef GenericFactory<Shape> ShapeFactory; | 
| 58 | * | 
| 59 | * //Line class | 
| 60 | * class Line : public Shape{ | 
| 61 | * ... | 
| 62 | * }; | 
| 63 | * | 
| 64 | * //declare function to create Line | 
| 65 | * Shape* createLine() { | 
| 66 | *   return new Line; | 
| 67 | * } | 
| 68 | * | 
| 69 | * //register createLine | 
| 70 | * ShapeFactory::getInstance()->registerCreator("Line", createLine); | 
| 71 | * | 
| 72 | * //Circle class | 
| 73 | * class Circle : public Shape{ | 
| 74 | * ... | 
| 75 | * }; | 
| 76 | * | 
| 77 | * //declare function to create Circle | 
| 78 | * Shape* createCircle() { | 
| 79 | *   return new Circle; | 
| 80 | * } | 
| 81 | * | 
| 82 | * //register createCircle | 
| 83 | * ShapeFactory::getInstance()->registerCreator("Circle", createCircle); | 
| 84 | * | 
| 85 | * //create object by ident | 
| 86 | * Line* line = ShapeFactory::getInstance()->createObject("Line"); | 
| 87 | * Circle* circle = ShapeFactory::getInstance()->createObject("Circle"); | 
| 88 | * @endcode | 
| 89 | */ | 
| 90 | template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()> | 
| 91 | class GenericFactory { | 
| 92 | public: | 
| 93 | typedef GenericFactory<Object, IdentType, Creator> FactoryType; | 
| 94 | typedef std::map<IdentType, Creator> CreatorMapType; | 
| 95 |  | 
| 96 | /** | 
| 97 | * Returns an instance of object factory | 
| 98 | * @return an instance of object factory | 
| 99 | */ | 
| 100 | static FactoryType* getInstance(){ | 
| 101 | if (instance_ == NULL) | 
| 102 | instance_ = new FactoryType; | 
| 103 | return instance_; | 
| 104 | } | 
| 105 |  | 
| 106 | /** | 
| 107 | * Registers a creator with a type identifier | 
| 108 | * @return true if registration is succeed, otherwise return false | 
| 109 | * @id the identification of the concrete object | 
| 110 | * @creator the object responsible to create the concrete object | 
| 111 | */ | 
| 112 | bool registerCreator(const IdentType& id, Creator creator) { | 
| 113 | return creatorMap_.insert( | 
| 114 | CreatorMapType::value_type(id, creator)).second; | 
| 115 | } | 
| 116 |  | 
| 117 | /** | 
| 118 | * Unregisters the creator for the given type identifier. If the type identifier | 
| 119 | * was previously registered, the function returns true. | 
| 120 | * @return truethe type identifier was previously registered and the creator is removed, | 
| 121 | * otherwise return false | 
| 122 | * @id the identification of the concrete object | 
| 123 | */ | 
| 124 | bool unregisterCreator(const IdentType& id) { | 
| 125 | return creatorMap_.erase(id) == 1; | 
| 126 | } | 
| 127 |  | 
| 128 | /** | 
| 129 | * Looks up the type identifier in the internal map. If it is found, it invokes the | 
| 130 | * corresponding creator for the type identifier and returns its result. | 
| 131 | * @return a pointer of the concrete object, return NULL if no creator is registed for | 
| 132 | * creating this concrete object | 
| 133 | * @param id the identification of the concrete object | 
| 134 | */ | 
| 135 | Object* createObject(const IdentType& id) { | 
| 136 | typename CreatorMapType::iterator i = creatorMap_.find(id); | 
| 137 | if (i != creatorMap_.end()) { | 
| 138 | return (i->second)(); | 
| 139 | } else { | 
| 140 | return NULL; | 
| 141 | } | 
| 142 | } | 
| 143 |  | 
| 144 | /** | 
| 145 | *  Returns all of the registed  type identifiers | 
| 146 | * @return all of the registed  type identifiers | 
| 147 | */ | 
| 148 | std::vector<IdentType> getIdents() { | 
| 149 | std::vector<IdentType> idents; | 
| 150 | typename CreatorMapType::iterator i; | 
| 151 |  | 
| 152 | for (i = creatorMap_.begin(); i != creatorMap_.end(); ++i) { | 
| 153 | idents.push_back(i->first); | 
| 154 | } | 
| 155 |  | 
| 156 | return idents; | 
| 157 | } | 
| 158 |  | 
| 159 | public: | 
| 160 | static FactoryType* instance_; | 
| 161 | CreatorMapType creatorMap_; | 
| 162 | }; | 
| 163 |  | 
| 164 | /** write out all of the type identifier to a output stream */ | 
| 165 | template<typename O, typename I, typename C> | 
| 166 | std::ostream& operator <<(std::ostream& o, GenericFactory<O, I, C>& factory) { | 
| 167 | std::vector<I> idents; | 
| 168 | std::vector<I>::iterator i; | 
| 169 |  | 
| 170 | idents = factory.getIdents(); | 
| 171 |  | 
| 172 | o << "Avaliable type identifiers in this factory: " << std::endl; | 
| 173 | for (i = idents.begin(); i != idents.end(); ++i) { | 
| 174 | o << *i << std::endl; | 
| 175 | } | 
| 176 |  | 
| 177 | return o; | 
| 178 | } | 
| 179 |  | 
| 180 | //static template class member | 
| 181 | template<class Object, typename IdentType,typename Creator> | 
| 182 | GenericFactory<Object,IdentType,Creator>* GenericFactory<Object,IdentType,Creator>::instance_ ; | 
| 183 |  | 
| 184 | #define DECLARE_CREATOR(abstractObject, concreteObject) \ | 
| 185 | abstractObject* create##concreteObject(){\ | 
| 186 | return new concreteObject;\ | 
| 187 | } | 
| 188 |  | 
| 189 | }//namespace oopse | 
| 190 | #endif //UTIL_GENERICFACTORY_HPP | 
| 191 |  |