--- trunk/OOPSE-4/src/utils/GenericFactory.hpp 2004/10/18 05:23:21 1587 +++ trunk/OOPSE-4/src/utils/GenericFactory.hpp 2004/10/18 17:07:27 1592 @@ -33,32 +33,76 @@ #define UTIL_GENERICFACTORY_HPP #include #include - -template +namespace oopse { +template class GenericFactory{ public: - typedef map CreatorMapType; - ~GenericFactory(); + typedef std::map CreatorMapType; + typedef GenericFactory SelfType; + static SelfType* getInstance() { + if (instance_ == NULL) { + instance_ = new GenericFactory(); + } - static GenericFactory* getInstance(); + return instance_; + } + + //bool register( const ProductIdentType& id, Creator creator) { - bool register(CreatorType* creator); + //insert method in std::map will return a pair. the second + //element of this pair indicates whether the result of the operation + //return creatorMap_.insert(CreatorMapType::value_type(id, creator)).second; + //} - bool unregister(); + bool unregister(const ProductIdentType& id) { + + return creatorMap_->erase(id) == 1; + + } - bool hasCreator( const ProductIdentType& id ); + bool hasCreator( const ProductIdentType& id ) { + CreatorMapType::iterator i; - const std::string toString(); + i = creatorMap_.find(id); - Product* createProduct( const ProductIdentType& id ); + if (i != creatorMap_.end()) { + return true; + } else { + return false; + } + } + + //const std::string toString() { + + //} + + Product* createProduct( const ProductIdentType& id ) { + CreatorMapType::iterator i; + + i = creatorMap_.find(id); + + if (i != creatorMap_.end()) { + //call the function to create the product + return (i->second)(); + } else { + return NULL; + } + } + private: GenericFactory(){} - static GenericFactory* instance_; - map creatorMap_; + static SelfType* instance_; + CreatorMapType creatorMap_; }; -typedef GenericFactory ForceFiledParserFactory; -#endif +#define REGISTER_CREATOR(factory, product, id) \ + product * create##product() {\ + return new product(); \ + } + +}//namespace oopse +#endif //UTIL_GENERICFACTORY_HPP +