2016-10-11 2 views
1

나는 google :: protobuf를 생성해야하는 boost :: spirit 파서를 출력 할 때 생성 된 클래스를 구현했습니다.Spirit :: Qi : google-protobuf에 대한 바인딩 추가

나는 page을 배경으로 애썼다. 불행히도 google :: protobuf 생성 클래스는 set/get 메서드 만 제공하기 때문에 특성 문법을 사용할 수 없습니다. 그래서, 난 부스트 :: 피닉스와 결합 defered 시도하지만 클래스 A에서 add_param() 메소드를 결합 (코멘트와 함께 아래의 코드 라인 99 참조)하는 방법을 잘 모르겠어요 :

#define BOOST_SPIRIT_DEBUG 

#include <string> 
#include <iostream> 
#include <sstream> 

#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/include/phoenix.hpp> 
#include <boost/fusion/include/std_pair.hpp> 

namespace qi = boost::spirit::qi; 
namespace phx = boost::phoenix; 

class A 
{ 
public: 
    A(): _i(0) {} 
    A(int i) : _i(i) 
    { 
     std::cout << "i=" << i << std::endl; 
    } 

    void set_i(int i) 
    { 
     _i = i; 
    } 

    void add_param(const std::string& value) 
    { 
     _params.push_back(value); 
    } 

    std::string to_string() const 
    { 
     std::stringstream ss; 
     ss << "_i=[" << _i << "], _params=["; 
     for (auto& p : _params) 
      ss << p << ","; 
     ss << "]" << std::endl; 
     return ss.str(); 
    } 

private: 
    int _i; 
    std::vector <std::string> _params; 
}; 

class B 
{ 
public: 
    B() : _id(), _av() {} 
    B(int id, const std::vector<A>& av) : _id(id), _av(av) {} 

    void set_id(int id) 
    { 
     _id = id; 
    } 

    void add_A(const A& a) 
    { 
     _av.push_back(a); 
    } 

    std::string to_string() const 
    { 
     std::stringstream ss; 
     ss << "_id=[" << _id << "], _av=["; 
     for (auto& av : _av) 
      ss << av.to_string() << ","; 
     ss << "]" << std::endl; 
     return ss.str(); 
    } 
private: 
    int _id; 
    std::vector<A> _av; 
}; 

namespace Utils 
{ 
    int mul2(int i) 
    { 
     return i * 2; 
    } 
} 

template <typename Iterator, typename Skipper> 
struct grammar_B: qi::grammar<Iterator, Skipper> 
{ 
    grammar_B(B& ctx) : grammar_B::base_type(start) 
    { 
     // A 
     i_rule = qi::int_[ qi::_val = phx::bind(Utils::mul2, qi::_1) ]; 
     param_rule = *(qi::char_ - ',' - '!'); 

     a_rule = (qi::lit("$") 
        >> i_rule 
        >> qi::lit(":") 
        >> *(param_rule >> +(qi::lit(","))) // how to bind to A::add_param() ? 
        >> qi::lit("!")) [ qi::_val = phx::construct<A>(qi::_1) ]; 

     // B 
     id_rule = qi::int_[ phx::bind(&B::set_id, phx::ref(ctx), qi::_1) ];  

     start %= id_rule >> qi::lit("=") >> a_rule; 

     BOOST_SPIRIT_DEBUG_NODE(id_rule); 
     BOOST_SPIRIT_DEBUG_NODE(i_rule); 
     BOOST_SPIRIT_DEBUG_NODE(param_rule); 
    } 
private: 
    qi::rule<Iterator, Skipper> start; 

    qi::rule<Iterator, int, Skipper> i_rule; 
    qi::rule<Iterator, std::string(), Skipper> param_rule; 
    qi::rule<Iterator, A(), Skipper> a_rule; 

    qi::rule<Iterator, int, Skipper> id_rule; 
}; 

int main(int argc, char* argv[]) 
{ 

    std::string input = "1=$100:param1,param2!"; 
    B ctx; 
    grammar_B<decltype(std::begin(input)), qi::space_type> p(ctx); 

    auto f(std::begin(input)), l(std::end(input)); 

    if (qi::phrase_parse(f, l, p, qi::space)) 
     std::cout << "Success: " << ctx.to_string() << std::endl; 
    else 
     std::cout << "failed" << std::endl; 

    return 0; 
} 

Coliru Example

지금은 2 가지 질문이 있습니다.

1) 내 접근법은 일반적으로 거부 바인딩으로 구현할 수 있습니까?

2) A : add_param()과 같은 메소드에 std :: vector <> 생성 된 속성을 바인딩하는 방법?

-

감사

답변

0

좋아, 마지막으로 내가 필요한 것을 구현할 수 있었다 : Coliry Example

관련 문제