Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
AnyOperation.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/async/Error.hpp"
23#include "util/async/impl/ErasedOperation.hpp"
24
25#include <fmt/core.h>
26#include <fmt/std.h>
27
28#include <any>
29#include <expected>
30#include <thread>
31#include <type_traits>
32#include <utility>
33
34// TODO: In the future, perhaps cancel and requestStop should be combined into one.
35// Users of the library should not care whether the operation is cancellable or stoppable - users just want to cancel
36// it whatever that means internally.
37
38namespace util::async {
39
43template <typename RetType>
45public:
51 /* implicit */ AnyOperation(impl::ErasedOperation&& operation) : operation_{std::move(operation)}
52 {
53 }
54
55 ~AnyOperation() = default;
56
57 AnyOperation(AnyOperation const&) = delete;
58 AnyOperation(AnyOperation&&) = default;
60 operator=(AnyOperation const&) = delete;
62 operator=(AnyOperation&&) = default;
63
68 void
69 wait() noexcept
70 {
71 operation_.wait();
72 }
73
79 void
81 {
82 operation_.abort();
83 }
84
90 [[nodiscard]] std::expected<RetType, ExecutionError>
92 {
93 try {
94 auto data = operation_.get();
95 if (not data)
96 return std::unexpected(std::move(data).error());
97
98 if constexpr (std::is_void_v<RetType>) {
99 return {};
100 } else {
101 return std::any_cast<RetType>(std::move(data).value());
102 }
103
104 } catch (std::bad_any_cast const& e) {
105 return std::unexpected{ExecutionError(fmt::format("{}", std::this_thread::get_id()), "Bad any cast")};
106 }
107 }
108
115 void
117 {
118 operation_.invoke();
119 }
120
121private:
122 impl::ErasedOperation operation_;
123};
124
125} // namespace util::async
A type-erased operation that can be executed via AnyExecutionContext.
Definition AnyOperation.hpp:44
void abort()
Abort the operation.
Definition AnyOperation.hpp:80
void invoke()
Force-invoke the operation.
Definition AnyOperation.hpp:116
AnyOperation(impl::ErasedOperation &&operation)
Construct a new type-erased Operation object.
Definition AnyOperation.hpp:51
void wait() noexcept
Wait for the operation to complete.
Definition AnyOperation.hpp:69
std::expected< RetType, ExecutionError > get()
Get the result of the operation.
Definition AnyOperation.hpp:91
Definition ErasedOperation.hpp:34
void abort()
Cancel if needed and request stop as soon as possible.
Definition ErasedOperation.hpp:69
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36
Error channel type for async operation of any ExecutionContext.
Definition Error.hpp:39