我试图遵循 Boost::iostreams 文档中的有限状态过滤器示例。但是当我去使用过滤器时,我收到一条错误消息,指出 ::imbue 无法访问,因为 'boost::iostreams::detail::finite_state_filter_impl' 使用 'protected' 从 'my_fsm' 继承。
沮丧的是,我将我的代码复制到了用于 boost 示例的测试中。测试编译并通过。我的结论是我可能误用了由以下定义的双重用途过滤器:
typedef io::finite_state_filter my_fsm_filter;
我觉得只是将它推到过滤流上可能不合适,但我找不到缺失的步骤。我确信必须需要包装过滤器,但我找不到任何示例(尽管我确信如果我对用于测试 boost 代码的代码进行了足够深入的研究,它必须在某个地方)。
这是一些示例代码:
#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;
}