olp-cpp-sdk  1.21.0
Classes | Public Types | Public Member Functions | Friends | List of all members
olp::client::CancellationContext Class Reference

A wrapper that manages the cancellation state of an asynchronous operation in a thread-safe way. More...

#include <CancellationContext.h>

Public Types

using ExecuteFuncType = std::function< CancellationToken()>
 An alias for the operation function.
 
using CancelFuncType = std::function< void()>
 An alias for the cancellation function.
 

Public Member Functions

bool ExecuteOrCancelled (const ExecuteFuncType &execute_fn, const CancelFuncType &cancel_fn=nullptr)
 Executes the given cancellable code block if the operation is not cancelled. More...
 
void CancelOperation ()
 Cancels the ongoing operation in a thread-safe way.
 
bool IsCancelled () const
 Checks whether this context is cancelled. More...
 

Friends

struct CancellationContextHash
 A helper for unordered containers.
 

Detailed Description

A wrapper that manages the cancellation state of an asynchronous operation in a thread-safe way.

All public APIs are thread-safe.

This class is both movable and copyable.

Member Function Documentation

◆ ExecuteOrCancelled()

bool olp::client::CancellationContext::ExecuteOrCancelled ( const ExecuteFuncType execute_fn,
const CancelFuncType cancel_fn = nullptr 
)
inline

Executes the given cancellable code block if the operation is not cancelled.

Otherwise, executes the custom cancellation function.

Parameters
execute_fnThe function that should be executed if this operation is not cancelled. This function should return CancellationToken for which CancellationContext propagates a cancel request.
cancel_fnThe function that is called if this operation is cancelled.
Returns
True if the execution is successful; false if the context was cancelled.

◆ IsCancelled()

bool olp::client::CancellationContext::IsCancelled ( ) const
inline

Checks whether this context is cancelled.

Returns
True if the context is cancelled; false otherwise.

The documentation for this class was generated from the following files: