More on ylmm::basic_buffer

The class ylmm::basic_buffer is an "Abstract base class" (ABC) for various kinds of input buffers.

It's interface is quite simple.

The thing to notice about ylmm::basic_buffer is the member function ylmm::basic_buffer::input and the pure virtual member functions ylmm::basic_buffer::input_buffered and ylmm::basic_buffer::input_interactive - at a minimum, these later two must overloaded in a derived class.

ylmm::basic_buffer::input is the member function responsible for reading characters into the Flex generated C function. Depending on wether the scanner was defined as interactive or buffered, the member function will deligate the call to one of the virtual member functions ylmm::basic_buffer::input_buffered or ylmm::basic_buffer::input_interactive.

Hence, the derived class should overload ylmm::basic_buffer::input_buffered to read as much as possible into an internal buffer, while ylmm::basic_buffer::input_interactive should read up to a newline and no more, immediately returning the result.

The basic class reads input from an std::istream. Using the std::streambuf layer, one can setup the scanner to read from almost anything. The sterio-typical example is ofcourse from std::cin, which is also the default.

    my_scanner scanner(new ylmm::istream_buffer(std::cin)); 

Another common applicatin is to read from a file via std::ifstream.

    std::ifstream input_file("input_file.txt");
    my_scanner scanner(new ylmm::istream_buffer(input_file));

But one can easily make more exotic variations. Suppose you have a class rlmm::streambuf which reads input lines via a readline interface (see here for such an interface), and that class derives from std::streambuf, then one can use the stream buffer layer to read lines from the user via readline, like so:

    std::istream input(new read_line::streambuf("prompt> ");
    ylmm::istream_buffer buffer(input);
    buffer.interactive(true);
    my_scanner scanner(&buffer);

At first, as well as reading the ISO/IEC C++ standard, you may be left with the impression that the std::streambuf layer is extreemly complex and difficult to deal with. In fact, it is not. It's one of the more beautiful designed elements of the ISO/IEC C++ standard. Using the standard std::streambuf layer, only your imagination sets the limit.

Top of page
Christian Holm (home page)
Last update Fri Jul 8 12:58:03 2005
Created by DoxyGen 1.4.3-20050530