c++ - Make reordered tuple from vector of tuple with use spirit::karma -
c++ - Make reordered tuple from vector of tuple with use spirit::karma -
#include <tuple> #include <vector> #include <string> #include <iostream> //------------------------------------------------------------------------- #include <boost/spirit/include/karma.hpp> #include <boost/fusion/adapted/std_tuple.hpp> //------------------------------------------------------------------------- namespace ph = boost::phoenix; namespace karma = boost::spirit::karma; typedef std::back_insert_iterator<std::string> sink; typedef std::tuple<double,int> data; typedef std::vector<data> container; struct generator : karma::grammar<sink,container()> { generator(void) : generator::base_type(start,"generator") { start = info % karma::eol; //data = karma::delimit[???]; return; } karma::rule<sink,container()> start; karma::rule<sink,data()> data; }; //------------------------------------------------------------------------- int main(int argc,char** argv) { generator generator; container container; container.push_back(data(3.1415,100500)); container.push_back(data(2.7183,9000)); std::string result; sink sink(result); bool b = boost::spirit::karma::generate(sink,generator,container); std::cerr << (b == true ? result : std::string("error!")) << std::endl; homecoming 0; }
in rule data (as example) need generate int before double , create arithmetical operation. how can access elements of synthesized attribute (tuple) in semantic actions of data rule?
the quickest solution can come @ instant simply:
data = delimit [ int_ [ _1 = at_c<1>(_val) ] << double_ [ _1 = at_c<0>(_val) ] ];
so, total sample like:
#include <boost/spirit/include/karma.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/spirit/include/phoenix_operator.hpp> #include <boost/fusion/adapted.hpp> #include <boost/tuple/tuple.hpp> //------------------------------------------------------------------------- namespace ph = boost::phoenix; namespace karma = boost::spirit::karma; typedef std::back_insert_iterator<std::string> sink; typedef boost::tuple<double,int> data; typedef std::vector<data> container; struct generator : karma::grammar<sink,container()> { generator(void) : generator::base_type(start,"generator") { using namespace karma; using namespace ph; info = delimit [ int_ [ _1 = at_c<1>(_val) ] << double_ [ _1 = at_c<0>(_val) ] ]; start = info % eol; return; } karma::rule<sink,container()> start; karma::rule<sink,data()> data; }; //------------------------------------------------------------------------- int main(int argc,char** argv) { generator generator; container container; container.push_back(data(3.1415,100500)); container.push_back(data(2.7183,9000)); std::string result; sink sink(result); bool b = boost::spirit::karma::generate(sink,generator,container); std::cerr << (b == true ? result : std::string("error!")) << std::endl; homecoming 0; }
output:
100500 3.142 9000 2.718
c++ boost-spirit-karma
Comments
Post a Comment