这是我在这里提到的问题的继续
Inconsistent Generator directive column behavior in boost karma
我想将该规则包装到另一个规则中,当这样做时,将column指令传递给子规则,但我无法弄清楚这是做什么的正确方法。
以下是代码
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/include/struct.hpp>
#include <boost/fusion/include/nview.hpp>
///////////////////////////////////////////////////////////////////////////////
namespace client
{
struct BoxData
{
int num;
std::string datatype;
std::string dataname;
std::string inputicon;
};
struct Box
{
std::vector<BoxData> box_data;
};
typedef std::back_insert_iterator<std::string> iterator_type;
}
BOOST_FUSION_ADAPT_STRUCT(
client::BoxData,
// (int, num)
(std::string, datatype)
(std::string, dataname)
(std::string, inputicon)
)
BOOST_FUSION_ADAPT_STRUCT(
client::Box,
(std::vector<client::BoxData>, box_data)
)
///////////////////////////////////////////////////////////////////////////////
int main()
{
// some employees
std::vector<client::BoxData> const employees{
{25, "int", "sra_command", "fa fa-wrench"},
{26, "float", "swt_command", "fa fa-wrench"},
{27, "double", "msc_command", "fa fa-mobile"},
{28, "int", "sra_command", "fa fa-wrench"},
{29, "float", "swt_command", "fa fa-wrench"},
{30, "double", "msc_command", "fa fa-mobile"},
{31, "int", "sra_command", "fa fa-wrench"},
{32, "float", "swt_command", "fa fa-wrench"},
{33, "double", "msc_command", "fa fa-mobile"},
};
client::Box box;
///TODO prevent copying of the vector
box.box_data = employees;
// now print them all
std::string generated;
{
using namespace boost::spirit::karma;
using Sink = client::iterator_type;
using BoxDataAttr = client::BoxData;
using BoxAttr = client::Box;
rule<Sink, BoxDataAttr(),space_type> small_box = "<B>" << string << "<1>" << string << "<2>" << string << "<3>";
rule<Sink , BoxAttr()> big_box = "<Start>" << *small_box << "<End>" ;
generate(Sink(generated),[big_box],box);
}
std::cout << generated << std::endl;
}
在第
行rule<Sink , BoxAttr()> big_box = "<Start>" << *small_box << "<End>" ;
我想传递
columns(2, "delimiter\n")[small_box]
分隔符,以便将列分隔符应用于small_box rule
。
因此预期的输出变为
<Start><B>int<1>sra_command<2>fa fa-wrench<3><B>float<1>swt_command<2>fa fa-wrench<3>delimiter
<B>double<1>msc_command<2>fa fa-mobile<3><B>int<1>sra_command<2>fa fa-wrench<3>delimiter
<B>float<1>swt_command<2>fa fa-wrench<3><B>double<1>msc_command<2>fa fa-mobile<3>delimiter
<B>int<1>sra_command<2>fa fa-wrench<3><B>float<1>swt_command<2>fa fa-wrench<3>delimiter
<B>double<1>msc_command<2>fa fa-mobile<3>delimiter<End>
最终因为会有一个向另一个自定义分隔符传递给big_box
规则的向量。
我打算编写一个诊断界面作为学习各种C ++概念的练习,其中涉及以下内容。
1.从IDL文件中解析数据。我还没有确定确切的格式,但看起来与此类似
struct Data1
{
int command_one output "fa fa-wrench";
float commmand_two output "fa fa-sensor";
}
struct Data2
{
bool a input switch;
int b input progress_bar {0,100} ;
}
从IDL文件生成HTML元素。每个结构都包装在一个框中,该框包含其中包含图标/可选部分的较小框中的各个元素。第n个分隔符对于生成用于在屏幕上放置这些框的引导网格非常有用。
使用websocket实现完成HTML页面,该实现在收到包含DOM元素名称 - 值对的JSON数据时自动更新DOM元素。同样,对于每个输入,都会执行send命令将其包装在JSON中并发送到Web服务器。
自动生成网络服务器的代码,以便剩下要实现的唯一代码是从外部源接收/传输数据,并且所有转换都来自/生成JSON。
以下用例促使我寻找解析器/生成器解决方案。
答案 0 :(得分:2)
我对你的问题的原因不太确定,但在我看来昨天的解决方案仍然存在:
<强> Live On Coliru 强>
rule<Sink, BoxDataAttr()> small_box = "<B>" << string << "<1>" << string << "<2>" << string << "<3>";
rule<Sink, BoxAttr()> big_box = "<Start>" << columns(4, "delimiter\n") [small_box % eol] << "<End>" ;
generate(Sink(generated), big_box, employees);
打印
<Start><B>int<1>sra_command<2>fa fa-wrench<3>
<B>float<1>swt_command<2>fa fa-wrench<3>
delimiter
<B>double<1>msc_command<2>fa fa-mobile<3>
<B>int<1>sra_command<2>fa fa-wrench<3>
delimiter
<B>float<1>swt_command<2>fa fa-wrench<3>
<B>double<1>msc_command<2>fa fa-mobile<3>
delimiter
<B>int<1>sra_command<2>fa fa-wrench<3>
<B>float<1>swt_command<2>fa fa-wrench<3>
delimiter
<B>double<1>msc_command<2>fa fa-mobile<3>delimiter
<End>
预感到,您可能错过了, space_type
规则small_box
作为分隔符的事实。
最终因为会有一个向另一个自定义分隔符传递给big_box规则的向量。
此时我质疑你选择的工具。为什么不在这里推出自己的漂亮印刷品。我希望它会相当简单。另请阅读https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
这里避免复制矢量:
client::Box const employees { {
{25, "int", "sra_command", "fa fa-wrench"},
{26, "float", "swt_command", "fa fa-wrench"},
{27, "double", "msc_command", "fa fa-mobile"},
{28, "int", "sra_command", "fa fa-wrench"},
{29, "float", "swt_command", "fa fa-wrench"},
{30, "double", "msc_command", "fa fa-mobile"},
{31, "int", "sra_command", "fa fa-wrench"},
{32, "float", "swt_command", "fa fa-wrench"},
{33, "double", "msc_command", "fa fa-mobile"},
} };
更一般地说,移动语义本可以起作用:
box.box_data = std::move(employees);