factory.h

Go to the documentation of this file.
00001 
00022 #ifndef _TEMPLATES__FACTORY_H_
00023 #define _TEMPLATES__FACTORY_H_
00024 
00025 #include <vector>
00026 #include <map>
00027 #include <cassert>
00028 
00032 namespace Templates
00033 {
00034         template <typename, class>
00035         class DefaultFactoryError;
00036 
00037         template <class, typename, typename, template<typename, class> class>
00038         class Factory;
00039 }
00040 
00044 template <typename IdentifierType, class AbstractProduct>
00045 class Templates::DefaultFactoryError
00046 {
00047         protected:
00048 
00052                 DefaultFactoryError() { }
00053 
00057                 ~DefaultFactoryError() { }
00058 
00059         public:
00060 
00069                 static AbstractProduct *onUnknownType(IdentifierType /*id*/)
00070                 {
00071                         // Trying to instantiate an unknown object!
00072                         assert(false);
00073                         return 0;
00074                 }
00075 };
00076 
00083 template <
00084         class AbstractProduct,
00085         typename IdentifierType,
00086         typename ProductCreator = AbstractProduct *(*)(),
00087         template<typename, class>
00088                 class FactoryErrorPolicy = Templates::DefaultFactoryError
00089 >
00090 class Templates::Factory: public FactoryErrorPolicy<IdentifierType, AbstractProduct>
00091 {
00092         private:
00093 
00095                 typedef std::map<IdentifierType, ProductCreator> IdToProductMap;
00096 
00098                 IdToProductMap _associations;
00099 
00100         public:
00101 
00105                 Factory(): _associations() { }
00106 
00118                 bool registerObject(IdentifierType id, ProductCreator creator)
00119                 {
00120                         return _associations.insert(
00121                                 typename IdToProductMap::value_type(id, creator)).second;
00122                 }
00123 
00131                 bool unregisterObject(const IdentifierType & id) const
00132                 {
00133                         return _associations.erase(id) == 1;
00134                 }
00135 
00145                 AbstractProduct *createObject(const IdentifierType & id) const
00146                 {
00147                         typename IdToProductMap::const_iterator i = _associations.find(id);
00148                         if (i != _associations.end()) {
00149                                 // Instantiate the object via registered function
00150                                 return (i->second)();
00151                         }
00152 
00153                         return onUnknownType(id);
00154                 }
00155 
00159                 std::vector<IdentifierType> registeredObjects() const
00160                 {
00161                         std::vector<IdentifierType> regObjects;
00162                         for (typename IdToProductMap::const_iterator i = _associations.begin();
00163                                 i != _associations.end(); ++i) {
00164                                 regObjects.push_back(i->first);
00165                         }
00166                         return regObjects;
00167                 }
00168 };
00169 
00170 #endif

Generated on Sat Dec 20 19:21:24 2008 for PGR2008 by  doxygen 1.5.6