36 namespace discord_core_internal {
68 template<event_delegate_token_t value_type>
struct key_hasher<value_type> {
69 DCA_INLINE
static uint64_t getHashKey(
const value_type& data) {
71 values[0] = data.eventId;
72 values[0] = data.handlerId;
73 return internalHashFunction(values, std::size(values) *
sizeof(uint64_t));
77 namespace discord_core_internal {
82 template<
typename rty02,
typename... arg_types02>
friend class event;
97 function.swap(other.function);
108 *
this = std::move(other);
116 function = functionNew;
122 DCA_INLINE
event_delegate(std::function<return_type(arg_types...)> functionNew) {
131 function = functionNew;
145 std::function<return_type(
const arg_types&...)> function{};
149 template<
typename return_type,
typename... arg_types>
class event {
151 unordered_map<event_delegate_token, event_delegate<return_type, arg_types...>> functions{};
153 DCA_INLINE
event& operator=(
const event& other) =
delete;
154 DCA_INLINE
event(
const event& other) =
delete;
162 if (
this != &other) {
163 std::swap(functions, other.functions);
164 std::swap(eventId, other.eventId);
173 DCA_INLINE
event(event&& other)
noexcept {
175 *
this = std::move(other);
180 std::unique_lock lock{ accessMutex };
181 eventId = std::chrono::duration_cast<std::chrono::duration<uint64_t, std::micro>>(sys_clock::now().time_since_epoch()).count();
187 DCA_INLINE event_delegate_token
add(event_delegate<return_type, arg_types...>&& eventDelegate) {
188 std::unique_lock lock{ accessMutex };
189 event_delegate_token eventToken{};
190 eventToken.handlerId = std::chrono::duration_cast<std::chrono::duration<uint64_t, std::micro>>(sys_clock::now().time_since_epoch()).count();
191 eventToken.eventId = eventId;
192 functions[eventToken] = std::move(eventDelegate);
198 DCA_INLINE
void erase(
const event_delegate_token& eventToken) {
199 std::unique_lock lock{ accessMutex };
200 if (eventToken.
eventId == eventId) {
201 if (functions.contains(eventToken)) {
202 functions.erase(eventToken);
210 std::unique_lock lock{ accessMutex };
211 for (
auto& [key, value]: functions) {
213 value.function(args...).get();
214 }
catch (
const dca_exception& error) {
223 std::unique_lock lock{ accessMutex };
229 std::mutex accessMutex{};
236 template<
typename rty02,
typename... arg_types02>
friend class trigger_event;
238 DCA_INLINE trigger_event_delegate& operator=(
const trigger_event_delegate& other) =
delete;
245 DCA_INLINE trigger_event_delegate&
operator=(trigger_event_delegate&& other)
noexcept {
246 if (
this != &other) {
247 testFunction.swap(other.testFunction);
248 function.swap(other.function);
257 *
this = std::move(other);
264 DCA_INLINE trigger_event_delegate&
operator=(std::function<return_type(arg_types...)> functionNew) {
265 function = functionNew;
282 DCA_INLINE trigger_event_delegate&
operator=(return_type (*functionNew)(arg_types...)) {
283 function = functionNew;
299 testFunction = testFunctionNew;
305 testFunction = testFunctionNew;
309 std::function<return_type(
const arg_types&...)> function{};
310 std::function<bool(
const arg_types&...)> testFunction{};
316 unordered_map<event_delegate_token, trigger_event_delegate<return_type, arg_types...>> functions{};
318 DCA_INLINE trigger_event& operator=(
const trigger_event& other) =
delete;
319 DCA_INLINE
trigger_event(
const trigger_event& other) =
delete;
326 DCA_INLINE trigger_event&
operator=(trigger_event&& other)
noexcept {
327 if (
this != &other) {
328 std::swap(functions, other.functions);
329 std::swap(eventId, other.eventId);
340 *
this = std::move(other);
345 std::unique_lock lock{ accessMutex };
346 eventId = std::chrono::duration_cast<std::chrono::duration<uint64_t, std::micro>>(sys_clock::now().time_since_epoch()).count();
352 DCA_INLINE event_delegate_token
add(trigger_event_delegate<return_type, arg_types...>&& eventDelegate) {
353 std::unique_lock lock{ accessMutex };
354 event_delegate_token eventToken{};
355 eventToken.handlerId = std::chrono::duration_cast<std::chrono::duration<uint64_t, std::micro>>(sys_clock::now().time_since_epoch()).count();
356 eventToken.eventId = eventId;
357 functions[eventToken] = std::move(eventDelegate);
363 DCA_INLINE
void erase(
const event_delegate_token& eventToken) {
364 std::unique_lock lock{ accessMutex };
365 if (eventToken.
eventId == eventId) {
366 if (functions.contains(eventToken)) {
367 functions.erase(eventToken);
375 std::unique_lock lock{ accessMutex };
376 for (
auto iterator = functions.begin(); iterator != functions.end(); ++iterator) {
378 if (iterator.operator*().second.testFunction(args...)) {
379 iterator.operator*().second.function(args...);
380 iterator = functions.erase(iterator->first);
382 }
catch (
const dca_exception& error) {
391 std::unique_lock lock{ accessMutex };
397 std::mutex accessMutex{};
Event-delegate, representing an event function to be executed.
DCA_INLINE event_delegate & operator=(return_type(*functionNew)(arg_types...))
Assignment operator to set the delegate function using a function pointer. this operator assigns a ne...
DCA_INLINE event_delegate(return_type(*functionNew)(arg_types...))
Constructor for the event_delegate class, taking a pointer to a function as an argument....
DCA_INLINE event_delegate(std::function< return_type(arg_types...)> functionNew)
Constructor, taking a std::function<return_type(arg_types...)> as an argument.
DCA_INLINE event_delegate & operator=(std::function< return_type(arg_types...)> functionNew)
Assignment operator to set the delegate function. this operator assigns a new std::function,...
DCA_INLINE event_delegate & operator=(event_delegate &&other) noexcept
Move assignment operator for the event_delegate class. this operator moves the contents of another ev...
DCA_INLINE event_delegate(event_delegate &&other) noexcept
Move constructor for the event_delegate class. this constructor moves the contents of another event_d...
event_delegate()=default
Default constructor for event_delegate class.
Template class representing an event that executes event functions.
DCA_INLINE event & operator=(event &&other) noexcept
Move assignment operator for the event class. this operator moves the contents of another event insta...
DCA_INLINE void erase(const event_delegate_token &eventToken)
Remove an event delegate from the event.
DCA_INLINE void operator()(const arg_types &... args)
Invoke the event with provided arguments.
DCA_INLINE event_delegate_token add(event_delegate< return_type, arg_types... > &&eventDelegate)
Add an event delegate to the event.
DCA_INLINE event(event &&other) noexcept
Move constructor for the event class. this constructor moves the contents of another event instance,...
DCA_INLINE event()
Default constructor for event class.
Event-delegate, for representing an event-function to be executed conditionally.
DCA_INLINE trigger_event_delegate(trigger_event_delegate &&other) noexcept
Move constructor for trigger_event_delegate class. this constructor moves the contents of another tri...
DCA_INLINE trigger_event_delegate & operator=(trigger_event_delegate &&other) noexcept
Move assignment operator for trigger_event_delegate class. this operator moves the contents of anothe...
DCA_INLINE trigger_event_delegate(return_type(*functionNew)(arg_types...))
Constructor for trigger_event_delegate class, taking a function pointer as argument.
DCA_INLINE trigger_event_delegate & operator=(return_type(*functionNew)(arg_types...))
Assignment operator to set the delegate function using a function pointer. this operator assigns a ne...
DCA_INLINE trigger_event_delegate()=default
Default constructor for trigger_event_delegate class.
DCA_INLINE trigger_event_delegate & operator=(std::function< return_type(arg_types...)> functionNew)
Assignment operator to set the delegate function using a std::function. this operator assigns a new s...
DCA_INLINE void setTestFunction(std::function< bool(arg_types...)> testFunctionNew)
Sets the test function to determine conditional execution.
DCA_INLINE trigger_event_delegate(std::function< return_type(arg_types...)> functionNew)
Constructor for the trigger_event_delegate class, taking a std::function as an argument....
DCA_INLINE void setTestFunction(bool(*testFunctionNew)(arg_types...))
Sets the test function using a function pointer.
A trigger event that fires based on the result of trigger-function return value.
DCA_INLINE trigger_event & operator=(trigger_event &&other) noexcept
Move assignment operator for the trigger_event class. this operator moves the contents of another tri...
DCA_INLINE void erase(const event_delegate_token &eventToken)
Remove an event delegate from the event.
DCA_INLINE void operator()(const arg_types &... args)
Invoke the trigger event with provided arguments.
DCA_INLINE event_delegate_token add(trigger_event_delegate< return_type, arg_types... > &&eventDelegate)
Add an event delegate to the event.
DCA_INLINE trigger_event()
Default constructor for trigger_event class.
DCA_INLINE trigger_event(trigger_event &&other) noexcept
Move constructor for the trigger_event class. this constructor moves the contents of another trigger_...
static DCA_INLINE void printError(const string_type &what, std::source_location where=std::source_location::current())
Print an error message of the specified type.
DCA_INLINE bool operator==(const event_delegate_token &rhs) const
Equality operator for comparing event_delegate_token instances.
DCA_INLINE bool operator<(const event_delegate_token &rhs) const
Less-than operator for comparing event_delegate_token instances.
DCA_INLINE event_delegate_token()=default
Default constructor for event_delegate_token.
uint64_t handlerId
Identifier for the handler.
uint64_t eventId
Identifier for the event.
The main namespace for the forward-facing interfaces.
Struct representing an event delegate token, associated with an event.