AsyncBerkeley
Asynchronous Berkeley sockets. Simple.
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1/* Copyright 2025 Kevin Exton
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
20#pragma once
21#ifndef IO_CONCEPTS_HPP
22#define IO_CONCEPTS_HPP
23#include "io/config.h"
24#if OS_WINDOWS
25#else
26#include "platforms/posix/concepts_posix.hpp" // IWYU pragma: export
27#endif
28#include "io/socket/detail/socket.hpp"
29
30#include <memory>
31#include <optional>
32#include <type_traits>
33// Forward declarations
34namespace io {
35namespace socket {
36class socket_handle;
37} // namespace socket
38namespace execution {
39enum struct execution_trigger : std::uint8_t;
40} // namespace execution
41} // namespace io
42
47namespace io {
48
50template <typename Allocator>
52 requires(Allocator alloc, typename Allocator::value_type *ptr) {
53 { alloc.allocate(0) } -> std::same_as<decltype(ptr)>;
54 { alloc.deallocate(ptr, 0) } -> std::same_as<void>;
55 };
56
58template <typename Lock>
59concept BasicLockable = requires(Lock lock) {
60 { lock.lock() } -> std::same_as<void>;
61 { lock.unlock() } -> std::same_as<void>;
62};
63
65template <typename B>
66concept ScatterGatherLike = requires(B buf) {
67 std::ranges::data(buf);
68 std::ranges::size(buf);
69};
70
79template <typename T>
80concept MuxTag = requires(T tag) {
81 typename T::event_type;
82 typename T::interval_type;
83 typename T::size_type;
84 typename T::template is_eager_t<T>;
85 T::template is_eager_v<T>;
86};
87
92template <typename Fn>
93concept Completion = requires(Fn &&func) {
94 requires std::is_invocable_v<Fn>;
95 typename std::invoke_result_t<Fn>::value_type;
96 requires std::is_same_v<
97 std::invoke_result_t<Fn>,
98 std::optional<typename std::invoke_result_t<Fn>::value_type>>;
99};
100
109template <typename T>
110concept Multiplexer = requires(T mux) {
111 requires MuxTag<T>;
112 typename T::demultiplexer;
113 typename T::template sender<decltype([]() -> std::optional<int> {
114 return std::nullopt;
115 })>;
116 mux.set(std::shared_ptr<socket::socket_handle>{},
117 execution::execution_trigger{},
118 []() -> std::optional<int> { return std::nullopt; });
119 mux.wait_for(typename T::interval_type{});
120};
121
126template <typename Socket>
127concept SocketLike = requires {
128 requires std::is_constructible_v<Socket, socket::native_socket_type>;
129 static_cast<socket::native_socket_type>(Socket{});
130};
131
136template <typename Dialog>
137concept DialogLike = requires(Dialog &dialog) {
138 typename Dialog::executor_type;
139 dialog.executor;
140 dialog.socket;
141};
142
147template <typename Message>
148concept MessageLike =
149 requires(Message msg) { static_cast<socket::socket_message_type>(msg); };
150
151} // namespace io
152#endif // IO_CONCEPTS_HPP
A concept that checks if a type is an allocator.
Definition concepts.hpp:51
A concept that validates the C++ BasicLockable named requirement.
Definition concepts.hpp:59
Concept for a completion handler.
Definition concepts.hpp:93
Concept for types that behave like a dialog.
Definition concepts.hpp:137
Concept for types that behave like a socket message.
Definition concepts.hpp:148
Concept for a multiplexer.
Definition concepts.hpp:110
Concept for a multiplexer tag.
Definition concepts.hpp:80
A concept that describes a scatter/gather like buffer object.
Definition concepts.hpp:66
Concept for types that behave like a socket.
Definition concepts.hpp:127
This file defines various macros that configure AsyncBerkeley.
The io namespace contains all the functions and classes for the I/O library.
Definition executor.hpp:33