class ugcs::vsm::Text_stream_filter

Overview

Class for convenient filtering of a text stream using regular expressions. More…

#include <text_stream_filter.h>

class Text_stream_filter: public std::enable_shared_from_this< Text_stream_filter >
{
public:
    // typedefs

    typedef std::shared_ptr<Text_stream_filter> Ptr;
    typedef std::weak_ptr<Text_stream_filter> Weak_ptr;
    typedef std::vector<std::string> Lines_list;
    typedef Callback_proxy<bool, std::smatch*, Lines_list*, Io_result> Match_handler;
    typedef Callback_proxy<bool, const std::string*> Line_handler;
    typedef uint32_t Entry_handle;

    // classes

    class Entry;

    // fields

    static constexpr static size_t MAX_HISTORY_LINES = 10;
    static constexpr static size_t MAX_LINE_LENGTH = 512;
    static constexpr static size_t MAX_READ = 64;

    // construction

    Text_stream_filter(Io_stream::Ref stream, Request_completion_context::Ptr comp_ctx, size_t max_read = MAX_READ);

    // methods

    template <typename... Args>
    static Ptr Create(Args&&... args);

    template <class __Callable, typename... __Args>
    __DEFINE_CALLBACK_BUILDER_BODY(
        Make_match_handler,
        (std::smatch*, Text_stream_filter::Lines_list*, Io_result),
        (nullptr, nullptr, Io_result::OK)
        );

    template <class __Callable, typename... __Args>
    __DEFINE_CALLBACK_BUILDER_BODY(
        Make_line_handler,
        (const std::string*),
        (nullptr)
        );

    void Enable();
    void Disable(bool close_stream = true);

    Entry_handle Add_entry(
        const std::regex& re,
        Match_handler handler,
        std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
        size_t ctx_lines_before = 0,
        size_t ctx_lines_after = 0
        );

    void Set_line_handler(Line_handler handler);
};

Detailed Documentation

Class for convenient filtering of a text stream using regular expressions.

Typedefs

typedef std::shared_ptr<Text_stream_filter> Ptr

Pointer type.

typedef std::weak_ptr<Text_stream_filter> Weak_ptr

Pointer type.

typedef std::vector<std::string> Lines_list

List of accumulated lines.

typedef Callback_proxy<bool, std::smatch*, Lines_list*, Io_result> Match_handler

Match handler called when entry matched the input line.

Returns:

true to perceive the handler in the filter, false to remove it. Timeout (if any) is restarted when “true” is returned. It is allowed to return true only for OK and TIMED_OUT result codes.

typedef Callback_proxy<bool, const std::string*> Line_handler

Received line handler.

Returns:

true if line is captured by the handler and should not be fed to filter entries.

typedef uint32_t Entry_handle

Manipulation handle for an entry.

Fields

static constexpr static size_t MAX_HISTORY_LINES = 10

Maximal number of lines saved before the matched line.

static constexpr static size_t MAX_LINE_LENGTH = 512

Maximal number of characters in one line which is supported.

static constexpr static size_t MAX_READ = 64

Maximal number of bytes to read at once.

Construction

Text_stream_filter(Io_stream::Ref stream, Request_completion_context::Ptr comp_ctx, size_t max_read = MAX_READ)

Construct filter bound to a stream.

Methods

template <typename... Args>
static Ptr Create(Args&&... args)

Create an instance.

template <class __Callable, typename... __Args>
__DEFINE_CALLBACK_BUILDER_BODY(
    Make_match_handler,
    (std::smatch*, Text_stream_filter::Lines_list*, Io_result),
    (nullptr, nullptr, Io_result::OK)
    )

Builder for match handlers.

template <class __Callable, typename... __Args>
__DEFINE_CALLBACK_BUILDER_BODY(
    Make_line_handler,
    (const std::string*),
    (nullptr)
    )

Builder for line handler.

void Enable()

Enable the filter.

void Disable(bool close_stream = true)

Disable the filter.

Entry_handle Add_entry(
    const std::regex& re,
    Match_handler handler,
    std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
    size_t ctx_lines_before = 0,
    size_t ctx_lines_after = 0
    )

Add matching entry to the filter.

Should not be called, when related stream is already closed.

Parameters:

re

Regular expression which is matched against each line. Handler is fired when match is found and enough context lines received.

handler

Handler to invoke. In case of timeout it is fired with result argument equal to Io_result::TIMED_OUT. In this case lines list and match object can contain some data if timeout occurred when waiting for required succeeding context lines.

timeout

Timeout value. Zero if no timeout needed.

ctx_lines_before

Number of context lines to capture before the matching line. Cannot be greater than MAX_HISTORY_LINES.

ctx_lines_after

Number of context lines to capture after the matching lines.

Returns:

Entry handle.

void Set_line_handler(Line_handler handler)

Set handler for pre-filtering all received lines.