class ugcs::vsm::Operation_waiter

Overview

Class for synchronizing with request execution. More…

#include <operation_waiter.h>

class Operation_waiter
{
public:
    // typedefs

    typedef std::unique_ptr<Operation_waiter> Ptr;
    typedef Callback_proxy<void, Ptr> Timeout_handler;

    // construction

    Operation_waiter(Request::Ptr request = nullptr);

    template <class Request_type>
    Operation_waiter(std::shared_ptr<Request_type> request);

    Operation_waiter(Operation_waiter&&);
    Operation_waiter(const Operation_waiter&);

    // methods

    Operation_waiter& operator = (Operation_waiter&&);
    Operation_waiter& operator = (const Operation_waiter&);
    operator bool () const;

    bool Wait(
        bool process_ctx = true,
        std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()
        );

    void Timeout(
        std::chrono::milliseconds timeout,
        Timeout_handler timeout_handler = Timeout_handler(),
        bool cancel_operation = true,
        Request_completion_context::Ptr ctx = nullptr
        );

    void Cancel();
    void Abort();
    bool Is_done() const;
};

Detailed Documentation

Class for synchronizing with request execution.

Instance of this class should be returned by value from asynchronous methods of request processors. Only one instance is allowed (copying is disabled).

Typedefs

typedef std::unique_ptr<Operation_waiter> Ptr

Pointer type when used as argument in callbacks.

typedef Callback_proxy<void, Ptr> Timeout_handler

Timeout handler prototype.

Construction

Operation_waiter(Request::Ptr request = nullptr)

Construct operation waiter.

Parameters:

request

Associated request. No request is associated if nullptr. In such case it is dummy waiter object which behaves like its associated request is already done.

template <class Request_type>
Operation_waiter(std::shared_ptr<Request_type> request)

Construct operation waiter from any type of request pointer.

Operation_waiter(Operation_waiter&&)

Default move constructor.

Operation_waiter(const Operation_waiter&)

Copy constructor disabled.

Having more than one copy of operation waiter is confusing and error prone.

Methods

Operation_waiter& operator = (Operation_waiter&&)

Move assignment operator.

If the waiter instance which is assigned to is not empty, it is properly destroyed.

Operation_waiter& operator = (const Operation_waiter&)

Assignment operator is disabled.

Having more than one copy of operation waiter is confusing and error prone.

operator bool () const

Test the waiter has request associated.

bool Wait(
    bool process_ctx = true,
    std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()
    )

Wait for request is fully processed, i.e.

all handlers were invoked and no more actions pending.

Parameters:

process_ctx

Process all requests in the associated completion context. This may be useful if the context is normally processed in the calling thread.

timeout

Timeout value for waiting. Zero value indicates indefinite waiting.

Returns:

“true” if request was processed, “false” if the function returned by timeout expiration.

void Timeout(
    std::chrono::milliseconds timeout,
    Timeout_handler timeout_handler = Timeout_handler(),
    bool cancel_operation = true,
    Request_completion_context::Ptr ctx = nullptr
    )

Schedule timeout for the operation.

After the specified time the optional callback is invoked and the operation is optionally canceled.

Parameters:

timeout

Timeout for the operation.

timeout_handler

Handler which should be invoked when timeout elapses. The handler should have constant reference to Operation_waiter::Ptr as the first argument.

cancel_operation

Cancel the operation after the timeout elapses if “true”.

ctx

Context used for handler invocation. By default the handler is invoked in request completion context, which should be present in this case.

void Cancel()

Cancel the operation.

This action behavior is defined by specific operation and processor.

void Abort()

Abort the operation.

Completion handler will not be executed, unless it has been already executed or execution already started.

bool Is_done() const

Check if request is fully processed, i.e.

all handlers were invoked and no more actions pending.