From bb43f2cec1eb1f6e1a8ddd31aba0ceed41ca734c Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 9 Jan 2013 19:12:20 -0800 Subject: [PATCH] refactor params interface to move lexical cast out of header --- demo/viewer/layer_info_dialog.cpp | 2 + include/mapnik/params.hpp | 74 +++++++++++++--------- include/mapnik/params_impl.hpp | 102 ++++++++++++++++++++++++++++++ src/params.cpp | 58 ++++------------- 4 files changed, 159 insertions(+), 77 deletions(-) create mode 100644 include/mapnik/params_impl.hpp diff --git a/demo/viewer/layer_info_dialog.cpp b/demo/viewer/layer_info_dialog.cpp index 93e0072db..4654e193a 100644 --- a/demo/viewer/layer_info_dialog.cpp +++ b/demo/viewer/layer_info_dialog.cpp @@ -21,6 +21,8 @@ #include "layer_info_dialog.hpp" // mapnik +#include +#include #include diff --git a/include/mapnik/params.hpp b/include/mapnik/params.hpp index 9013bd891..13d030a23 100644 --- a/include/mapnik/params.hpp +++ b/include/mapnik/params.hpp @@ -23,12 +23,13 @@ #ifndef MAPNIK_PARAMS_HPP #define MAPNIK_PARAMS_HPP +// mapnik +#include +#include + // boost #include #include -#include -// mapnik -#include // stl #include @@ -36,45 +37,58 @@ namespace mapnik { + +// fwd declare +class boolean; + typedef boost::variant value_holder; typedef std::pair parameter; typedef std::map param_map; -// TODO - rewrite to avoid usage of lexical_cast -template -struct value_extractor_visitor : public boost::static_visitor<> -{ - value_extractor_visitor(boost::optional & var) - :var_(var) {} - - void operator () (T val) const - { - var_ = val; - } - - template - void operator () (T1 val) const - { - try - { - var_ = boost::lexical_cast(val); - } - catch (boost::bad_lexical_cast & ) {} - } - - boost::optional & var_; -}; - -class parameters : public param_map +class MAPNIK_DECL parameters : public param_map { public: - parameters(); + parameters() {} template boost::optional get(std::string const& key) const; template boost::optional get(std::string const& key, T const& default_opt_value) const; + }; +#ifdef _MSC_VER +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key) const; +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key, + std::string const& default_opt_value) const; +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key) const; +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key, + value_double const& default_opt_value) const; + +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key) const; +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key, + int const& default_opt_value) const; +#ifdef BIGINT +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key) const; +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key, + value_integer const& default_opt_value) const; +#endif + +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key) const; +template MAPNIK_DECL +boost::optional parameters::get(std::string const& key, + mapnik::boolean const& default_opt_value) const; + +#endif + } #endif // MAPNIK_PARAMS_HPP diff --git a/include/mapnik/params_impl.hpp b/include/mapnik/params_impl.hpp new file mode 100644 index 000000000..15ff1cdc5 --- /dev/null +++ b/include/mapnik/params_impl.hpp @@ -0,0 +1,102 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2013 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 + * + *****************************************************************************/ + +// NOTE: This is an implementation header file and is only meant to be included +// from implementation files. It therefore doesn't have an include guard. + +// mapnik +#include +#include + +// boost +#include +#include +#include + +// stl +#include + +namespace mapnik +{ + +template +struct value_extractor_visitor : public boost::static_visitor<> +{ + value_extractor_visitor(boost::optional & var) + :var_(var) {} + + void operator () (T val) const + { + var_ = val; + } + + template + void operator () (T1 val) const + { + try + { + var_ = boost::lexical_cast(val); + } + catch (boost::bad_lexical_cast & ) {} + } + + boost::optional & var_; +}; + +namespace params_detail { + +template +struct converter +{ + typedef boost::optional return_type; + static return_type extract(parameters const& params, + std::string const& name, + boost::optional const& default_opt_value) + { + boost::optional result(default_opt_value); + parameters::const_iterator itr = params.find(name); + if (itr != params.end()) + { + boost::apply_visitor(value_extractor_visitor(result),itr->second); + } + return result; + } +}; + +} // end namespace params_detail + + +template +boost::optional parameters::get(std::string const& key) const +{ + return params_detail::converter::extract(*this,key, boost::none); +} + +template +boost::optional parameters::get(std::string const& key, T const& default_opt_value) const +{ + return params_detail::converter::extract(*this,key,boost::optional(default_opt_value)); +} + + +} + diff --git a/src/params.cpp b/src/params.cpp index 964750dd8..a989839a3 100644 --- a/src/params.cpp +++ b/src/params.cpp @@ -23,62 +23,26 @@ // mapnik #include #include +#include #include -// boost -#include -#include -#include -#include -#include - namespace mapnik { -namespace params_detail { +template boost::optional parameters::get(std::string const& key) const; +template boost::optional parameters::get(std::string const& key, std::string const& default_opt_value) const; -template -struct converter -{ - typedef boost::optional return_type; - static return_type extract(parameters const& params, - std::string const& name, - boost::optional const& default_opt_value) - { - boost::optional result(default_opt_value); - parameters::const_iterator itr = params.find(name); - if (itr != params.end()) - { - boost::apply_visitor(value_extractor_visitor(result),itr->second); - } - return result; - } -}; -} // end namespace params_detail +template boost::optional parameters::get(std::string const& key) const; +template boost::optional parameters::get(std::string const& key, value_double const& default_opt_value) const; -// parameters +template boost::optional parameters::get(std::string const& key) const; +template boost::optional parameters::get(std::string const& key, int const& default_opt_value) const; -parameters::parameters() {} +template boost::optional parameters::get(std::string const& key) const; +template boost::optional parameters::get(std::string const& key, mapnik::boolean const& default_opt_value) const; -template -boost::optional parameters::get(std::string const& key) const -{ - return params_detail::converter::extract(*this,key, boost::none); -} - -template -boost::optional parameters::get(std::string const& key, T const& default_opt_value) const -{ - return params_detail::converter::extract(*this,key,boost::optional(default_opt_value)); -} - -#define compile_params_get(T) template boost::optional parameters::get(std::string const& key) const; template boost::optional parameters::get(std::string const& key, T const& default_opt_value) const - -compile_params_get(std::string); -compile_params_get(value_double); -compile_params_get(int); #ifdef BIGINT -compile_params_get(value_integer); +template boost::optional parameters::get(std::string const& key) const; +template boost::optional parameters::get(std::string const& key, value_integer const& default_opt_value) const; #endif -compile_params_get(mapnik::boolean); }