I'm trying to access a vector of pointers in my karma grammar with little success. The pointer type is noncopyable, therefore the rule using it has to take a reference:
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace karma = boost::spirit::karma;
namespace fusion = boost::fusion;
namespace phx = boost::phoenix;
struct test1 : boost::noncopyable {
test1(int i = 0) : value(i) {}
int value;
};
struct test2 : boost::noncopyable {
int value;
std::vector<test1*> vector;
};
BOOST_FUSION_ADAPT_STRUCT( test1, (int, value) );
BOOST_FUSION_ADAPT_STRUCT( test2, (int, value) (std::vector<test1*>, vector) );
typedef std::ostream_iterator<char> Iterator;
int main() {
karma::rule<Iterator, test1*()> t1r;
karma::rule<Iterator, test2&()> t2r;
t2r %= "test 2 rule:" << karma::int_ << karma::eol << (t1r % karma::eol);
t1r %= "test 1 rule: " << karma::int_;
std::stringstream stream;
std::ostream_iterator<char> out(stream);
test2 t;
t.vector.push_back(new test1(2));
t.vector.push_back(new test1(3));
t.vector.push_back(new test1(4));
t.vector.push_back(new test1(5));
t.value = 1;
karma::generate(out, t2r, t);
std::cout<<stream.str()<<std::endl;
}
This compiles but returns: test 2 rule:1, test 1 rule: 25104656, test 1 rule: 25104720 and so on. I know that in this simple case I could do
t1r = "test 1 rule: " << karma::int_[karma::_1 = phx::bind(&test1::value, *karma::_val)];
to solve it, but in reality value is another noncopyable type which sould be passed to a grammar and I therefore need to use the struct adaption as done in the example.
I'm also aware of the custimisation point deref_iterator as mentioned here , however, I work on a template library and don't think it is possible to specialize deref_iterator with a template dependent type.
Any ideas on how to make the example work?