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
35template <typename T>
36concept SomeStoppable = requires(T v) {
37 { v.requestStop() } -> std::same_as<void>;
38};
39
43template <typename T>
44concept SomeCancellable = requires(T v) {
45 { v.cancel() } -> std::same_as<void>;
46};
47
51template <typename T>
52concept SomeAwaitable = requires(T v) {
53 { v.wait() } -> std::same_as<void>;
54};
55
59template <typename T>
60concept SomeAbortable = requires(T v) {
61 { v.abort() } -> std::same_as<void>;
62};
63
67template <typename T>
69
73template <typename T>
74concept SomeOperationWithData = SomeOperation<T> and requires(T v) {
75 { v.get() };
76};
77
81template <typename T>
83
87template <typename T>
89
93template <typename T>
94concept SomeOutcome = requires(T v) {
95 { v.getOperation() } -> SomeOperation;
96};
97
101template <typename T>
102concept SomeStopToken = requires(T v) {
103 { v.isStopRequested() } -> std::same_as<bool>;
104};
105
109template <typename T>
110concept SomeYieldStopSource = requires(T v, boost::asio::yield_context yield) {
111 { v[yield] } -> SomeStopToken;
112};
113
117template <typename T>
118concept SomeSimpleStopSource = requires(T v) {
119 { v.getToken() } -> SomeStopToken;
120};
121
125template <typename T>
127
131template <typename T>
132concept SomeStopSourceProvider = requires(T v) {
133 { v.getStopSource() } -> SomeStopSource;
134};
135
139template <typename T>
141
145template <typename T>
146concept SomeHandlerWithoutStopToken = requires(T fn) {
147 { std::invoke(fn) };
148};
149
153template <typename T, typename... Args>
154concept SomeHandlerWith = requires(T fn) {
155 { std::invoke(fn, std::declval<Args>()...) };
156};
157
161template <typename T>
162concept SomeStdDuration = requires {
163 // Thank you Ed Catmur for this trick.
164 // See https://stackoverflow.com/questions/74383254/concept-that-models-only-the-stdchrono-duration-types
165 []<typename Rep, typename Period>( //
166 std::type_identity<std::chrono::duration<Rep, Period>>
167 ) {}(std::type_identity<std::decay_t<T>>());
168};
169
173template <typename T>
174concept SomeStdOptional = requires {
175 []<typename Type>( //
176 std::type_identity<std::optional<Type>>
177 ) {}(std::type_identity<std::decay_t<T>>());
178};
179
183template <typename T>
184concept SomeOptStdDuration = SomeStdOptional<T> and SomeStdDuration<decltype(T{}.value())>;
185
189template <typename T, typename Erased>
190concept NotSameAs = not std::is_same_v<std::decay_t<T>, Erased>;
191
195template <typename T, typename Erased>
196concept RValueNotSameAs = requires(T&& t) {
197 requires std::is_rvalue_reference_v<decltype(t)>;
198 requires NotSameAs<T, Erased>;
199};
200
201} // namespace util::async
Checks that decayed T s not of the same type as Erased.
Definition Concepts.hpp:190
Check that T is an r-value and is not the same type as Erased.
Definition Concepts.hpp:196
Specifies the interface for an operation that can be aborted.
Definition Concepts.hpp:60
Specifies the interface for an operation that can be awaited.
Definition Concepts.hpp:52
Specifies the interface for an operation that can be cancelled.
Definition Concepts.hpp:88
Specifies the interface for an entity that can be cancelled.
Definition Concepts.hpp:44
Specifies the interface for a handler that can be invoked with the specified args.
Definition Concepts.hpp:154
Specifies the interface for a handler that can be stopped.
Definition Concepts.hpp:146
Specifies the interface for an operation.
Definition Concepts.hpp:74
Specifies the interface for an operation.
Definition Concepts.hpp:68
Specifies that the type must be some std::duration wrapped in an optional.
Definition Concepts.hpp:184
Specifies the interface for an outcome (promise)
Definition Concepts.hpp:94
Specifies the interface for a simple stop token.
Definition Concepts.hpp:118
Specifies that the type must be some std::duration.
Definition Concepts.hpp:162
Specifies that the type must be some std::optional.
Definition Concepts.hpp:174
Specifies the interface for a provider of stop sources.
Definition Concepts.hpp:132
Specifies the interface for a stop source.
Definition Concepts.hpp:126
Specifies the interface for a stop token.
Definition Concepts.hpp:102
Specifies the interface for an operation that can be stopped.
Definition Concepts.hpp:82
Specifies the interface for an outcome (promise) that can be stopped.
Definition Concepts.hpp:140
Specifies the interface for an entity that can be stopped.
Definition Concepts.hpp:36
Specifies the interface for a stop token that internally uses a boost::asio::yield_context.
Definition Concepts.hpp:110
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36