精神业力:访问不可复制的指针

时间:2013-03-16 14:05:25

标签: c++ boost boost-spirit boost-spirit-karma

我正试图在我的业力语法中访问一个指针向量,但收效甚微。指针类型是不可复制的,因此使用它的规则必须采用引用:

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

此编译但返回:测试2规则:1,测试1规则:25104656,测试1规则:25104720 ,依此类推。我知道在这个简单的例子中我可以做到

t1r = "test 1 rule: " << karma::int_[karma::_1 = phx::bind(&test1::value, *karma::_val)];

要解决它,但实际上 value 是另一种不可复制的类型,它可以传递给语法,因此我需要使用结构自适应,就像在示例中所做的那样。

我也注意到here提到的custimisation点deref_iterator,但是,我在模板库上工作,并且不认为可以使用依赖于模板的类型来专门化deref_iterator。

有关如何使示例工作的任何想法?

1 个答案:

答案 0 :(得分:1)

llonesmiz在评论中回答了我的问题,所以我发布他的内部以供将来参考。使用自定义点转换属性可以解决问题:

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