Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Concepts.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 <boost/asio/spawn.hpp>
23
24#include <chrono>
25#include <concepts>
26#include <functional>
27#include <optional>
28#include <type_traits>
29
30namespace util::async {
31
40 virtual ~ExecutionContextTag() = default;
41};
42
51template <typename T>
52concept SomeExecutionContext = std::derived_from<std::remove_cvref_t<T>, ExecutionContextTag>;
53
57template <typename T>
58concept SomeStoppable = requires(T v) {
59 { v.requestStop() } -> std::same_as<void>;
60};
61
65template <typename T>
66concept SomeCancellable = requires(T v) {
67 { v.cancel() } -> std::same_as<void>;
68};
69
73template <typename T>
74concept SomeAwaitable = requires(T v) {
75 { v.wait() } -> std::same_as<void>;
76};
77
81template <typename T>
82concept SomeAbortable = requires(T v) {
83 { v.abort() } -> std::same_as<void>;
84};
85
89template <typename T>
91
95template <typename T>
96concept SomeOperationWithData = SomeOperation<T> and requires(T v) {
97 { v.get() };
98};
99
103template <typename T>
105 { v.invoke() };
106};
107
111template <typename T>
113
117template <typename T>
119
123template <typename T>
124concept SomeOutcome = requires(T v) {
125 { v.getOperation() } -> SomeOperation;
126};
127
131template <typename T>
132concept SomeStopToken = requires(T v) {
133 { v.isStopRequested() } -> std::same_as<bool>;
134};
135
139template <typename T>
140concept SomeYieldStopSource = requires(T v, boost::asio::yield_context yield) {
141 { v[yield] } -> SomeStopToken;
142};
143
147template <typename T>
148concept SomeSimpleStopSource = requires(T v) {
149 { v.getToken() } -> SomeStopToken;
150};
151
155template <typename T>
157
161template <typename T>
162concept SomeStopSourceProvider = requires(T v) {
163 { v.getStopSource() } -> SomeStopSource;
164};
165
169template <typename T>
171
175template <typename T>
176concept SomeHandlerWithoutStopToken = requires(T fn) {
177 { std::invoke(fn) };
178};
179
183template <typename T, typename... Args>
184concept SomeHandlerWith = requires(T fn) {
185 { std::invoke(fn, std::declval<Args>()...) };
186};
187
191template <typename T>
192concept SomeStdDuration = requires {
193 // Thank you Ed Catmur for this trick.
194 // See
195 // https://stackoverflow.com/questions/74383254/concept-that-models-only-the-stdchrono-duration-types
196 []<typename Rep, typename Period>( //
197 std::type_identity<std::chrono::duration<Rep, Period>>
198 ) {}(std::type_identity<std::decay_t<T>>());
199};
200
204template <typename T>
205concept SomeStdOptional = requires {
206 []<typename Type>( //
207 std::type_identity<std::optional<Type>>
208 ) {}(std::type_identity<std::decay_t<T>>());
209};
210
214template <typename T>
215concept SomeOptStdDuration = SomeStdOptional<T> and SomeStdDuration<decltype(T{}.value())>;
216
220template <typename T, typename Erased>
221concept NotSameAs = not std::is_same_v<std::decay_t<T>, Erased>;
222
226template <typename T, typename Erased>
227concept RValueNotSameAs = requires(T&& t) {
228 requires std::is_rvalue_reference_v<decltype(t)>;
229 requires NotSameAs<T, Erased>;
230};
231
232} // namespace util::async
Checks that decayed T s not of the same type as Erased.
Definition Concepts.hpp:221
Check that T is an r-value and is not the same type as Erased.
Definition Concepts.hpp:227
Specifies the interface for an operation that can be aborted.
Definition Concepts.hpp:82
Specifies the interface for an operation that can be awaited.
Definition Concepts.hpp:74
Specifies the interface for an operation that can be cancelled.
Definition Concepts.hpp:118
Specifies the interface for an entity that can be cancelled.
Definition Concepts.hpp:66
Concept that identifies types derived from ExecutionContextTag.
Definition Concepts.hpp:52
Specifies the interface for an operation that can force-invoked.
Definition Concepts.hpp:104
Specifies the interface for a handler that can be invoked with the specified args.
Definition Concepts.hpp:184
Specifies the interface for a handler that can be stopped.
Definition Concepts.hpp:176
Specifies the interface for an operation.
Definition Concepts.hpp:96
Specifies the interface for an operation.
Definition Concepts.hpp:90
Specifies that the type must be some std::duration wrapped in an optional.
Definition Concepts.hpp:215
Specifies the interface for an outcome (promise).
Definition Concepts.hpp:124
Specifies the interface for a simple stop token.
Definition Concepts.hpp:148
Specifies that the type must be some std::duration.
Definition Concepts.hpp:192
Specifies that the type must be some std::optional.
Definition Concepts.hpp:205
Specifies the interface for a provider of stop sources.
Definition Concepts.hpp:162
Specifies the interface for a stop source.
Definition Concepts.hpp:156
Specifies the interface for a stop token.
Definition Concepts.hpp:132
Specifies the interface for an operation that can be stopped.
Definition Concepts.hpp:112
Specifies the interface for an outcome (promise) that can be stopped.
Definition Concepts.hpp:170
Specifies the interface for an entity that can be stopped.
Definition Concepts.hpp:58
Specifies the interface for a stop token that internally uses a boost::asio::yield_context.
Definition Concepts.hpp:140
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36
Tag type for identifying execution context types.
Definition Concepts.hpp:39