mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
callback-system.hpp
Go to the documentation of this file.
1
#pragma once
2
#include <functional>
7
namespace
mal_toolkit
8
{
17
template
<
typename
T,
typename
... U>
18
size_t
GetFunctionAddress
(std::function<T(U...)> f)
19
{
20
typedef
T(FnType)(U...);
21
FnType **fnPointer = f.template target<FnType *>();
22
return
reinterpret_cast<
size_t
>
(*fnPointer);
23
}
24
// TODO (malatindez): improve this later
25
// add support for class methods
26
33
template
<
typename
F>
34
class
CallbackSystem
35
{
36
public
:
37
using
CallbackFunction
= F;
38
using
CallbackId
= uint32_t;
39
using
CallbackAddress
=
size_t
;
40
47
CallbackId
AddCallback
(
CallbackFunction
const
&callback)
48
{
49
auto
id
=
next_id_
++;
50
callbacks_
.emplace(
id
, std::move(callback));
51
callback_addresses_
.emplace(
id
,
GetFunctionAddress
(callback));
52
return
id;
53
}
54
60
void
RemoveCallback
(
CallbackId
id
)
61
{
62
callbacks_
.erase(
id
);
63
callback_addresses_
.erase(
id
);
64
}
65
71
void
RemoveCallback
(
CallbackFunction
const
&callback)
72
{
73
auto
address =
GetFunctionAddress
(callback);
74
for
(
auto
const
&[
id
, callback_address] :
callback_addresses_
)
75
{
76
if
(callback_address == address)
77
{
78
RemoveCallback
(
id
);
79
return
;
80
}
81
}
82
}
83
89
void
InvokeCallbacks
(T
const
&data)
90
{
91
for
(
auto
const
&[
id
, callback] :
callbacks_
)
92
{
93
callback(data);
94
}
95
}
96
97
private
:
98
std::unordered_map<CallbackId, Callback>
callbacks_
;
99
std::unordered_map<CallbackId, CallbackAddress>
callback_addresses_
;
100
CallbackId
next_id_
= 0;
101
};
102
}
// namespace mal_toolkit
mal_toolkit::CallbackSystem
A system for managing and invoking callback functions.
Definition
callback-system.hpp:35
mal_toolkit::CallbackSystem::next_id_
CallbackId next_id_
Definition
callback-system.hpp:100
mal_toolkit::CallbackSystem::callbacks_
std::unordered_map< CallbackId, Callback > callbacks_
Definition
callback-system.hpp:98
mal_toolkit::CallbackSystem::InvokeCallbacks
void InvokeCallbacks(T const &data)
Invoke all registered callbacks with the provided data.
Definition
callback-system.hpp:89
mal_toolkit::CallbackSystem::CallbackFunction
F CallbackFunction
Definition
callback-system.hpp:37
mal_toolkit::CallbackSystem::AddCallback
CallbackId AddCallback(CallbackFunction const &callback)
Add a new callback function.
Definition
callback-system.hpp:47
mal_toolkit::CallbackSystem::RemoveCallback
void RemoveCallback(CallbackFunction const &callback)
Remove a callback by its function object.
Definition
callback-system.hpp:71
mal_toolkit::CallbackSystem::callback_addresses_
std::unordered_map< CallbackId, CallbackAddress > callback_addresses_
Definition
callback-system.hpp:99
mal_toolkit::CallbackSystem::RemoveCallback
void RemoveCallback(CallbackId id)
Remove a callback by its ID.
Definition
callback-system.hpp:60
mal_toolkit::CallbackSystem::CallbackId
uint32_t CallbackId
Definition
callback-system.hpp:38
size_t
mal_toolkit
Contains a collection of tools and utilities provided by the MAL Toolkit library.
Definition
backoffs.hpp:7
mal_toolkit::GetFunctionAddress
size_t GetFunctionAddress(std::function< T(U...)> f)
Get the address of a callable function.
Definition
callback-system.hpp:18
third_party
mal-toolkit
mal-toolkit
mal-toolkit
callback-system.hpp
Generated on Sun Jul 14 2024 00:02:52 for mal-packet-weaver by
1.11.0