refactor params interface to move lexical cast out of header

This commit is contained in:
Dane Springmeyer 2013-01-09 19:12:20 -08:00
parent eecab1b22b
commit bb43f2cec1
4 changed files with 159 additions and 77 deletions

View file

@ -21,6 +21,8 @@
#include "layer_info_dialog.hpp"
// mapnik
#include <mapnik/params.hpp>
#include <mapnik/params_impl.hpp>
#include <mapnik/layer.hpp>

View file

@ -23,12 +23,13 @@
#ifndef MAPNIK_PARAMS_HPP
#define MAPNIK_PARAMS_HPP
// mapnik
#include <mapnik/config.hpp>
#include <mapnik/value_types.hpp>
// boost
#include <boost/variant/variant.hpp>
#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>
// mapnik
#include <mapnik/value_types.hpp>
// stl
#include <string>
@ -36,45 +37,58 @@
namespace mapnik
{
// fwd declare
class boolean;
typedef boost::variant<value_null,value_integer,value_double,std::string> value_holder;
typedef std::pair<std::string, value_holder> parameter;
typedef std::map<std::string, value_holder> param_map;
// TODO - rewrite to avoid usage of lexical_cast
template <typename T>
struct value_extractor_visitor : public boost::static_visitor<>
{
value_extractor_visitor(boost::optional<T> & var)
:var_(var) {}
void operator () (T val) const
{
var_ = val;
}
template <typename T1>
void operator () (T1 val) const
{
try
{
var_ = boost::lexical_cast<T>(val);
}
catch (boost::bad_lexical_cast & ) {}
}
boost::optional<T> & var_;
};
class parameters : public param_map
class MAPNIK_DECL parameters : public param_map
{
public:
parameters();
parameters() {}
template <typename T>
boost::optional<T> get(std::string const& key) const;
template <typename T>
boost::optional<T> get(std::string const& key, T const& default_opt_value) const;
};
#ifdef _MSC_VER
template MAPNIK_DECL
boost::optional<std::string> parameters::get(std::string const& key) const;
template MAPNIK_DECL
boost::optional<std::string> parameters::get(std::string const& key,
std::string const& default_opt_value) const;
template MAPNIK_DECL
boost::optional<value_double> parameters::get(std::string const& key) const;
template MAPNIK_DECL
boost::optional<value_double> parameters::get(std::string const& key,
value_double const& default_opt_value) const;
template MAPNIK_DECL
boost::optional<int> parameters::get(std::string const& key) const;
template MAPNIK_DECL
boost::optional<int> parameters::get(std::string const& key,
int const& default_opt_value) const;
#ifdef BIGINT
template MAPNIK_DECL
boost::optional<value_integer> parameters::get(std::string const& key) const;
template MAPNIK_DECL
boost::optional<value_integer> parameters::get(std::string const& key,
value_integer const& default_opt_value) const;
#endif
template MAPNIK_DECL
boost::optional<mapnik::boolean> parameters::get(std::string const& key) const;
template MAPNIK_DECL
boost::optional<mapnik::boolean> parameters::get(std::string const& key,
mapnik::boolean const& default_opt_value) const;
#endif
}
#endif // MAPNIK_PARAMS_HPP

View file

@ -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 <mapnik/params.hpp>
#include <mapnik/value_types.hpp>
// boost
#include <boost/variant/variant.hpp>
#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>
// stl
#include <string>
namespace mapnik
{
template <typename T>
struct value_extractor_visitor : public boost::static_visitor<>
{
value_extractor_visitor(boost::optional<T> & var)
:var_(var) {}
void operator () (T val) const
{
var_ = val;
}
template <typename T1>
void operator () (T1 val) const
{
try
{
var_ = boost::lexical_cast<T>(val);
}
catch (boost::bad_lexical_cast & ) {}
}
boost::optional<T> & var_;
};
namespace params_detail {
template <typename T>
struct converter
{
typedef boost::optional<T> return_type;
static return_type extract(parameters const& params,
std::string const& name,
boost::optional<T> const& default_opt_value)
{
boost::optional<T> result(default_opt_value);
parameters::const_iterator itr = params.find(name);
if (itr != params.end())
{
boost::apply_visitor(value_extractor_visitor<T>(result),itr->second);
}
return result;
}
};
} // end namespace params_detail
template <typename T>
boost::optional<T> parameters::get(std::string const& key) const
{
return params_detail::converter<T>::extract(*this,key, boost::none);
}
template <typename T>
boost::optional<T> parameters::get(std::string const& key, T const& default_opt_value) const
{
return params_detail::converter<T>::extract(*this,key,boost::optional<T>(default_opt_value));
}
}

View file

@ -23,62 +23,26 @@
// mapnik
#include <mapnik/boolean.hpp>
#include <mapnik/params.hpp>
#include <mapnik/params_impl.hpp>
#include <mapnik/value_types.hpp>
// boost
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/none.hpp>
#include <boost/optional.hpp>
namespace mapnik {
namespace params_detail {
template boost::optional<std::string> parameters::get(std::string const& key) const;
template boost::optional<std::string> parameters::get(std::string const& key, std::string const& default_opt_value) const;
template <typename T>
struct converter
{
typedef boost::optional<T> return_type;
static return_type extract(parameters const& params,
std::string const& name,
boost::optional<T> const& default_opt_value)
{
boost::optional<T> result(default_opt_value);
parameters::const_iterator itr = params.find(name);
if (itr != params.end())
{
boost::apply_visitor(value_extractor_visitor<T>(result),itr->second);
}
return result;
}
};
} // end namespace params_detail
template boost::optional<value_double> parameters::get(std::string const& key) const;
template boost::optional<value_double> parameters::get(std::string const& key, value_double const& default_opt_value) const;
// parameters
template boost::optional<int> parameters::get(std::string const& key) const;
template boost::optional<int> parameters::get(std::string const& key, int const& default_opt_value) const;
parameters::parameters() {}
template boost::optional<mapnik::boolean> parameters::get(std::string const& key) const;
template boost::optional<mapnik::boolean> parameters::get(std::string const& key, mapnik::boolean const& default_opt_value) const;
template <typename T>
boost::optional<T> parameters::get(std::string const& key) const
{
return params_detail::converter<T>::extract(*this,key, boost::none);
}
template <typename T>
boost::optional<T> parameters::get(std::string const& key, T const& default_opt_value) const
{
return params_detail::converter<T>::extract(*this,key,boost::optional<T>(default_opt_value));
}
#define compile_params_get(T) template boost::optional<T> parameters::get(std::string const& key) const; template boost::optional<T> 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<value_integer> parameters::get(std::string const& key) const;
template boost::optional<value_integer> parameters::get(std::string const& key, value_integer const& default_opt_value) const;
#endif
compile_params_get(mapnik::boolean);
}