class ugcs::vsm::Request_container

Overview

Generic container for queued requests. More…

#include <request_container.h>

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

    typedef std::shared_ptr<Request_container> Ptr;
    typedef std::weak_ptr<Request_container> Weak_ptr;

    // enums

    enum Type;

    // classes

    class Request;
    class Request_waiter;

    // construction

    Request_container(const std::string& name, Request_waiter::Ptr waiter = Request_waiter::Create());

    // methods

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

    virtual Type Get_type() const;
    bool Check_type(Type mask);
    void Submit_request(Request::Ptr request);
    void Submit_request_locked(Request::Ptr request, Request_waiter::Locker locker);
    int Process_requests(int requests_limit = 0);
    int Process_requests(std::unique_lock<std::mutex>& lock, int requests_limit = 0);
    void Set_waiter(Request_waiter::Ptr waiter);
    Request_waiter::Ptr Get_waiter() const;
    const std::string& Get_name();
    void Enable();
    void Disable();
    bool Is_enabled() const;
};

// direct descendants

template <bool is_processor>
class Request_context;

Detailed Documentation

Generic container for queued requests.

It provides interface for submitting requests into the container and executing associated handlers for queued requests.

Typedefs

typedef std::shared_ptr<Request_container> Ptr

Pointer type.

typedef std::weak_ptr<Request_container> Weak_ptr

Pointer type.

Construction

Request_container(const std::string& name, Request_waiter::Ptr waiter = Request_waiter::Create())

Create container with default associated waiter.

Methods

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

Create an instance.

virtual Type Get_type() const

Get this container type.

bool Check_type(Type mask)

Check if the container type conforms the specified characteristic mask.

void Submit_request(Request::Ptr request)

Submit request to this container for further processing or notification handlers invocation.

Parameters:

request

Request to submit.

void Submit_request_locked(Request::Ptr request, Request_waiter::Locker locker)

The same as Submit_request, but with previously acquired lock of the associated waiter.

Lock should be with notify.

int Process_requests(int requests_limit = 0)

Process all currently queued requests.

Parameters:

requests_limit

Limit of requests to process at once. Zero means no limit.

Returns:

Number of requests processed.

int Process_requests(std::unique_lock<std::mutex>& lock, int requests_limit = 0)

Process all currently queued requests.

This version does not lock the waiter but uses provided mutex instead assuming it is locked when called. Intended for use from Request_waiter::Wait_and_process.

Parameters:

lock

External lock to use for protecting queue access.

requests_limit

Limit of requests to process at once. Zero means no limit.

Returns:

Number of requests processed.

void Set_waiter(Request_waiter::Ptr waiter)

Set request waiter associated with this container.

Parameters:

waiter

Waiter object.

Request_waiter::Ptr Get_waiter() const

Get request waiter associated with this container.

const std::string& Get_name()

Get the name of the container.

void Enable()

Enable the container.

The container is ready to accept requests after that. Derived class can start dedicated threads there.

Parameters:

Invalid_op_exception

if the container is already enabled.

void Disable()

Disable the container.

All requests which are submitted after this method is called will be aborted. It is up to container implementation to decide whether to process or abort requests which are already submitted or being processed. If a container has dedicated threads, it should terminate them (should be done synchronously in On_disable() method). Method must be called from the same thread as Enable method.

bool Is_enabled() const

Check if the container is currently enabled.