boost karma 中的生成器指令列行为不一致

2023-11-30

我正在编写一个 karma 生成器来生成 HTML 页面,并且在使用列指令时遇到不一致的行为。 这很可能是我对其工作原理的理解。

基本上我正在生成一个网格,它要求我在数据每出现两次后插入一些分隔符。

以下是我用来进行测试运行的基本程序。

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/include/struct.hpp>
#include <boost/fusion/include/nview.hpp>
#include <boost/assign/std/vector.hpp>

namespace fusion = boost::fusion;
namespace karma = boost::spirit::karma;

///////////////////////////////////////////////////////////////////////////////
namespace client
{
    //  Our employee struct
    struct employee
    {
        int num;
        std::string datatype;
        std::string dataname;
        std::string inputicon;
    };

    // define iterator type
    typedef std::back_insert_iterator<std::string> iterator_type;

}

BOOST_FUSION_ADAPT_STRUCT(
        client::employee,
        (int, num)
                (std::string, datatype)
                (std::string, dataname)
                (std::string, inputicon)
)

///////////////////////////////////////////////////////////////////////////////
int main()
{
    std::string str;

    // some employees
    client::employee john = { 25, "int", "sra_command","fa fa-wrench" };
    client::employee mary = { 25, "float", "swt_command","fa fa-wrench" };
    client::employee tom = { 25, "double", "msc_command","fa fa-mobile" };

    // now make a list of all employees and print them all
    std::vector<client::employee> employees;
    {
        using namespace boost::assign;
        employees += john, mary, tom;
    }

    {
        typedef
        fusion::result_of::as_nview<client::employee const, 1, 2, 3>::type
                names_and_salary;


        karma::rule<client::iterator_type, names_and_salary()> small_box  =
                                                   "<startofblock>"  <<  karma::string << "<after_first>"
                                                                    << karma::string << "<after_second>"
                                                                    << karma::string << "<after_third>";

        std::string generated;
        typedef std::back_insert_iterator<std::string> sink_type;
        sink_type sink(generated);
        karma::generate_delimited(sink, karma::columns(2,karma::string("nth_delimiter"))[small_box % karma::eol],karma::space,employees );

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

上面生成以下输出:

<startofblock>int<after_first>sra_command<after_second>fa fa-wrench<after_third> 
 nth_delimiter<startofblock>float<after_first>swt_command<after_second>fa fa-wrench<after_third> 
 nth_delimiter<startofblock>double<after_first>msc_command<after_second>fa fa-mobile<after_third> nth_delimiter

如前所述,第 n 个分隔符出现在每一代之后,而不是每秒。

预期输出是

<startofblock>int<after_first>sra_command<after_second>fa fa-wrench<after_third> 
<startofblock>float<after_first>swt_command<after_second>fa fa-wrench<after_third>  
 nth_delimiter<startofblock>double<after_first>msc_command<after_second>fa fa-mobile<after_third> 

我认为这很接近您想要实现的目标。就像我说的,最后一次(不完整的)运行仍然会被“终止”nth_delimiter tag:

Live On Coliru

#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/include/struct.hpp>
#include <boost/fusion/include/nview.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace client {
    struct employee
    {
        int num;
        std::string datatype;
        std::string dataname;
        std::string inputicon;
    };

    typedef std::back_insert_iterator<std::string> iterator_type;
}

BOOST_FUSION_ADAPT_STRUCT(client::employee, /*num,*/ datatype, dataname, inputicon)

///////////////////////////////////////////////////////////////////////////////
int main() {
    // some employees
    std::vector<client::employee> 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" },
    };

    // now print them all
    std::string generated;
    {
        using namespace boost::spirit::karma;

        using Sink = client::iterator_type;
        //using Attr = boost::fusion::result_of::as_nview<client::employee const, 1, 2, 3>::type;
        using Attr = client::employee;
        rule<Sink, Attr()> small_box  = "<B>" << string << "<1>" << string << "<2>" << string << "<3>";

        generate(Sink(generated), columns(2, "<nth_delimiter>\n") [+small_box], employees);
    }

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

Prints

<B>int<1>sra_command<2>fa fa-wrench<3><B>float<1>swt_command<2>fa fa-wrench<3><nth_delimiter>
<B>double<1>msc_command<2>fa fa-mobile<3><B>int<1>sra_command<2>fa fa-wrench<3><nth_delimiter>
<B>float<1>swt_command<2>fa fa-wrench<3><B>double<1>msc_command<2>fa fa-mobile<3><nth_delimiter>
<B>int<1>sra_command<2>fa fa-wrench<3><B>float<1>swt_command<2>fa fa-wrench<3><nth_delimiter>
<B>double<1>msc_command<2>fa fa-mobile<3><nth_delimiter>

Bonus:

问题在于smallbox % eol就是它smallbox是一个元素,并且eol too:

    generate(Sink(generated), columns(4, "<nth_delimiter>") [small_box % eol], employees);

Live On Coliru

<B>int<1>sra_command<2>fa fa-wrench<3>
<B>float<1>swt_command<2>fa fa-wrench<3>
<nth_delimiter><B>double<1>msc_command<2>fa fa-mobile<3>
<B>int<1>sra_command<2>fa fa-wrench<3>
<nth_delimiter><B>float<1>swt_command<2>fa fa-wrench<3>
<B>double<1>msc_command<2>fa fa-mobile<3>
<nth_delimiter><B>int<1>sra_command<2>fa fa-wrench<3>
<B>float<1>swt_command<2>fa fa-wrench<3>
<nth_delimiter><B>double<1>msc_command<2>fa fa-mobile<3><nth_delimiter>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

boost karma 中的生成器指令列行为不一致 的相关文章

随机推荐

  • 策略和享元模式

    我读过 策略对象通常会成为很好的蝇量级 来自可重用面向对象软件的设计模式元素 我想知道如何实现这一点 我在互联网上没有找到任何例子 下面的代码 C 是否符合这个想法 Thanks using System using System Coll
  • 对 void“函数”C++ 的未定义引用

    我收到以下错误 features cpp text 0x4f6 undefined reference to void Convolve
  • 如何将 NSMutableDictionary 保存到文档中的文件中?

    我想将 NSMutableDictionary 对象的内容保存到文件中 我该怎么做呢 我已经知道如何使用 NSDictionary 对象完成此任务 但我不知道如何将此 NSMutableDictionary 转换 复制到 NSDiction
  • 生成器函数(yield)比迭代器类(__next__)快得多

    UPDATE 反映最先进的知识水平 状态 2017 05 12 这次更新的原因是 当我问这个问题时 我并没有意识到我已经发现了一些关于 Python3 如何 在幕后 工作的信息 以下所有内容的结论是 如果您为迭代器编写自己的 Python3
  • 大型对象列表上多处理 Pool.map() 的缩放效果不佳:How to meet betterparallelscaling in python?

    让我们定义 from multiprocessing import Pool import numpy as np def func x for i in range 1000 i 2 return 1 请注意func 做某事并且总是返回一
  • 使用 Python 获取 Windows 版本

    当我在控制台 CMD 中输入 winver 时 我将获得我的Windows版本 内部版本号左边的四个数字 例如 1803 1903 1909 2004 20H2 但是我怎样才能在Python中获得我的Windows版本呢 我已经尝试过 im
  • Django:无法导入模块

    我正在尝试在我的views py 中导入一个模块 from django shortcuts import render Create your views here from viewcreator import Builder impo
  • iPhone - UILocalNotification 作为警报

    即使我的 iPhone 应用程序处于后台 我如何使用 UILocalNotification 每天晚上 8 点显示我的闹钟 Set the fireDate到晚上 8 点并设置repeatInterval to NSDayCalendarU
  • 将 N 列的数据框转换为两个“堆叠”列的数据框

    你好堆栈社区 我正在从事网络分析工作 并且有一个数据重塑问题 我的原始数据以一系列列的形式出现 每列都是 源 和 目标 对 最终的数据框需要由 源 和 目标 两列组成 请注意 这些对是交错的 因为它们的源和目标像在有向网络中一样链接 请参阅
  • 使用 vbscript 更改控制台标题

    有没有办法改变cmd标题 我写了一个vbs程序 但dos标题很糟糕 名称为 c windows system32 cscript exe 我尝试一下 标题 the name 和 标题 姓名 但两者都不起作用 感谢帮助 AlexK您指向的链接
  • 如何覆盖循环内的可变字符串?

    我的示例稍作修改The Rust Book 中的猜谜游戏教程 第一次迭代后 循环似乎无法正确读取用户对可变字符串的输入 您能找出以下代码中关于以下内容的问题吗 mut input text extern crate rand use ran
  • 通道发送是否是 goroutine 调度的抢占点?

    从我对Go调度器的理解来看 Go调度算法是部分先发制人 当 goroutine 调用函数或阻塞 I O 时 会发生 goroutine 切换 向通道发送消息时是否会发生 goroutine 切换 goroutine A ch lt mess
  • PHP 正则表达式,忽略交替语句中的第一个分组

    我试图弄清楚如何使用 preg match 捕获一条语句 如果另一条语句不存在 示例文本 div h1 strong Citing Your Sources strong h1 div 因为 pagetitle
  • 如何将应用程序路径传递给Azure批处理中的任务

    我正在使用 Azure 批处理 我有一个需要在计算节点上执行的exe 我在任务窗口的天蓝色门户中使用此路径 cmd c AZ BATCH APP PACKAGE MyAppCreateRG CreateRG exe args HelloRG
  • 什么是 -D 编译器标志 C++(clang、GNU、MSVC)

    好吧 我假设 D前缀表示 define无论它后面跟着什么变量名 但是我找不到任何有关编译器标志的 makefile 功能的文档 CXX clang DTHISISPREPROCESSORVARIABLE So DTHISISPREPROCE
  • 剧作家使用 JavaScript (Node) 将变量传递给 eval

    注意 这是针对 Playwright 浏览器 API 如 puppeteer 我试图找到页面上的所有元素 然后想将这些元素过滤为值 这些值具有特定的选择器 css 类等 我遇到的问题是我无法将外部变量传递到 eval 函数中 以便我的 CS
  • 如何调整 OpenEars 错误识别

    我在我的应用程序中使用了 OpenEars 只需识别字母表中的 a 到 z 即可 但它在识别字母方面的识别能力比识别单词的能力差 那么 我如何使用我的声音模型来提高 OpenEars 的识别度 以及如何使用 OpenEars 来识别一些特殊
  • 根据时间序列中的条件对行进行分组并忽略错误值

    我有一组具有不同采样间隔的动物位置 我想要做的是对采样间隔符合特定条件 例如低于特定值 的序列进行分组和标记 这是一个修订版这个问题被标记为重复项this one 这个修订后的问题的不同之处在于 所有不符合标准的值都应该被忽略 而不是被标记
  • Bing 搜索 API - 如何本地化结果?

    默认情况下如何使用斯洛文尼亚语搜索结果 我们的服务器有德国 IP Bing API 首先自动显示德国结果 我已经尝试了文档中描述的一些参数 但到目前为止它们都不起作用 Thanks 您是否尝试过设置市场选项 根据这个示例页面 你应该尝试这样
  • boost karma 中的生成器指令列行为不一致

    我正在编写一个 karma 生成器来生成 HTML 页面 并且在使用列指令时遇到不一致的行为 这很可能是我对其工作原理的理解 基本上我正在生成一个网格 它要求我在数据每出现两次后插入一些分隔符 以下是我用来进行测试运行的基本程序 inclu