/***************************************************************************** * * 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 #include #include // boost #include #include // keep gcc happy #include #include #include #include // stl #include namespace mapnik { namespace detail { template struct extract_value { static inline boost::optional do_extract_from_string(std::string const& source) { std::string err_msg = (boost::format("No conversion from std::string to %s") % typeid(T).name()).str(); throw std::runtime_error(err_msg); } }; template <> struct extract_value { static inline boost::optional do_extract_from_string(std::string const& source) { bool result; if (mapnik::util::string2bool(source, result)) return boost::optional(result); return boost::optional(); } }; template <> struct extract_value { static inline boost::optional do_extract_from_string(std::string const& source) { mapnik::value_integer result; if (mapnik::util::string2int(source, result)) return boost::optional(result); return boost::optional(); } }; #ifdef BIGINT template <> struct extract_value { static inline boost::optional do_extract_from_string(std::string const& source) { mapnik::value_integer result; if (mapnik::util::string2int(source, result)) return boost::optional(result); return boost::optional(); } }; #endif template <> struct extract_value { static inline boost::optional do_extract_from_string(std::string const& source) { mapnik::value_double result; if (mapnik::util::string2double(source, result)) return boost::optional(result); return boost::optional(); } }; template <> struct extract_value { static inline boost::optional do_extract_from_string(std::string const& source) { return boost::optional(); // FIXME } }; template <> struct extract_value { static inline boost::optional do_extract_from_string(std::string const& source) { return boost::optional(source); } }; template boost::optional param_cast(std::string const& source) { return extract_value::do_extract_from_string(source); } } // end namespace detail template struct value_extractor_visitor : public boost::static_visitor<> { value_extractor_visitor(boost::optional & var) :var_(var) {} void operator() (std::string const& val) const { var_ = detail::param_cast(val); } template void operator () (T1 val) const { // TODO try { var_ = boost::lexical_cast(val); } catch (boost::bad_lexical_cast & ) {} //std::string err_msg = (boost::format("No conversion from %s to %s") // % typeid(T1).name() // % typeid(T).name()).str(); //throw std::runtime_error(err_msg); } boost::optional & var_; }; namespace params_detail { template struct converter { typedef T value_type; 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)); } }