#include #include #include #include #include #include #include #include #include #include #include #include #include struct test { boost::uint32_t a; boost::uint32_t b; }; BOOST_FUSION_ADAPT_STRUCT ( test, (boost::uint32_t, a) (boost::uint32_t, b) ) template struct test_generator: boost::spirit::karma::grammar { test_generator(): test_generator::base_type(start) { using boost::spirit::big_dword; start = ( big_dword << big_dword ) ; } boost::spirit::karma::rule start; }; template struct test_parser: boost::spirit::qi::grammar { test_parser(): test_parser::base_type(start) { using boost::spirit::big_dword; using boost::spirit::_1; using boost::spirit::_val; using boost::phoenix::ref; using boost::phoenix::cref; boost::uint32_t temp; start = ( big_dword >> //big_dword // Ok. big_dword[ref(temp) = _1] // Fail. ) ; } boost::spirit::qi::rule start; }; int main() { typedef boost::spirit::multi_pass< std::istreambuf_iterator > input_iterator_type; typedef std::ostream_iterator output_iterator_type; std::string filename("struct.bin"); std::ofstream out(filename.c_str(), std::ios::binary | std::ios::trunc); if(!out) { std::cerr << "Failed to open output file\n"; return 1; } typedef test_generator test_generator; test_generator pg; test c1 = {1, 2}; output_iterator_type oi(out); bool result = boost::spirit::karma::generate(oi, pg, c1); out.close(); std::ifstream in(filename.c_str(), std::ios::binary); if(!in) { std::cerr << "Failed to open input file\n"; return 1; } input_iterator_type first = input_iterator_type(in); input_iterator_type last = input_iterator_type(); typedef test_parser test_parser; test_parser pp; test c2; bool r = parse(first, last, pp, c2); if(!r || first != last) { std::string rest(first, last); std::cerr << "Failed parsing at: 0x" << std::hex << (int)rest[0] << std::dec << std::endl; in.close(); return 1; } std::cout << c2.a << std::endl; std::cout << c2.b << std::endl; assert(c1.a == c2.a); assert(c1.b == c2.b); in.close(); return 0; }