Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
callback_manager.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11#pragma once
12
21#include <cstdint>
22#include <functional>
23#include <mutex>
24#include <system_error>
25#include <tuple>
26#include <utility>
27#include <vector>
28
30{
31
60 template<typename... CallbackTypes>
62 {
63 public:
67 callback_manager() = default;
68
72 ~callback_manager() = default;
73
74 // Non-copyable, movable
77 callback_manager(callback_manager&&) noexcept = default;
78 callback_manager& operator=(callback_manager&&) noexcept = default;
79
85 template<std::size_t Index>
86 auto set(std::tuple_element_t<Index, std::tuple<CallbackTypes...>> callback) -> void
87 {
88 std::lock_guard lock(mutex_);
89 std::get<Index>(callbacks_) = std::move(callback);
90 }
91
97 template<typename T>
98 auto set(T callback) -> void
99 {
100 std::lock_guard lock(mutex_);
101 std::get<T>(callbacks_) = std::move(callback);
102 }
103
109 template<std::size_t Index>
110 [[nodiscard]] auto get() const -> std::tuple_element_t<Index, std::tuple<CallbackTypes...>>
111 {
112 std::lock_guard lock(mutex_);
113 return std::get<Index>(callbacks_);
114 }
115
121 template<typename T>
122 [[nodiscard]] auto get() const -> T
123 {
124 std::lock_guard lock(mutex_);
125 return std::get<T>(callbacks_);
126 }
127
138 template<std::size_t Index, typename... Args>
139 auto invoke(Args&&... args) -> void
140 {
141 auto callback = get<Index>();
142 if (callback)
143 {
144 callback(std::forward<Args>(args)...);
145 }
146 }
147
154 template<typename T, typename... Args>
155 auto invoke(Args&&... args) -> void
156 {
157 auto callback = get<T>();
158 if (callback)
159 {
160 callback(std::forward<Args>(args)...);
161 }
162 }
163
171 template<std::size_t Index, typename... Args>
172 auto invoke_if(bool condition, Args&&... args) -> void
173 {
174 if (condition)
175 {
176 invoke<Index>(std::forward<Args>(args)...);
177 }
178 }
179
183 auto clear() -> void
184 {
185 std::lock_guard lock(mutex_);
186 callbacks_ = std::tuple<CallbackTypes...>{};
187 }
188
193 template<std::size_t Index>
194 auto clear() -> void
195 {
196 std::lock_guard lock(mutex_);
197 std::get<Index>(callbacks_) = {};
198 }
199
200 private:
201 mutable std::mutex mutex_;
202 std::tuple<CallbackTypes...> callbacks_;
203 };
204
205 // Convenience type aliases for common callback configurations
206
217 std::function<void(const std::vector<uint8_t>&)>, // receive
218 std::function<void()>, // connected
219 std::function<void()>, // disconnected
220 std::function<void(std::error_code)> // error
221 >;
222
227 {
228 static constexpr std::size_t receive = 0;
229 static constexpr std::size_t connected = 1;
230 static constexpr std::size_t disconnected = 2;
231 static constexpr std::size_t error = 3;
232 };
233
234} // namespace kcenon::network::utils
Thread-safe callback storage and invocation utility.
auto invoke(Args &&... args) -> void
Invokes a callback at the specified index if set.
auto clear() -> void
Clears a specific callback.
auto clear() -> void
Clears all callbacks.
callback_manager()=default
Default constructor.
std::tuple< CallbackTypes... > callbacks_
callback_manager(const callback_manager &)=delete
auto get() const -> T
Gets a callback by type.
auto set(T callback) -> void
Sets a callback by type.
callback_manager & operator=(const callback_manager &)=delete
callback_manager(callback_manager &&) noexcept=default
auto set(std::tuple_element_t< Index, std::tuple< CallbackTypes... > > callback) -> void
Sets a callback at the specified index.
auto invoke_if(bool condition, Args &&... args) -> void
Invokes a callback conditionally.
auto get() const -> std::tuple_element_t< Index, std::tuple< CallbackTypes... > >
Gets a callback at the specified index.
Utility components for network_system.
Callback indices for tcp_client_callbacks.