Skip to content

Commit

Permalink
Merge pull request #24 from RaidcoreGG/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
DeltaGW2 authored Feb 22, 2024
2 parents 9cebfa8 + 717cf3b commit 77c5dc1
Show file tree
Hide file tree
Showing 26 changed files with 442 additions and 260 deletions.
2 changes: 2 additions & 0 deletions src/Consts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const char* EV_WINDOW_RESIZED = "EV_WINDOW_RESIZED";
const char* EV_MUMBLE_IDENTITY_UPDATED = "EV_MUMBLE_IDENTITY_UPDATED";
const char* EV_OPTIONS_CALLED = "EV_OPTIONS_CALLED";
const char* EV_EULA_ACCEPTED = "EV_EULA_ACCEPTED";
const char* EV_ADDON_LOADED = "EV_ADDON_LOADED";
const char* EV_ADDON_UNLOADED = "EV_ADDON_UNLOADED";

/* DataLink */
const char* DL_MUMBLE_LINK = "DL_MUMBLE_LINK";
Expand Down
2 changes: 2 additions & 0 deletions src/Consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ extern const char* EV_WINDOW_RESIZED;
extern const char* EV_MUMBLE_IDENTITY_UPDATED;
extern const char* EV_OPTIONS_CALLED;
extern const char* EV_EULA_ACCEPTED;
extern const char* EV_ADDON_LOADED;
extern const char* EV_ADDON_UNLOADED;

/* DataLink */
extern const char* DL_MUMBLE_LINK;
Expand Down
26 changes: 17 additions & 9 deletions src/Events/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <thread>
#include <algorithm>
#include <chrono>

#include "core.h"
#include "Consts.h"
#include "Shared.h"

Expand All @@ -13,48 +15,55 @@ namespace Events

void Raise(const char* aIdentifier, void* aEventData)
{
//auto start_time = std::chrono::high_resolution_clock::now();

std::string str = aIdentifier;

Events::Mutex.lock();
const std::lock_guard<std::mutex> lock(Mutex);
{
for (EVENT_CONSUME callback : Registry[str])
{
std::thread([callback, aEventData]() { callback(aEventData); }).detach();
callback(aEventData);
}
}
Events::Mutex.unlock();

//auto end_time = std::chrono::high_resolution_clock::now();
//auto time = end_time - start_time;
//LogDebug(CH_EVENTS, u8"Executed event (%s) in %uµs.", aIdentifier, time / std::chrono::microseconds(1));
}
void RaiseNotification(const char* aIdentifier)
{
Raise(aIdentifier, nullptr);
}

void Subscribe(const char* aIdentifier, EVENT_CONSUME aConsumeEventCallback)
{
std::string str = aIdentifier;

Events::Mutex.lock();
const std::lock_guard<std::mutex> lock(Mutex);
{
Registry[str].push_back(aConsumeEventCallback);
}
Events::Mutex.unlock();
}

void Unsubscribe(const char* aIdentifier, EVENT_CONSUME aConsumeEventCallback)
{
std::string str = aIdentifier;

Events::Mutex.lock();
const std::lock_guard<std::mutex> lock(Mutex);
{
if (Registry.find(str) != Registry.end())
{
Registry[str].erase(std::remove(Registry[str].begin(), Registry[str].end(), aConsumeEventCallback), Registry[str].end());
}
}
Events::Mutex.unlock();
}

int Verify(void* aStartAddress, void* aEndAddress)
{
int refCounter = 0;

Events::Mutex.lock();
const std::lock_guard<std::mutex> lock(Mutex);
{
for (auto& [identifier, consumers] : Registry)
{
Expand All @@ -68,7 +77,6 @@ namespace Events
}
}
}
Events::Mutex.unlock();

return refCounter;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Events/EventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Events

/* Raises an event of provided name, passing a pointer to an eventArgs struct. */
void Raise(const char* aIdentifier, void* aEventData);
/* Raises an event without payload, a notification. */
void RaiseNotification(const char* aIdentifier);
/* Subscribes the provided ConsumeEventCallback function, to the provided event name. */
void Subscribe(const char* aIdentifier, EVENT_CONSUME aConsumeEventCallback);
/* Unsubscribes the provided ConsumeEventCallback function from the provided event name. */
Expand Down
1 change: 1 addition & 0 deletions src/Events/FuncDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

typedef void (*EVENT_CONSUME)(void* aEventArgs);
typedef void (*EVENTS_RAISE)(const char* aIdentifier, void* aEventData);
typedef void (*EVENTS_RAISENOTIFICATION)(const char* aIdentifier);
typedef void (*EVENTS_SUBSCRIBE)(const char* aIdentifier, EVENT_CONSUME aConsumeEventCallback);

#endif
Loading

0 comments on commit 77c5dc1

Please sign in to comment.