nickdmax
nickdmax

Reputation: 559

Using Boost::iostreams dual-use filters

I was attempting to follow the example of Finite State Filters in the Boost::iostreams documentation. 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'.

Frustrated I copied my code into the tests used to in the boost examples. 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;

I feel that just pushing it onto a filtered_stream may not be proper, but I could not find a missing step. 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;
}

Upvotes: 2

Views: 252

Answers (1)

sehe
sehe

Reputation: 392911

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

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. 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

using base_type::imbue;

to class finite_state_filter has the same effect.

Upvotes: 1

Related Questions