Question

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?

Was it helpful?

Solution

llonesmiz answerd my question in the comments, so I post his insides for future references. Using the customisation point transform attribute the problem can be solved like this:

#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;

template <typename Value1, typename Value2>
struct test1 : boost::noncopyable {
  test1(Value1 i = Value1(), Value2 j = Value2() ): value1(i),value2(j) {}
  Value1 value1;
  Value2 value2;
};

struct test2 : boost::noncopyable {
  int value;
  std::vector<test1<int,double>*> vector;
};

BOOST_FUSION_ADAPT_TPL_STRUCT(
(Value1)(Value2),
(test1) (Value1)(Value2),
(Value1, value1)
(Value2, value2))

typedef std::vector<test1<int,double>*> test1_vector;

BOOST_FUSION_ADAPT_STRUCT( test2, (int, value) (test1_vector, vector) )

typedef std::ostream_iterator<char> Iterator;

namespace boost { namespace spirit { namespace traits
{
template <typename Value1, typename Value2>
struct transform_attribute<test1<Value1,Value2>* const, test1<Value1,Value2>&, karma::domain>
{
    typedef test1<Value1,Value2>& type;
    static type pre(test1<Value1,Value2>* const& val) 
    { 
      return *val; 
    }
};
}}}

int main() {

  karma::rule<Iterator, test1<int,double>*()> t1r;
  karma::rule<Iterator, test2&()> t2r;

  t2r %= "test 2 rule:" << karma::int_ << karma::eol << (t1r % karma::eol);
  t1r %= "test 1 rule: " << karma::attr_cast<test1<int,double>*,test1<int,double>&>(karma::delimit(karma::space)[karma::int_<< karma::double_]);

  std::stringstream stream;
  std::ostream_iterator<char> out(stream);

  test2 t;
  t.vector.push_back(new test1<int,double>(2));
  t.vector.push_back(new test1<int,double>(3));
  t.vector.push_back(new test1<int,double>(4));
  t.vector.push_back(new test1<int,double>(5));
  t.value = 1;

  karma::generate(out, t2r, t);      
  std::cout<<stream.str()<<std::endl;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top