gpt4 book ai didi

c++ - boost::spirit::karma 语法:带有可选属性的结构的逗号分隔输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:50 24 4
gpt4 key购买 nike

我需要一个带有可选值的结构的逗号分隔输出。例如,如果我有这个结构:

MyStruct 
{
boost::optional<std::string> one;
boost::optional<int> two;
boost::optional<float> three;
};

输出如:{ "string", 1, 3.0 } { "string"} { 1, 3.0 } 和等等。

现在,我有这样的代码:

struct MyStruct
{
boost::optional<std::string> one;
boost::optional<int> two;
boost::optional<float> three;
};

BOOST_FUSION_ADAPT_STRUCT
(MyStruct,
one,
two,
three)

template<typename Iterator>
struct MyKarmaGrammar : boost::spirit::karma::grammar<Iterator, MyStruct()>
{
MyKarmaGrammar() : MyKarmaGrammar::base_type(request_)
{
using namespace std::literals::string_literals;
namespace karma = boost::spirit::karma;

using karma::int_;
using karma::double_;
using karma::string;
using karma::lit;
using karma::_r1;

key_ = '"' << string(_r1) << '"';

str_prop_ = key_(_r1) << ':'
<< string
;

int_prop_ = key_(_r1) << ':'
<< int_
;

dbl_prop_ = key_(_r1) << ':'
<< double_
;

//REQUEST
request_ = '{'
<< -str_prop_("one"s) <<
-int_prop_("two"s) <<
-dbl_prop_("three"s)
<< '}'
;
}

private:
//GENERAL RULES
boost::spirit::karma::rule<Iterator, void(std::string)> key_;
boost::spirit::karma::rule<Iterator, double(std::string)> dbl_prop_;
boost::spirit::karma::rule<Iterator, int(std::string)> int_prop_;
boost::spirit::karma::rule<Iterator, std::string(std::string)> str_prop_;

//REQUEST
boost::spirit::karma::rule<Iterator, MyStruct()> request_;
};

int main()
{
using namespace std::literals::string_literals;

MyStruct request = {std::string("one"), 2, 3.1};

std::string generated;
std::back_insert_iterator<std::string> sink(generated);

MyKarmaGrammar<std::back_insert_iterator<std::string>> serializer;

boost::spirit::karma::generate(sink, serializer, request);

std::cout << generated << std::endl;
}

这可行,但我需要一个逗号分隔的输出。我尝试使用如下语法:

request_ = '{'
<< (str_prop_("one"s) |
int_prop_("two"s) |
dbl_prop_("three"s)) % ','
<< '}'
;

但是我收到这个编译错误:

/usr/include/boost/spirit/home/support/container.hpp:194:52: error: no type named ‘const_iterator’ in ‘struct MyStruct’
typedef typename Container::const_iterator type;

谢谢!

最佳答案

您的结构不是容器,因此列表- operator%不管用。文档指出它希望该属性是容器类型。

所以,就像在 Qi 对应物中一样 I showed you to create a conditional delim production :

 delim         = (&qi::lit('}')) | ',';

