Factory.h

Go to the documentation of this file.
00001 
00023 #ifndef __FACTORY__H__
00024 #define __FACTORY__H__
00025 
00026 #include <map>
00027 
00028 #include "exceptions.h"
00029 
00033 template <typename IdentifierType, class AbstractProduct>
00034 class DefaultFactoryError {
00035 public:
00039         static AbstractProduct * OnUnknownType(IdentifierType) {
00040                 throw ExceptionFactory("Unknown Type!");
00041         }
00042 
00043 protected:
00047         ~DefaultFactoryError() {}
00048 };
00049 
00056 template <
00057         class AbstractProduct,
00058         typename IdentifierType,
00059         typename Initializator,
00060         typename ProductCreator = AbstractProduct * (*)(const Initializator &),
00061         template<typename, class>
00062                 class FactoryErrorPolicy = DefaultFactoryError
00063 >
00064 class Factory: public FactoryErrorPolicy<IdentifierType, AbstractProduct> {
00065 public:
00071         bool Register(const IdentifierType & id, ProductCreator creator) {
00072                 return associations.insert(
00073                         typename IdToProductMap::value_type(id, creator)).second;
00074         }
00075 
00080         bool Unregister(const IdentifierType & id) {
00081                 return associations.erase(id) == 1;
00082         }
00083 
00089         AbstractProduct * CreateObject(const IdentifierType & id, const Initializator & init) {
00090                 typename IdToProductMap::const_iterator i = associations.find(id);
00091                 if (i != associations.end()) {
00092                         return (i->second)(init);
00093                 }
00094 
00095                 return OnUnknownType(id);
00096         }
00097 
00098 private:
00100         typedef std::map<IdentifierType, ProductCreator> IdToProductMap;
00102         IdToProductMap associations;
00103 };
00104 
00105 #endif /* #ifndef __FACTORY__H__ */
00106 
00107 /* End of file Factory.h */

Generated on Sun Apr 29 11:46:10 2007 for IPP/ICP2007 by  doxygen 1.4.7