diff --git a/include/mapnik/formatting/registry.hpp b/include/mapnik/formatting/registry.hpp new file mode 100644 index 000000000..9c2c9df22 --- /dev/null +++ b/include/mapnik/formatting/registry.hpp @@ -0,0 +1,57 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2012 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ +#ifndef FORMATTING_REGISTRY_HPP +#define FORMATTING_REGISTRY_HPP + +// mapnik +#include +#include + +// boost +#include + +// stl +#include +#include + +namespace mapnik +{ +namespace formatting +{ + +typedef node_ptr (*from_xml_function_ptr)(boost::property_tree::ptree const& xml); + +class registry : public singleton, + private boost::noncopyable +{ +public: + registry(); + ~registry() {} + void register_name(std::string name, from_xml_function_ptr ptr, bool overwrite=false); + node_ptr from_xml(std::string name, boost::property_tree::ptree const& xml); +private: + std::map map_; +}; + +} //ns formatting +} //ns mapnik +#endif // FORMATTING_REGISTRY_HPP diff --git a/src/build.py b/src/build.py index e12ec4d33..50f3c235f 100644 --- a/src/build.py +++ b/src/build.py @@ -170,6 +170,7 @@ source = Split( formatting/list.cpp formatting/text.cpp formatting/format.cpp + formatting/registry.cpp """ ) diff --git a/src/formatting/base.cpp b/src/formatting/base.cpp index 2cf76a24d..e9c1ff3e7 100644 --- a/src/formatting/base.cpp +++ b/src/formatting/base.cpp @@ -22,9 +22,7 @@ // mapnik #include #include -#include -#include -#include +#include // boost #include @@ -47,16 +45,11 @@ node_ptr node::from_xml(boost::property_tree::ptree const& xml) boost::property_tree::ptree::const_iterator itr = xml.begin(); boost::property_tree::ptree::const_iterator end = xml.end(); for (; itr != end; ++itr) { - node_ptr n; - if (itr->first == "") { - n = text_node::from_xml(itr->second); - } else if (itr->first == "Format") { - n = format_node::from_xml(itr->second); - } else if (itr->first == "ExpressionFormat") { - n = expression_format::from_xml(itr->second); - } else if (itr->first != "" && itr->first != "" && itr->first != "Placement") { - throw config_error("Unknown item " + itr->first); + if (itr->first == "" || itr->first == "" || itr->first == "Placement") + { + continue; } + node_ptr n = registry::instance()->from_xml(itr->first, itr->second); if (n) list->push_back(n); } if (list->get_children().size() == 1) { diff --git a/src/formatting/registry.cpp b/src/formatting/registry.cpp new file mode 100644 index 000000000..2c3a29a4a --- /dev/null +++ b/src/formatting/registry.cpp @@ -0,0 +1,56 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2012 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ +// mapnik +#include +#include +#include +#include + +namespace mapnik +{ +namespace formatting +{ + +registry::registry() +{ + register_name("", &text_node::from_xml); + register_name("Format", &format_node::from_xml); + register_name("ExpressionFormat", &expression_format::from_xml); +} + +void registry::register_name(std::string name, from_xml_function_ptr ptr, bool overwrite) +{ + if (overwrite) { + map_[name] = ptr; + } else { + map_.insert(make_pair(name, ptr)); + } +} + +node_ptr registry::from_xml(std::string name, const boost::property_tree::ptree &xml) +{ + std::map::const_iterator itr = map_.find(name); + if (itr == map_.end()) throw config_error("Unknown element '" + name + "'"); + return itr->second(xml); +} +} //ns formatting +} //ns mapnik