Reputation: 479
Starting with a trivial problem, I chose to use boost::spirit::karma
to turn it into something a little trickier. We have a map that maps entries of an enum class
to vector
s of int
s, I'd like to produce output of the form
Foo has 3 entries:
Foo has 1
Foo has 5
Foo has 7
...
if there is an entry {Foo, {1, 5, 7}}
in the data.
The data format isn't really that important, but if the key of the map were string
s or int
s, one could use karma::lit
to output the saved attribute again. However, this isn't possible, and the rule consumes an attribute, making it impossible to use it again.
I managed to extract the size of the vector with quite a hack, but I cannot get the name Foo
to be repeated.
#include <string>
#include <type_traits>
#include <vector>
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_symbols.hpp>
#include <boost/phoenix/bind/bind_member_variable.hpp>
#include <boost/fusion/include/define_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
enum class TableName {
Foo,
Bar
};
using Map = std::map<TableName, std::vector<int>>;
template<typename OutputIterator>
struct TestGrammar : boost::spirit::karma::grammar<OutputIterator, Map()>
{
TestGrammar() : TestGrammar::base_type(testRule)
{
using boost::spirit::karma::duplicate;
using boost::spirit::karma::int_;
using boost::spirit::karma::lit;
using boost::spirit::karma::_a;
using boost::spirit::karma::_1;
using boost::spirit::karma::_val;
using boost::spirit::karma::skip;
using boost::phoenix::ref;
using boost::phoenix::size;
tableNameSymbols.add
(TableName::Foo, "Foo")
(TableName::Bar, "Bar")
;
tableNameRule %= tableNameSymbols;
// Using _a instead of size_ or sym_ does not work... why?
pairRule %= tableNameRule[ref(sym_) = _1] <<
duplicate[
skip[*int_][ref(size_) = size(_1)] <<
lit(" has ") << lit(ref(size_)) << " entries:\n" <<
*(lit('\t') << /* tableNameRule[_1 = ref(sym_)] */ lit("IT") << lit(" has ") << int_ << lit('\n'))
];
testRule = *(pairRule << lit('\n'));
}
boost::spirit::karma::symbols<TableName, const char *> tableNameSymbols;
boost::spirit::karma::rule<OutputIterator, TableName()> tableNameRule;
boost::spirit::karma::rule<OutputIterator, std::pair<TableName, std::vector<int>>()> pairRule;
boost::spirit::karma::rule<OutputIterator, Map()> testRule;
private:
std::size_t size_;
TableName sym_;
};
int main()
{
using namespace boost::spirit::karma;
Map map = {
{TableName::Foo, {1, 5, 7}},
{TableName::Bar, {2}}
};
std::string output;
std::back_insert_iterator<std::string> sink(output);
/*
* The following outputs
Foo has 3 entries:
IT has 1
IT has 5
IT has 7
Bar has 1 entries:
IT has 2
It should output:
Foo has 3 entries:
Foo has 1
Foo has 5
Foo has 7
Bar has 1 entries:
Bar has 2
*/
bool r = generate(
sink,
TestGrammar<std::decay<decltype(sink)>::type>(),
map
);
std::cout << output;
return r == false;
}
Upvotes: 0
Views: 211