AsyncBerkeley
Asynchronous Berkeley sockets. Simple.
Loading...
Searching...
No Matches
triggers.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_TRIGGERS_HPP
22#define IO_TRIGGERS_HPP
23#include "executor.hpp"
27
28#include <memory>
33namespace io::execution {
38template <Multiplexer Mux> class basic_triggers {
39public:
46
48 basic_triggers() = default;
49
51 basic_triggers(const basic_triggers &) = delete;
52
55
57 template <AllocatorLike Allocator>
58 explicit basic_triggers(const Allocator &alloc = Allocator()) noexcept(
59 noexcept(Allocator()))
60 : executor_{std::allocate_shared<executor_type>(alloc, alloc)}
61 {}
62
64 auto operator=(const basic_triggers &) -> basic_triggers & = delete;
65
67 auto operator=(basic_triggers &&) -> basic_triggers & = default;
68
79 template <SocketLike Socket>
80 auto push(std::shared_ptr<Socket> socket) -> socket_dialog
81 {
82 return {executor_, executor_type::push(std::move(socket))};
83 }
84
91 template <typename... Args> auto emplace(Args &&...args) -> socket_dialog
92 {
93 return {executor_, executor_type::emplace(std::forward<Args>(args)...)};
94 }
95
102 template <typename... Args> auto set(Args &&...args) -> decltype(auto)
103 {
104 return executor_->set(std::forward<Args>(args)...);
105 }
106
112 constexpr auto wait_for(int interval = -1) -> decltype(auto)
113 {
114 return executor_->wait_for(interval);
115 }
116
121 constexpr auto wait() -> decltype(auto) { return executor_->wait(); }
122
127 [[nodiscard]] auto on_empty() -> decltype(auto)
128 {
129 return executor_->on_empty();
130 }
131
136 [[nodiscard]] auto
137 get_executor() const noexcept -> std::weak_ptr<executor_type>
138 {
139 return executor_;
140 }
141
143 ~basic_triggers() = default;
144
145private:
147 std::shared_ptr<executor_type> executor_{std::make_shared<executor_type>()};
148};
149
150} // namespace io::execution
151#endif // IO_TRIGGERS_HPP
A class that provides a high-level interface for an executor.
Definition triggers.hpp:38
basic_triggers()=default
Default constructor.
auto set(Args &&...args) -> decltype(auto)
Sets a completion handler for an event.
Definition triggers.hpp:102
constexpr auto wait_for(int interval=-1) -> decltype(auto)
Waits for events to occur.
Definition triggers.hpp:112
auto push(std::shared_ptr< Socket > socket) -> socket_dialog
Constructs a socket_dialog associated to the executor.
Definition triggers.hpp:80
basic_triggers(const basic_triggers &)=delete
Deleted copy constructor.
basic_triggers(const Allocator &alloc=Allocator()) noexcept(noexcept(Allocator()))
Construct with an allocator.
Definition triggers.hpp:58
auto emplace(Args &&...args) -> socket_dialog
In-place constructs a socket_dialog associated to the executor.
Definition triggers.hpp:91
auto operator=(const basic_triggers &) -> basic_triggers &=delete
Deleted copy assignment operator.
auto operator=(basic_triggers &&) -> basic_triggers &=default
Default move assignment operator.
auto on_empty() -> decltype(auto)
Sends a notice when the triggers are empty.
Definition triggers.hpp:127
~basic_triggers()=default
Default destructor.
constexpr auto wait() -> decltype(auto)
Waits for events to occur.
Definition triggers.hpp:121
basic_triggers(basic_triggers &&)=default
Default move constructor.
auto get_executor() const noexcept -> std::weak_ptr< executor_type >
Gets the executor.
Definition triggers.hpp:137
An executor that uses a multiplexer to wait for events.
Definition executor.hpp:46
static auto emplace(Args &&...args) -> std::shared_ptr< socket_handle >
Emplaces a socket handle in the collection.
Definition executor.hpp:102
static auto push(std::shared_ptr< Socket > socket) -> decltype(auto)
Configures the socket to be non-blocking.
Definition executor.hpp:77
A thread-safe, move-only RAII wrapper for a native socket handle.
Definition socket_handle.hpp:42
This file defines concepts for the execution components.
This file defines a generic executor for the I/O library.
Provides high-level interfaces for executors and completion triggers.
Definition executor.hpp:33
Defines the socket_dialog struct.
Cross-platform, thread-safe RAII socket wrapper.
A dialog that facilitates asynchronous operations on the socket by the executor.
Definition socket_dialog.hpp:44