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/Concepts.hpp"
23#include "util/async/Error.hpp"
24#include "util/async/impl/ErasedOperation.hpp"
25
26#include <fmt/core.h>
27#include <fmt/std.h>
28
29#include <any>
30#include <expected>
31#include <thread>
32#include <type_traits>
33#include <utility>
34
35// TODO: In the future, perhaps cancel and requestStop should be combined into one.
36// Users of the library should not care whether the operation is cancellable or stoppable - users just want to cancel
37// it whatever that means internally.
38
39namespace util::async {
40
44template <typename RetType>
46public:
52 /* implicit */ AnyOperation(impl::ErasedOperation&& operation) : operation_{std::move(operation)}
53 {
54 }
55
56 ~AnyOperation() = default;
57
58 AnyOperation(AnyOperation const&) = delete;
59 AnyOperation(AnyOperation&&) = default;
61 operator=(AnyOperation const&) = delete;
63 operator=(AnyOperation&&) = default;
64
69 void
70 wait() noexcept
71 {
72 operation_.wait();
73 }
74
80 void
81 abort() noexcept
82 {
83 operation_.abort();
84 }
85
91 [[nodiscard]] std::expected<RetType, ExecutionError>
93 {
94 try {
95 auto data = operation_.get();
96 if (not data)
97 return std::unexpected(std::move(data).error());
98
99 if constexpr (std::is_void_v<RetType>) {
100 return {};
101 } else {
102 return std::any_cast<RetType>(std::move(data).value());
103 }
104
105 } catch (std::bad_any_cast const& e) {
106 return std::unexpected{ExecutionError(fmt::format("{}", std::this_thread::get_id()), "Bad any cast")};
107 }
108 }
109
110private:
111 impl::ErasedOperation operation_;
112};
113
114} // namespace util::async
A type-erased operation that can be executed via AnyExecutionContext.
Definition AnyOperation.hpp:45
AnyOperation(impl::ErasedOperation &&operation)
Construct a new type-erased Operation object.
Definition AnyOperation.hpp:52
void abort() noexcept
Abort the operation.
Definition AnyOperation.hpp:81
void wait() noexcept
Wait for the operation to complete.
Definition AnyOperation.hpp:70
std::expected< RetType, ExecutionError > get()
Get the result of the operation.
Definition AnyOperation.hpp:92
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