From 145f83b806e6dbb83cf1aa219f31bd1d0016c0ca Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Thu, 19 Apr 2012 13:35:41 +0100 Subject: [PATCH] boost::spirit::karma based to_string impl --- include/mapnik/util/conversions.hpp | 45 ++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/include/mapnik/util/conversions.hpp b/include/mapnik/util/conversions.hpp index b57f55ff9..76752bd9c 100644 --- a/include/mapnik/util/conversions.hpp +++ b/include/mapnik/util/conversions.hpp @@ -23,21 +23,50 @@ #ifndef MAPNIK_UTIL_CONVERSIONS_HPP #define MAPNIK_UTIL_CONVERSIONS_HPP -// mapnik - // stl #include +// boost +#include +#include namespace mapnik { namespace util { - bool string2int(const char * value, int & result); - bool string2int(std::string const& value, int & result); +bool string2int(const char * value, int & result); +bool string2int(std::string const& value, int & result); - bool string2double(std::string const& value, double & result); - bool string2double(const char * value, double & result); +bool string2double(std::string const& value, double & result); +bool string2double(const char * value, double & result); - bool string2float(std::string const& value, float & result); - bool string2float(const char * value, float & result); +bool string2float(std::string const& value, float & result); +bool string2float(const char * value, float & result); + +// generic +template +bool to_string(std::string & str, T value) +{ + namespace karma = boost::spirit::karma; + std::back_insert_iterator sink(str); + return karma::generate(sink, value); +} + +template +struct double_policy : boost::spirit::karma::real_policies +{ + typedef boost::spirit::karma::real_policies base_type; + static int floatfield(T n) { return base_type::fmtflags::fixed; } + static unsigned precision(T n) { return 16 ;} +}; + + +// specialisation for double +template <> +inline bool to_string(std::string & str, double value) +{ + namespace karma = boost::spirit::karma; + typedef boost::spirit::karma::real_generator > double_type; + std::back_insert_iterator sink(str); + return karma::generate(sink, double_type(), value); +} }}