简体   繁体   English

使用Boost :: iostreams两用过滤器

[英]Using Boost::iostreams dual-use filters

I was attempting to follow the example of Finite State Filters in the Boost::iostreams documentation. 我试图遵循Boost :: iostreams文档中的有限状态过滤器示例。 However when I went to use the filter I got an error stating the ::imbue was not accessible because 'boost::iostreams::detail::finite_state_filter_impl' uses 'protected' to inherit from 'my_fsm'. 但是,当我使用过滤器时,出现错误,指出:: imbue无法访问,因为'boost :: iostreams :: detail :: finite_state_filter_impl'使用'protected'从'my_fsm'继承。

Frustrated I copied my code into the tests used to in the boost examples. 沮丧的是,我将代码复制到了boost示例中使用的测试中。 The tests compile and pass. 测试编译并通过。 My conculsion is that I am probably mis-using the dual use filter defined by: 我的困惑是,我可能误用了以下定义的双重用途过滤器:

typedef io::finite_state_filter my_fsm_filter; typedef io :: finite_state_filter my_fsm_filter;

I feel that just pushing it onto a filtered_stream may not be proper, but I could not find a missing step. 我觉得仅将其推送到filtered_stream可能不合适,但是我找不到丢失的步骤。 I am sure there must be a need to wrap the filter but I can find no example (though I am sure if I dug deep enough into the code used to test the boost code it has to be there somewhere). 我确定肯定有必要包装过滤器,但我找不到任何示例(尽管我确定是否对用于测试增强代码的代码进行了足够深入的研究,但它必须存在于某处)。

here is a bit of example code: 这是一些示例代码:

#include <boost/mpl/vector.hpp>
#include <libs/iostreams/example/finite_state_filter.hpp>

namespace io = boost::iostreams;

struct my_fsm : io::finite_state_machine<my_fsm> {
    BOOST_IOSTREAMS_FSM(my_fsm) // define skip and push.
    typedef my_fsm self;
    static const int beginline = 0;
    static const int skipline = 1;
    static const int dataline = 2;

    typedef boost::mpl::vector <
        row<beginline, is<'C'>, skipline, &self::skip>,
        row<beginline, is_any, dataline, &self::push>,
        row<skipline, is<'\n'>, beginline, &self::skip>,
        row<skipline, is_any, skipline, &self::skip>,
        row<dataline, is<'\n'>, beginline, &self::push>,
        row<dataline, is_any, dataline, &self::push>
    > transition_table;
};

typedef io::finite_state_filter<my_fsm> my_fsm_filter;

#include <iostream>
#include <string>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/stream.hpp>

namespace io = boost::iostreams;

int main() {
    io::stream<io::file_sink> out(io::file_sink("outputfile.txt"));
    io::filtering_istream in;
    my_fsm_filter infsm;
    in.push(my_fsm_filter());
    in.push(io::file_source("inputdata.txt"));

    while (in) {
        std::string line;
        if(std::getline(in, line)) {
            //std::cout << line << std::endl;
            out << line << std::endl;
        }
    }
    return 0;
}

I personally feel that there is a bug in the sample header with respect to this imbue call. 我个人认为样本标题中有关于此注入事件的错误。

However, you can work around it by changing the typedef to 但是,您可以通过将typedef更改为

struct my_fsm_filter : io::finite_state_filter<my_fsm> {
    using io::finite_state_filter<my_fsm>::imbue;
};

This explicitly exposes the imbue method as public on the derived type. 这将imbue方法显式公开为派生类型上的public。 I haven't looked at the sample program that you reported to be working (because you didn't link to it). 我没有查看您报告正在运行的示例程序(因为您没有链接到该示例程序)。 But it's possible they used a similar hack. 但是有可能他们使用了类似的技巧。

In my tests, a similar edit to finite_state_filte.hpp L278 to add 在我的测试中,对finite_state_filte.hpp L278进行了类似的编辑,以添加

using base_type::imbue;

to class finite_state_filter has the same effect. finite_state_filter具有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM