json feature_generator_grammar - re-factor properties_generator_grammar into separate compilation unit - ref #2546
This commit is contained in:
parent
dc8253ec2a
commit
f1373883c2
5 changed files with 198 additions and 132 deletions
|
@ -24,23 +24,13 @@
|
|||
#define MAPNIK_JSON_FEATURE_GENERATOR_GRAMMAR_HPP
|
||||
|
||||
// mapnik
|
||||
#include <mapnik/value_types.hpp>
|
||||
#include <mapnik/value.hpp>
|
||||
#include <mapnik/feature.hpp>
|
||||
#include <mapnik/geometry_container.hpp>
|
||||
#include <mapnik/json/geometry_generator_grammar.hpp>
|
||||
#include <mapnik/feature_kv_iterator.hpp>
|
||||
#include <mapnik/json/properties_generator_grammar.hpp>
|
||||
|
||||
// boost
|
||||
#include <boost/spirit/include/karma.hpp>
|
||||
#include <boost/spirit/include/phoenix.hpp>
|
||||
#include <boost/spirit/include/phoenix_function.hpp>
|
||||
#include <boost/spirit/include/phoenix_fusion.hpp>
|
||||
#include <boost/spirit/include/phoenix_core.hpp>
|
||||
#include <boost/spirit/include/phoenix_operator.hpp>
|
||||
#include <boost/fusion/adapted/std_tuple.hpp>
|
||||
#include <boost/fusion/include/at.hpp>
|
||||
#include <boost/fusion/include/cons.hpp>
|
||||
|
||||
namespace boost { namespace spirit { namespace traits {
|
||||
|
||||
|
@ -101,73 +91,15 @@ struct get_id
|
|||
}
|
||||
};
|
||||
|
||||
struct make_properties_range
|
||||
{
|
||||
using properties_range_type = boost::iterator_range<mapnik::feature_kv_iterator>;
|
||||
|
||||
template <typename T>
|
||||
struct result { using type = properties_range_type; };
|
||||
|
||||
properties_range_type operator() (mapnik::feature_impl const& f) const
|
||||
{
|
||||
return boost::make_iterator_range(f.begin(),f.end());
|
||||
}
|
||||
};
|
||||
|
||||
struct utf8
|
||||
{
|
||||
template <typename T>
|
||||
struct result { using type = std::string; };
|
||||
|
||||
std::string operator() (mapnik::value_unicode_string const& ustr) const
|
||||
{
|
||||
std::string result;
|
||||
to_utf8(ustr,result);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename OutputIterator>
|
||||
struct escaped_string
|
||||
: karma::grammar<OutputIterator, std::string(char const*)>
|
||||
{
|
||||
escaped_string();
|
||||
karma::rule<OutputIterator, std::string(char const*)> esc_str;
|
||||
karma::symbols<char, char const*> esc_char;
|
||||
};
|
||||
|
||||
struct extract_string
|
||||
{
|
||||
using result_type = std::tuple<std::string,bool>;
|
||||
|
||||
result_type operator() (mapnik::value const& val) const
|
||||
{
|
||||
bool need_quotes = val.is<value_unicode_string>();
|
||||
return std::make_tuple(val.to_string(), need_quotes);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename OutputIterator>
|
||||
struct feature_generator_grammar:
|
||||
struct feature_generator_grammar :
|
||||
karma::grammar<OutputIterator, mapnik::feature_impl const&()>
|
||||
{
|
||||
using pair_type = std::tuple<std::string, mapnik::value>;
|
||||
using range_type = make_properties_range::properties_range_type;
|
||||
|
||||
feature_generator_grammar();
|
||||
karma::rule<OutputIterator, mapnik::feature_impl const&()> feature;
|
||||
multi_geometry_generator_grammar<OutputIterator, mapnik::geometry_container> geometry;
|
||||
escaped_string<OutputIterator> escaped_string_;
|
||||
karma::rule<OutputIterator, mapnik::feature_impl const&()> properties;
|
||||
karma::rule<OutputIterator, pair_type()> pair;
|
||||
karma::rule<OutputIterator, std::tuple<std::string,bool>()> value;
|
||||
karma::rule<OutputIterator, mapnik::value_null()> value_null_;
|
||||
karma::rule<OutputIterator, mapnik::value_unicode_string()> ustring;
|
||||
typename karma::int_generator<mapnik::value_integer,10, false> int__;
|
||||
properties_generator_grammar<OutputIterator, mapnik::feature_impl> properties;
|
||||
boost::phoenix::function<get_id> id_;
|
||||
boost::phoenix::function<extract_string> extract_string_;
|
||||
boost::phoenix::function<utf8> utf8_;
|
||||
std::string quote_;
|
||||
};
|
||||
|
||||
}}
|
||||
|
|
|
@ -29,50 +29,14 @@
|
|||
|
||||
namespace mapnik { namespace json {
|
||||
|
||||
namespace karma = boost::spirit::karma;
|
||||
|
||||
template <typename OutputIterator>
|
||||
escaped_string<OutputIterator>::escaped_string()
|
||||
: escaped_string::base_type(esc_str)
|
||||
{
|
||||
karma::lit_type lit;
|
||||
karma::_r1_type _r1;
|
||||
karma::hex_type hex;
|
||||
karma::right_align_type right_align;
|
||||
karma::print_type kprint;
|
||||
esc_char.add
|
||||
('"', "\\\"")
|
||||
('\\', "\\\\")
|
||||
('\b', "\\b")
|
||||
('\f', "\\f")
|
||||
('\n', "\\n")
|
||||
('\r', "\\r")
|
||||
('\t', "\\t")
|
||||
;
|
||||
|
||||
esc_str = lit(_r1)
|
||||
<< *(esc_char
|
||||
| kprint
|
||||
| "\\u" << right_align(4,lit('0'))[hex])
|
||||
<< lit(_r1)
|
||||
;
|
||||
}
|
||||
|
||||
template <typename OutputIterator>
|
||||
feature_generator_grammar<OutputIterator>::feature_generator_grammar()
|
||||
: feature_generator_grammar::base_type(feature),
|
||||
quote_("\"")
|
||||
: feature_generator_grammar::base_type(feature)
|
||||
{
|
||||
boost::spirit::karma::lit_type lit;
|
||||
boost::spirit::karma::uint_type uint_;
|
||||
boost::spirit::karma::bool_type bool_;
|
||||
boost::spirit::karma::double_type double_;
|
||||
boost::spirit::karma::_val_type _val;
|
||||
boost::spirit::karma::_1_type _1;
|
||||
boost::spirit::karma::string_type kstring;
|
||||
boost::spirit::karma::eps_type eps;
|
||||
|
||||
using boost::phoenix::at_c;
|
||||
|
||||
feature = lit("{\"type\":\"Feature\",\"id\":")
|
||||
<< uint_[_1 = id_(_val)]
|
||||
|
@ -80,30 +44,6 @@ feature_generator_grammar<OutputIterator>::feature_generator_grammar()
|
|||
<< lit(",\"properties\":") << properties
|
||||
<< lit('}')
|
||||
;
|
||||
|
||||
properties = lit('{')
|
||||
<< -(pair % lit(','))
|
||||
<< lit('}')
|
||||
;
|
||||
|
||||
pair = lit('"')
|
||||
<< kstring[_1 = boost::phoenix::at_c<0>(_val)] << lit('"')
|
||||
<< lit(':')
|
||||
<< value[_1 = extract_string_(at_c<1>(_val))]
|
||||
;
|
||||
|
||||
value = eps(at_c<1>(_val)) << escaped_string_(quote_.c_str())[_1 = at_c<0>(_val)]
|
||||
|
|
||||
kstring[_1 = at_c<0>(_val)]
|
||||
;
|
||||
|
||||
// FIXME http://boost-spirit.com/home/articles/karma-examples/creating-your-own-generator-component-for-spirit-karma/
|
||||
//value = (value_null_| bool_ | int__ | double_ | ustring)//[_1 = value_base_(_r1)]
|
||||
// ;
|
||||
//value_null_ = kstring[_1 = "null"]
|
||||
// ;
|
||||
//ustring = escaped_string_(quote_.c_str())[_1 = utf8_(_val)]
|
||||
// ;
|
||||
}
|
||||
|
||||
}}
|
||||
|
|
95
include/mapnik/json/properties_generator_grammar.hpp
Normal file
95
include/mapnik/json/properties_generator_grammar.hpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*****************************************************************************
|
||||
*
|
||||
* This file is part of Mapnik (c++ mapping toolkit)
|
||||
*
|
||||
* Copyright (C) 2014 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_PROPERTIES_GENERATOR_GRAMMAR_HPP
|
||||
#define MAPNIK_JSON_PROPERTIES_GENERATOR_GRAMMAR_HPP
|
||||
|
||||
#include <mapnik/value_types.hpp>
|
||||
#include <mapnik/value.hpp>
|
||||
#include <mapnik/feature_kv_iterator.hpp>
|
||||
|
||||
// boost
|
||||
#include <boost/spirit/include/karma.hpp>
|
||||
#include <boost/spirit/include/phoenix.hpp>
|
||||
#include <boost/spirit/include/phoenix_function.hpp>
|
||||
#include <boost/spirit/include/phoenix_fusion.hpp>
|
||||
#include <boost/spirit/include/phoenix_core.hpp>
|
||||
#include <boost/spirit/include/phoenix_operator.hpp>
|
||||
#include <boost/fusion/adapted/std_tuple.hpp>
|
||||
#include <boost/fusion/include/at.hpp>
|
||||
#include <boost/fusion/include/cons.hpp>
|
||||
|
||||
namespace mapnik { namespace json {
|
||||
|
||||
template <typename OutputIterator>
|
||||
struct escaped_string
|
||||
: karma::grammar<OutputIterator, std::string(char const*)>
|
||||
{
|
||||
escaped_string();
|
||||
karma::rule<OutputIterator, std::string(char const*)> esc_str;
|
||||
karma::symbols<char, char const*> esc_char;
|
||||
};
|
||||
|
||||
struct extract_string
|
||||
{
|
||||
using result_type = std::tuple<std::string,bool>;
|
||||
|
||||
result_type operator() (mapnik::value const& val) const
|
||||
{
|
||||
bool need_quotes = val.is<value_unicode_string>();
|
||||
return std::make_tuple(val.to_string(), need_quotes);
|
||||
}
|
||||
};
|
||||
|
||||
struct utf8
|
||||
{
|
||||
using result_type = std::string;
|
||||
std::string operator() (mapnik::value_unicode_string const& ustr) const
|
||||
{
|
||||
std::string result;
|
||||
to_utf8(ustr,result);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename OutputIterator, typename KeyValueStore>
|
||||
struct properties_generator_grammar : karma::grammar<OutputIterator, KeyValueStore const&()>
|
||||
|
||||
{
|
||||
using pair_type = std::tuple<std::string, mapnik::value>;
|
||||
properties_generator_grammar();
|
||||
// rules
|
||||
karma::rule<OutputIterator, KeyValueStore const&()> properties;
|
||||
karma::rule<OutputIterator, pair_type()> pair;
|
||||
karma::rule<OutputIterator, std::tuple<std::string,bool>()> value;
|
||||
karma::rule<OutputIterator, mapnik::value_null()> value_null_;
|
||||
karma::rule<OutputIterator, mapnik::value_unicode_string()> ustring;
|
||||
escaped_string<OutputIterator> escaped_string_;
|
||||
typename karma::int_generator<mapnik::value_integer,10, false> int__;
|
||||
boost::phoenix::function<extract_string> extract_string_;
|
||||
boost::phoenix::function<utf8> utf8_;
|
||||
std::string quote_;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif //MAPNIK_JSON_PROPERTIES_GENERATOR_GRAMMAR_HPP
|
96
include/mapnik/json/properties_generator_grammar_impl.hpp
Normal file
96
include/mapnik/json/properties_generator_grammar_impl.hpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*****************************************************************************
|
||||
*
|
||||
* This file is part of Mapnik (c++ mapping toolkit)
|
||||
*
|
||||
* Copyright (C) 2014 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
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
|
||||
#include <mapnik/json/properties_generator_grammar.hpp>
|
||||
|
||||
namespace mapnik { namespace json {
|
||||
|
||||
namespace karma = boost::spirit::karma;
|
||||
|
||||
template <typename OutputIterator>
|
||||
escaped_string<OutputIterator>::escaped_string()
|
||||
: escaped_string::base_type(esc_str)
|
||||
{
|
||||
karma::lit_type lit;
|
||||
karma::_r1_type _r1;
|
||||
karma::hex_type hex;
|
||||
karma::right_align_type right_align;
|
||||
karma::print_type kprint;
|
||||
|
||||
esc_char.add
|
||||
('"', "\\\"")
|
||||
('\\', "\\\\")
|
||||
('\b', "\\b")
|
||||
('\f', "\\f")
|
||||
('\n', "\\n")
|
||||
('\r', "\\r")
|
||||
('\t', "\\t")
|
||||
;
|
||||
|
||||
esc_str = lit(_r1)
|
||||
<< *(esc_char
|
||||
| kprint
|
||||
| "\\u" << right_align(4,lit('0'))[hex])
|
||||
<< lit(_r1)
|
||||
;
|
||||
}
|
||||
|
||||
template <typename OutputIterator, typename KeyValueStore>
|
||||
properties_generator_grammar<OutputIterator,KeyValueStore>::properties_generator_grammar()
|
||||
: properties_generator_grammar::base_type(properties),
|
||||
quote_("\"")
|
||||
{
|
||||
|
||||
boost::spirit::karma::lit_type lit;
|
||||
boost::spirit::karma::_val_type _val;
|
||||
boost::spirit::karma::_1_type _1;
|
||||
boost::spirit::karma::string_type kstring;
|
||||
boost::spirit::karma::eps_type eps;
|
||||
using boost::phoenix::at_c;
|
||||
|
||||
properties = lit('{')
|
||||
<< -(pair % lit(','))
|
||||
<< lit('}')
|
||||
;
|
||||
|
||||
pair = lit('"')
|
||||
<< kstring[_1 = boost::phoenix::at_c<0>(_val)] << lit('"')
|
||||
<< lit(':')
|
||||
<< value[_1 = extract_string_(at_c<1>(_val))]
|
||||
;
|
||||
|
||||
value = eps(at_c<1>(_val)) << escaped_string_(quote_.c_str())[_1 = at_c<0>(_val)]
|
||||
|
|
||||
kstring[_1 = at_c<0>(_val)]
|
||||
;
|
||||
|
||||
// FIXME http://boost-spirit.com/home/articles/karma-examples/creating-your-own-generator-component-for-spirit-karma/
|
||||
//value = (value_null_| bool_ | int__ | double_ | ustring)//[_1 = value_base_(_r1)]
|
||||
// ;
|
||||
//value_null_ = kstring[_1 = "null"]
|
||||
// ;
|
||||
//ustring = escaped_string_(quote_.c_str())[_1 = utf8_(_val)]
|
||||
// ;
|
||||
}
|
||||
|
||||
}}
|
|
@ -24,9 +24,12 @@
|
|||
#include <mapnik/geometry_container.hpp>
|
||||
#include <mapnik/json/feature_generator_grammar_impl.hpp>
|
||||
#include <mapnik/json/geometry_generator_grammar_impl.hpp>
|
||||
#include <mapnik/json/properties_generator_grammar_impl.hpp>
|
||||
#include <string>
|
||||
|
||||
using sink_type = std::back_insert_iterator<std::string>;
|
||||
|
||||
template struct mapnik::json::properties_generator_grammar<sink_type, mapnik::feature_impl>;
|
||||
template struct mapnik::json::feature_generator_grammar<sink_type>;
|
||||
template struct mapnik::json::geometry_generator_grammar<sink_type, mapnik::geometry_type>;
|
||||
template struct mapnik::json::multi_geometry_generator_grammar<sink_type, mapnik::geometry_container>;
|
||||
|
|
Loading…
Reference in a new issue