你在这里需要类似的东西。然而,一切都反转了。而不是根据 { 的存在“检测”输入序列的结尾,我们需要从“自左大括号以来还没有输出一个字段”来跟踪前面字段的缺失。

这有点棘手,因为所需的状态不能来自与输入相同的来源。为简单起见,我们将在此处使用解析器成员¹:

private:
bool _is_first_field;

现在,当我们生成左大括号时,我们要将其初始化为 true :

    auto _f = px::ref(_is_first_field); // short-hand
request_ %= lit('{') [ _f = true ]

Note: Use of %= instead of = tells Spirit that we want automatic attribute propagation to happen, in spite of the presence of a Semantic Action ([ _f = true ]).

现在,我们需要生成分隔符:

    delim = eps(_f) | ", ";

简单。用法也很简单,只是我们要有条件地 reset _f :

    auto reset = boost::proto::deep_copy(eps [ _f = false ]);

str_prop_ %= (delim << key_(_r1) << string << reset) | "";
int_prop_ %= (delim << key_(_r1) << int_ << reset) | "";
dbl_prop_ %= (delim << key_(_r1) << double_ << reset) | "";

这里非常微妙的一点是,我将声明的规则属性类型从 T 更改为给 optional<T> 。如果值生成器为空(boost::none),这允许 Karma 施展魔法使值生成器失败,并跳过 reset。 !

ka::rule<Iterator, boost::optional<double>(std::string)> dbl_prop_;
ka::rule<Iterator, boost::optional<int>(std::string)> int_prop_;
ka::rule<Iterator, boost::optional<std::string>(std::string)> str_prop_;

现在,让我们整理一些测试用例:

测试用例

Live On Coliru

#include "iostream"
#include <boost/optional/optional_io.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>

struct MyStruct {
boost::optional<std::string> one;
boost::optional<int> two;
boost::optional<double> three;
};

BOOST_FUSION_ADAPT_STRUCT(MyStruct, one, two, three)

namespace ka = boost::spirit::karma;
namespace px = boost::phoenix;

template<typename Iterator>
struct MyKarmaGrammar : ka::grammar<Iterator, MyStruct()> {
MyKarmaGrammar() : MyKarmaGrammar::base_type(request_) {
using namespace std::literals::string_literals;

using ka::int_;
using ka::double_;
using ka::string;
using ka::lit;
using ka::eps;
using ka::_r1;

auto _f = px::ref(_is_first_field);
auto reset = boost::proto::deep_copy(eps [ _f = false ]);

key_ = '"' << string(_r1) << "\":";

delim = eps(_f) | ", ";

str_prop_ %= (delim << key_(_r1) << string << reset) | "";
int_prop_ %= (delim << key_(_r1) << int_ << reset) | "";
dbl_prop_ %= (delim << key_(_r1) << double_ << reset) | "";

//REQUEST
request_ %= lit('{') [ _f = true ]
<< str_prop_("one"s) <<
int_prop_("two"s) <<
dbl_prop_("three"s)
<< '}';
}

private:
bool _is_first_field = true;
//GENERAL RULES
ka::rule<Iterator, void(std::string)> key_;
ka::rule<Iterator, boost::optional<double>(std::string)> dbl_prop_;
ka::rule<Iterator, boost::optional<int>(std::string)> int_prop_;
ka::rule<Iterator, boost::optional<std::string>(std::string)> str_prop_;
ka::rule<Iterator> delim;

//REQUEST
ka::rule<Iterator, MyStruct()> request_;
};

template <typename T> std::array<boost::optional<T>, 2> option(T const& v) {
return { { v, boost::none } };
}

int main() {
using namespace std::literals::string_literals;

for (auto a : option("one"s))
for (auto b : option(2))
for (auto c : option(3.1))
for (auto request : { MyStruct { a, b, c } }) {
std::string generated;
std::back_insert_iterator<std::string> sink(generated);

MyKarmaGrammar<std::back_insert_iterator<std::string>> serializer;

ka::generate(sink, serializer, request);

std::cout << boost::fusion::as_vector(request) << ":\t" << generated << "\n";
}
}

打印:

( one  2  3.1): {"one":one, "two":2, "three":3.1}
( one 2 --): {"one":one, "two":2}
( one -- 3.1): {"one":one, "three":3.1}
( one -- --): {"one":one}
(-- 2 3.1): {"two":2, "three":3.1}
(-- 2 --): {"two":2}
(-- -- 3.1): {"three":3.1}
(-- -- --): {}

¹ 请注意,这限制了解析器的重入使用,并使其成为非常量等。karma::locals是真正的答案,增加了一点复杂性

关于c++ - boost::spirit::karma 语法:带有可选属性的结构的逗号分隔输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43614973/

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