Maipa's Standard Library Extension 1.5.6
mstd
Loading...
Searching...
No Matches
event_handler.hpp
1/*
2 * mstd - Maipa's Standard Library
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/mstd/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _MSTD_EVENT_HANDLER_HPP_
12 #define _MSTD_EVENT_HANDLER_HPP_
13
14 #include <mstd/config.hpp>
15
17_MSTD_WARNING("this is only available for c++17 and greater!");
18 #else
19
20 #include <mstd/events_types.hpp>
21 #include <mstd/functions.hpp>
22 #include <mstd/stable_vector.hpp>
23
24namespace mstd {
25 template<class... Args>
27 public:
28 using event_type = void(Args&&...);
30
32
34
35 private:
37
38 public:
39 _MSTD_CONSTEXPR20 event_handler() noexcept = default;
40 _MSTD_CONSTEXPR20 ~event_handler() noexcept = default;
41
46
47 _MSTD_CONSTEXPR20 bool remove_callback(const id_type callbackId) {
48 if (!_events.has_value(callbackId)) { return false; }
50 return true;
51 }
52
54
56 template<class... InvokeArgs>
58 #else
59 template<class... InvokeArgs, std::enable_if_t<std::is_invocable_v<event_type, InvokeArgs...>, bool> = true>
60 #endif
62 if (_events.empty()) { return; }
63
64 // SAFETY WHEN CALLBACK DELETES ITSELF
68
69 for (const auto& callback : callbacksToRun) { callback(std::forward<InvokeArgs>(args)...); }
70 }
71
73
75
77 template<class... InvokeArgs>
79 #else
80 template<class... InvokeArgs, std::enable_if_t<std::is_invocable_v<event_type, InvokeArgs...>, bool> = true>
81 #endif
85 };
86} // namespace mstd
87 #endif
88#endif
Definition event_handler.hpp:26
_MSTD_CONSTEXPR20 event_handler() noexcept=default
void(Args &&...) event_type
Definition event_handler.hpp:28
events_type _events
Definition event_handler.hpp:36
_MSTD_CONSTEXPR20 void remove_all_callbacks()
Definition event_handler.hpp:53
_MSTD_CONSTEXPR20 ~event_handler() noexcept=default
_MSTD_CONSTEXPR20 bool remove_callback(const id_type callbackId)
Definition event_handler.hpp:47
_MSTD_CONSTEXPR20 id_type add_callback(const event_action_handler &callback)
Definition event_handler.hpp:42
#define _MSTD_HAS_CXX17
Definition config.hpp:45
#define _MSTD_HAS_CXX20
Definition config.hpp:52
#define _MSTD_TYPENAME17
Definition config.hpp:82
#define _MSTD_CONSTEXPR20
Definition config.hpp:84
Definition arithmetic_types.hpp:23