00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef FACTORY_HPP
00026 #define FACTORY_HPP
00027
00028
00029 #include <mapnik/utils.hpp>
00030
00031 #include <stdexcept>
00032 #include <map>
00033
00034 namespace mapnik {
00035 template <typename key_type,
00036 typename product_type>
00037 class default_factory_error
00038 {
00039 public:
00040 struct factory_exception : public std::exception
00041 {
00042 const char* what() const throw()
00043 {
00044 return "uknown object type";
00045 }
00046 };
00047 static product_type* on_unknown_type(const key_type&)
00048 {
00049 return 0;
00050 }
00051 };
00052
00053 template
00054 <
00055 typename product_type,
00056 typename key_type,
00057 typename product_creator=product_type* (*)(),
00058 template <typename,typename> class factory_error_policy=default_factory_error
00059 >
00060 class factory : public singleton<factory <product_type,
00061 key_type,
00062 product_creator,factory_error_policy> >,
00063 factory_error_policy <key_type,product_type>
00064 {
00065 private:
00066 typedef std::map<key_type,product_creator> product_map;
00067 product_map map_;
00068 public:
00069
00070 bool register_product(const key_type& key,product_creator creator)
00071 {
00072 return map_.insert(typename product_map::value_type(key,creator)).second;
00073 }
00074
00075 bool unregister_product(const key_type& key)
00076 {
00077 return map_.erase(key)==1;
00078 }
00079
00080 product_type* create_object(const key_type& key,const std::string& file)
00081 {
00082 typename product_map::const_iterator pos=map_.find(key);
00083 if (pos!=map_.end())
00084 {
00085 return (pos->second)(file);
00086 }
00087 return on_unknown_type(key);
00088 }
00089 };
00090 }
00091
00092 #endif //FACTORY_HPP