From 21487f36b9d91e98037a3b24bf7d348706c19731 Mon Sep 17 00:00:00 2001 From: artemp Date: Fri, 6 May 2016 10:15:44 +0200 Subject: [PATCH] json - refactor stringifier and attribute_value_visitor into separate *.hpp and share between geojson and topojson grammars --- .../mapnik/json/attribute_value_visitor.hpp | 68 +++++++++++ include/mapnik/json/feature_grammar.hpp | 106 +----------------- include/mapnik/json/stringifier.hpp | 100 +++++++++++++++++ include/mapnik/json/topojson_utils.hpp | 32 +----- 4 files changed, 173 insertions(+), 133 deletions(-) create mode 100644 include/mapnik/json/attribute_value_visitor.hpp create mode 100644 include/mapnik/json/stringifier.hpp diff --git a/include/mapnik/json/attribute_value_visitor.hpp b/include/mapnik/json/attribute_value_visitor.hpp new file mode 100644 index 000000000..dd27495ca --- /dev/null +++ b/include/mapnik/json/attribute_value_visitor.hpp @@ -0,0 +1,68 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2016 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 MAPNIK_JSON_ATTRIBUTE_VALUE_VISITOR_HPP +#define MAPNIK_JSON_ATTRIBUTE_VALUE_VISITOR_HPP + +// mapnik +#include +#include +#include +#include + +namespace mapnik { namespace json { + +struct attribute_value_visitor +{ +public: + attribute_value_visitor(mapnik::transcoder const& tr) + : tr_(tr) {} + + mapnik::value operator()(std::string const& val) const + { + return mapnik::value(tr_.transcode(val.c_str())); + } + + mapnik::value operator()(std::vector const& array) const + { + std::string str = stringifier()(array); + return mapnik::value(tr_.transcode(str.c_str())); + } + + mapnik::value operator()(std::unordered_map const& object) const + { + std::string str = stringifier()(object); + return mapnik::value(tr_.transcode(str.c_str())); + } + + template + mapnik::value operator()(T const& val) const + { + return mapnik::value(val); + } + + mapnik::transcoder const& tr_; +}; + +}} + +#endif //MAPNIK_JSON_ATTRIBUTE_VALUE_VISITOR_HPP diff --git a/include/mapnik/json/feature_grammar.hpp b/include/mapnik/json/feature_grammar.hpp index e63052eb5..699ec907a 100644 --- a/include/mapnik/json/feature_grammar.hpp +++ b/include/mapnik/json/feature_grammar.hpp @@ -24,15 +24,10 @@ #define MAPNIK_FEATURE_GRAMMAR_HPP // mapnik -#include #include #include -#include -#include -#include -#include -#include - +#include +#include #pragma GCC diagnostic push #include #include @@ -46,103 +41,6 @@ namespace qi = boost::spirit::qi; namespace phoenix = boost::phoenix; namespace fusion = boost::fusion; -namespace { -struct stringifier -{ - std::string operator()(std::string const& val) const - { - return "\"" + val + "\""; - } - - std::string operator()(value_null) const - { - return "null"; - } - - std::string operator()(value_bool val) const - { - return val ? "true" : "false"; - } - - std::string operator()(value_integer val) const - { - std::string str; - util::to_string(str, val); - return str; - } - - std::string operator()(value_double val) const - { - std::string str; - util::to_string(str, val); - return str; - } - - std::string operator()(std::vector const& array) const - { - std::string str = "["; - bool first = true; - for (auto const& val : array) - { - if (first) first = false; - else str += ","; - str += mapnik::util::apply_visitor(*this, val); - } - str += "]"; - return str; - } - - std::string operator()(std::unordered_map const& object) const - { - std::string str = "{"; - bool first = true; - for (auto const& kv : object) - { - if (first) first = false; - else str += ","; - str += kv.first; - str += ":"; - str += mapnik::util::apply_visitor(*this, kv.second); - } - str += "}"; - return str; - } -}; - -} // anonymous ns - -struct attribute_value_visitor -{ -public: - attribute_value_visitor(mapnik::transcoder const& tr) - : tr_(tr) {} - - mapnik::value operator()(std::string const& val) const - { - return mapnik::value(tr_.transcode(val.c_str())); - } - - mapnik::value operator()(std::vector const& array) const - { - std::string str = stringifier()(array); - return mapnik::value(tr_.transcode(str.c_str())); - } - - mapnik::value operator()(std::unordered_map const& object) const - { - std::string str = stringifier()(object); - return mapnik::value(tr_.transcode(str.c_str())); - } - - template - mapnik::value operator()(T const& val) const - { - return mapnik::value(val); - } - - mapnik::transcoder const& tr_; -}; - struct put_property { using result_type = void; diff --git a/include/mapnik/json/stringifier.hpp b/include/mapnik/json/stringifier.hpp new file mode 100644 index 000000000..f7d18493a --- /dev/null +++ b/include/mapnik/json/stringifier.hpp @@ -0,0 +1,100 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2016 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 MAPNIK_JSON_STRINGIFIER_HPP +#define MAPNIK_JSON_STRINGIFIER_HPP + +// mapnik +#include +#include +// stl +#include + + +namespace mapnik { namespace json { + +struct stringifier +{ + std::string operator()(std::string const& val) const + { + return "\"" + val + "\""; + } + + std::string operator()(value_null) const + { + return "null"; + } + + std::string operator()(value_bool val) const + { + return val ? "true" : "false"; + } + + std::string operator()(value_integer val) const + { + std::string str; + util::to_string(str, val); + return str; + } + + std::string operator()(value_double val) const + { + std::string str; + util::to_string(str, val); + return str; + } + + std::string operator()(std::vector const& array) const + { + std::string str = "["; + bool first = true; + for (auto const& val : array) + { + if (first) first = false; + else str += ","; + str += mapnik::util::apply_visitor(*this, val); + } + str += "]"; + return str; + } + + std::string operator()(std::unordered_map const& object) const + { + std::string str = "{"; + bool first = true; + for (auto const& kv : object) + { + if (first) first = false; + else str += ","; + str += kv.first; + str += ":"; + str += mapnik::util::apply_visitor(*this, kv.second); + } + str += "}"; + return str; + } +}; + +}} + + +#endif // MAPNIK_JSON_STRINGIFIER_HPP diff --git a/include/mapnik/json/topojson_utils.hpp b/include/mapnik/json/topojson_utils.hpp index 9e132a168..0402bb687 100644 --- a/include/mapnik/json/topojson_utils.hpp +++ b/include/mapnik/json/topojson_utils.hpp @@ -2,7 +2,7 @@ * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2015 Artem Pavlenko + * Copyright (C) 2016 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -246,33 +247,6 @@ private: }; namespace { -struct attribute_value_visitor - -{ -public: - attribute_value_visitor(mapnik::transcoder const& tr) - : tr_(tr) {} - - mapnik::value operator()(std::string const& val) const - { - return mapnik::value(tr_.transcode(val.c_str())); - } - mapnik::value operator()(std::vector const& arr) const - { - return mapnik::value(tr_.transcode("FAIL ARRAY")); - } - mapnik::value operator()(std::unordered_map const& obj) const - { - return mapnik::value(tr_.transcode("FAIL OBJECT")); - } - template - mapnik::value operator()(T const& val) const - { - return mapnik::value(val); - } - - mapnik::transcoder const& tr_; -}; template void assign_properties(mapnik::feature_impl & feature, T const& geom, mapnik::transcoder const& tr) @@ -281,7 +255,7 @@ void assign_properties(mapnik::feature_impl & feature, T const& geom, mapnik::tr { for (auto const& p : *geom.props) { - feature.put_new(std::get<0>(p), mapnik::util::apply_visitor(attribute_value_visitor(tr),std::get<1>(p))); + feature.put_new(std::get<0>(p), mapnik::util::apply_visitor(mapnik::json::attribute_value_visitor(tr),std::get<1>(p))); } } }