Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Outcome.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/context/impl/Cancellation.hpp"
23
24#include <concepts>
25#include <future>
26
27namespace util::async {
28
29template <typename RetType, typename StopSourceType>
31
32namespace impl {
33
34template <typename RetType>
36
42template <typename RetType>
44protected:
45 std::promise<RetType> promise_;
46
47public:
48 using DataType = RetType;
49
50 BasicOutcome() = default;
51 BasicOutcome(BasicOutcome&&) = default;
52
53 BasicOutcome(BasicOutcome const&) = delete;
54
60 void
61 setValue(std::convertible_to<RetType> auto&& val)
62 {
63 promise_.set_value(std::forward<decltype(val)>(val));
64 }
65
69 void
71 {
72 promise_.set_value({});
73 }
74
80 [[nodiscard]] std::future<RetType>
82 {
83 return promise_.get_future();
84 }
85};
86
87} // namespace impl
88
94template <typename RetType>
95class Outcome : public impl::BasicOutcome<RetType> {
96public:
104 {
106 }
107};
108
115template <typename RetType, typename StopSourceType>
116class StoppableOutcome : public impl::BasicOutcome<RetType> {
117private:
118 StopSourceType stopSource_;
119
120public:
131
137 [[nodiscard]] StopSourceType&
139 {
140 return stopSource_;
141 }
142};
143
144} // namespace util::async
Unstoppable outcome.
Definition Outcome.hpp:95
impl::BasicOperation< Outcome > getOperation()
Gets the unstoppable operation for this outcome.
Definition Outcome.hpp:103
The future side of async operations that can be stopped.
Definition Outcome.hpp:30
Stoppable outcome.
Definition Outcome.hpp:116
StoppableOperation< RetType, StopSourceType > getOperation()
Gets the stoppable operation for this outcome.
Definition Outcome.hpp:127
StopSourceType & getStopSource()
Gets the stop source for this outcome.
Definition Outcome.hpp:138
Definition Outcome.hpp:35
Base for all promise side of async operations.
Definition Outcome.hpp:43
void setValue(std::convertible_to< RetType > auto &&val)
Sets the value on the inner promise
Definition Outcome.hpp:61
std::future< RetType > getStdFuture()
Get the future for the inner promise
Definition Outcome.hpp:81
void setValue()
Sets the value channel for void operations.
Definition Outcome.hpp:70
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36