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>
82concept SomeForceInvocableOperation = SomeOperation<T> and requires(T v) {
83 { v.invoke() };
84};
85
89template <typename T>
91
95template <typename T>
97
101template <typename T>
102concept SomeOutcome = requires(T v) {
103 { v.getOperation() } -> SomeOperation;
104};
105
109template <typename T>
110concept SomeStopToken = requires(T v) {
111 { v.isStopRequested() } -> std::same_as<bool>;
112};
113
117template <typename T>
118concept SomeYieldStopSource = requires(T v, boost::asio::yield_context yield) {
119 { v[yield] } -> SomeStopToken;
120};
121
125template <typename T>
126concept SomeSimpleStopSource = requires(T v) {
127 { v.getToken() } -> SomeStopToken;
128};
129
133template <typename T>
135
139template <typename T>
140concept SomeStopSourceProvider = requires(T v) {
141 { v.getStopSource() } -> SomeStopSource;
142};
143
147template <typename T>
149
153template <typename T>
154concept SomeHandlerWithoutStopToken = requires(T fn) {
155 { std::invoke(fn) };
156};
157
161template <typename T, typename... Args>
162concept SomeHandlerWith = requires(T fn) {
163 { std::invoke(fn, std::declval<Args>()...) };
164};
165
169template <typename T>
170concept SomeStdDuration = requires {
171 // Thank you Ed Catmur for this trick.
172 // See https://stackoverflow.com/questions/74383254/concept-that-models-only-the-stdchrono-duration-types
173 []<typename Rep, typename Period>( //
174 std::type_identity<std::chrono::duration<Rep, Period>>
175 ) {}(std::type_identity<std::decay_t<T>>());
176};
177
181template <typename T>
182concept SomeStdOptional = requires {
183 []<typename Type>( //
184 std::type_identity<std::optional<Type>>
185 ) {}(std::type_identity<std::decay_t<T>>());
186};
187
191template <typename T>
192concept SomeOptStdDuration = SomeStdOptional<T> and SomeStdDuration<decltype(T{}.value())>;
193
197template <typename T, typename Erased>
198concept NotSameAs = not std::is_same_v<std::decay_t<T>, Erased>;
199
203template <typename T, typename Erased>
204concept RValueNotSameAs = requires(T&& t) {
205 requires std::is_rvalue_reference_v<decltype(t)>;
206 requires NotSameAs<T, Erased>;
207};
208
209} // namespace util::async
Checks that decayed T s not of the same type as Erased.
Definition Concepts.hpp:198
Check that T is an r-value and is not the same type as Erased.
Definition Concepts.hpp:204
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:96
Specifies the interface for an entity that can be cancelled.
Definition Concepts.hpp:44
Specifies the interface for an operation that can force-invoked.
Definition Concepts.hpp:82
Specifies the interface for a handler that can be invoked with the specified args.
Definition Concepts.hpp:162
Specifies the interface for a handler that can be stopped.
Definition Concepts.hpp:154
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:192
Specifies the interface for an outcome (promise)
Definition Concepts.hpp:102
Specifies the interface for a simple stop token.
Definition Concepts.hpp:126
Specifies that the type must be some std::duration.
Definition Concepts.hpp:170
Specifies that the type must be some std::optional.
Definition Concepts.hpp:182
Specifies the interface for a provider of stop sources.
Definition Concepts.hpp:140
Specifies the interface for a stop source.
Definition Concepts.hpp:134
Specifies the interface for a stop token.
Definition Concepts.hpp:110
Specifies the interface for an operation that can be stopped.
Definition Concepts.hpp:90
Specifies the interface for an outcome (promise) that can be stopped.
Definition Concepts.hpp:148
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:118
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36