gpt4 book ai didi

c++ - boost::spirit 绑定(bind)函数提供参数作为 spirit:qi::_val

转载 作者:行者123 更新时间:2023-11-30 04:17:02 24 4
gpt4 key购买 nike

需要为 std::pair 对象提供类型为 boost::variant 的对象的值。您将如何使用其他资源来实现这个想法?下面还有其他方法吗?

    struct aggr_pair_visitor : public ::boost::static_visitor<void>
{
public:
explicit aggr_pair_visitor( column_and_aggregate & pair_ ) : pair(pair_)
{
}
void operator()(column_name_t const & column)
{
pair.first = column;
}
void operator()(unsigned const & faggr)
{
if ( faggr > static_cast<unsigned>(sql_faggregate::SUM) || faggr < static_cast<unsigned>(sql_faggregate::AVG) )
throw std::runtime_error("Failed to parse aggregate type : Not valid integer");
else pair.second = static_cast<sql_faggregate>(faggr);
}
private:
column_and_aggregate & pair;
};
void apply_col_and_aggr_visitor( column_and_aggregate & col_and_aggr_pair, ::boost::variant< column_name_t, unsigned > const & val )
{
aggr_pair_visitor pair_visitor( col_and_aggr_pair );
::boost::apply_visitor( pair_visitor, val ); // N.B.!!! Runtime execution of operator()!
}
spirit::qi::rule< iterator, column_and_aggregate(), ascii::space_type > agg_pair =
quoted_string[::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )]
> ':'
> spirit::int_[::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )];
spirit::qi::rule< iterator, column_and_aggregate_container(), ascii::space_type > aggregates_parser =
'{'
> agg_pair[phoenix::push_back(spirit::qi::_val, spirit::qi::_1)] % ',' // N.B.!!! list-redux technic
> '}';

最佳答案

好吧,第二眼我觉得你只是错过了融合适应 std::pair:

#include <boost/fusion/adapted/std_pair.hpp>
// Or:
#include <boost/fusion/adapted.hpp>

使用它,整个复杂性都消失了,不需要任何涉及变体的东西。让我们假设以下类型:

typedef std::string column_name_t;

enum sql_faggregate
{
SUM,
// ....
AVG,
};

typedef std::pair<column_name_t, sql_faggregate> column_and_aggregate;
typedef std::vector<column_and_aggregate> column_and_aggregate_container;

一个简单的语法是:

template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, column_and_aggregate_container(), Skipper>
{
parser() : parser::base_type(aggregates_parser)
{
using namespace qi;
// using phx::bind; using phx::ref; using phx::val;

quoted_string = lexeme [ "'" >> *~qi::char_("'") >> "'" ];
faggr = int_;
agg_pair = quoted_string > ':' > faggr;
aggregates_parser = '{' > agg_pair % ',' > '}';
}

private:
qi::rule<It, std::string(), qi::space_type> quoted_string;
qi::rule<It, sql_faggregate(), qi::space_type> faggr;
qi::rule<It, column_and_aggregate(), qi::space_type> agg_pair;
qi::rule<It, column_and_aggregate_container(), qi::space_type> aggregates_parser;
};

您也可以添加输入验证:

faggr %= int_ [ qi::_pass = (qi::_1 >=SUM and qi::_1<=AVG) ];

注意 %= 以确保属性传播。

完整的工作演示

程序代码:

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

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

typedef std::string column_name_t;

enum sql_faggregate
{
SUM,
// ....
AVG,
};

typedef std::pair<column_name_t, sql_faggregate> column_and_aggregate;
typedef std::vector<column_and_aggregate> column_and_aggregate_container;

template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, column_and_aggregate_container(), Skipper>
{
parser() : parser::base_type(aggregates_parser)
{
using namespace qi;
// using phx::bind; using phx::ref; using phx::val;

quoted_string = lexeme [ "'" >> *~qi::char_("'") >> "'" ];
faggr = int_;
agg_pair = quoted_string > ':' > faggr;
aggregates_parser = '{' > agg_pair % ',' > '}';

BOOST_SPIRIT_DEBUG_NODE(aggregates_parser);
}

private:
qi::rule<It, std::string(), qi::space_type> quoted_string;
qi::rule<It, sql_faggregate(), qi::space_type> faggr;
qi::rule<It, column_and_aggregate(), qi::space_type> agg_pair;
qi::rule<It, column_and_aggregate_container(), qi::space_type> aggregates_parser;
};

bool doParse(const std::string& input)
{
typedef std::string::const_iterator It;
auto f(begin(input)), l(end(input));

parser<It, qi::space_type> p;
column_and_aggregate_container data;

try
{
bool ok = qi::phrase_parse(f,l,p,qi::space,data);
if (ok)
{
std::cout << "parse success\n";
for (auto& pair : data)
std::cout << "result: '" << pair.first << "' : " << (int) pair.second << "\n";
}
else std::cerr << "parse failed: '" << std::string(f,l) << "'\n";

if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
return ok;
} catch(const qi::expectation_failure<It>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'\n";
}

return false;
}

int main()
{
bool ok = doParse("{ 'column 1' : 1, 'column 2' : 0, 'column 3' : 1 }");
return ok? 0 : 255;
}

打印输出:

parse success
result: 'column 1' : 1
result: 'column 2' : 0
result: 'column 3' : 1

PS:如果你想在语义 Action 中做同样的事情,你可能想这样写:

agg_pair = 
quoted_string [ phx::bind(&column_and_aggregate::first, _val) = _1 ]
> ':'
> faggr [ phx::bind(&column_and_aggregate::second, _val) = _1 ];

您会发现您可以将它放入上面的示例中,它的工作原理完全相同。对于这个特定的语法,它只是更冗长,所以我不推荐它:)

关于c++ - boost::spirit 绑定(bind)函数提供参数作为 spirit:qi::_val,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com