/home/andreas/src/svn/mapnik/include/mapnik/factory.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * 
00003  * This file is part of Mapnik (c++ mapping toolkit)
00004  *
00005  * Copyright (C) 2006 Artem Pavlenko
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  *****************************************************************************/
00022 
00023 //$Id: factory.hpp 39 2005-04-10 20:39:53Z pavlenko $
00024 
00025 #ifndef FACTORY_HPP
00026 #define FACTORY_HPP
00027 
00028 // stl
00029 #include <stdexcept>
00030 #include <map>
00031 // mapnik
00032 #include <mapnik/utils.hpp>
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

Generated on Thu Jul 19 17:59:25 2007 for Mapnik by  doxygen 1.4.7