Crafter.Event/interfaces/Crafter.Event.cppm

266 lines
9.8 KiB
Text
Raw Normal View History

2025-05-07 19:05:26 +02:00
/*
2025-05-25 16:41:07 +02:00
Crafter®.Event
2025-05-07 19:05:26 +02:00
Copyright (C) 2025 Catcrafts®
2025-11-25 02:07:05 +01:00
catcrafts.net
2025-05-07 19:05:26 +02:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
2025-11-25 02:07:05 +01:00
License version 3.0 as published by the Free Software Foundation;
2025-05-07 19:05:26 +02:00
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2025-05-06 12:38:46 +02:00
export module Crafter.Event;
2025-11-16 15:05:48 +01:00
import std;
2025-05-06 12:38:46 +02:00
namespace Crafter {
export template <class T>
class Event;
export template<>
class Event<void>;
export template<typename T>
class EventListener {
public:
int priority;
EventListener();
2025-11-25 01:53:40 +01:00
EventListener(EventListener&) = delete;
EventListener& operator=(EventListener&) = delete;
2025-05-06 12:38:46 +02:00
void SetEvent(Event<T>* eventToListenTo, std::function<void(T)> function, int priority = 0);
EventListener(Event<T>* eventToListenTo, std::function<void(T)> function, int priority = 0);
~EventListener();
Event<T>* eventToListenTo;
std::function<void(T)> function;
};
export template<>
class EventListener<void> {
public:
int priority;
EventListener();
2025-11-25 01:53:40 +01:00
EventListener(EventListener&) = delete;
EventListener& operator=(EventListener&) = delete;
2025-05-06 12:38:46 +02:00
void SetEvent(Event<void>* eventToListenTo, std::function<void()> function, int priority = 0);
EventListener(Event<void>* eventToListenTo, std::function<void()> function, int priority = 0);
~EventListener();
Event<void>* eventToListenTo;
std::function<void()> function;
};
2025-11-25 20:47:57 +01:00
// Base implementation for Event<T> - common functionality
template<typename T>
class EventBase {
2025-11-25 21:20:10 +01:00
public:
#ifdef CRAFTER_TIMING
std::map<const EventListener<T>*, std::chrono::nanoseconds> listenerTimes;
#endif
2025-11-25 20:47:57 +01:00
protected:
2025-05-06 12:38:46 +02:00
std::map<int, std::vector<EventListener<T>*>> listeners;
2025-11-25 20:47:57 +01:00
public:
EventBase() = default;
EventBase(EventBase&& other) : listeners(std::move(other.listeners)) {
2025-11-25 01:53:40 +01:00
for (const auto& listenerSlice : listeners) {
for (const auto& listener : listenerSlice.second) {
2025-11-26 01:38:30 +01:00
if(listener) {
listener->eventToListenTo = this;
}
2025-11-25 01:53:40 +01:00
}
}
}
2025-11-25 20:47:57 +01:00
EventBase(EventBase&) = delete;
EventBase& operator=(EventBase&) = delete;
virtual ~EventBase() {
2025-05-06 12:38:46 +02:00
for (const auto& listenerSlice : listeners) {
2025-11-25 01:53:40 +01:00
for (const auto& listener : listenerSlice.second) {
2025-11-26 01:38:30 +01:00
if(listener) {
listener->eventToListenTo = nullptr;
}
2025-05-06 12:38:46 +02:00
}
}
}
void AddListener(EventListener<T>* listener, int priority = 0) {
if (listeners.contains(priority)) {
listeners[priority].push_back(listener);
}
else {
listeners[priority] = std::vector<EventListener<T>*>{ listener };
}
}
void RemoveListener(EventListener<T>* listener) {
2025-11-26 01:38:30 +01:00
auto pos = listeners.find(listener->priority);
std::replace(pos->second.begin(), pos->second.end(), listener, static_cast<EventListener<T>*>(nullptr));
2025-05-06 12:38:46 +02:00
}
2025-11-25 21:17:21 +01:00
#ifdef CRAFTER_TIMING
// Log timing data in a clear manner
void LogTiming() const {
if (this->listenerTimes.empty()) {
return; // No timing data to report
}
// Create a vector of pairs for sorting
std::vector<std::pair<const EventListener<T>*, std::chrono::nanoseconds>> sortedTimes(
this->listenerTimes.begin(),
this->listenerTimes.end()
);
// Sort by duration (descending order)
std::sort(sortedTimes.begin(), sortedTimes.end(),
[](const auto& a, const auto& b) {
return a.second > b.second;
});
// Print results
for (const auto& pair : sortedTimes) {
const auto& listener = pair.first;
const auto& duration = pair.second;
// Format output based on magnitude
if (duration.count() >= 1000000) {
// Milliseconds
std::printf("Listener (priority %d): %.3f ms\n",
listener->priority,
static_cast<double>(duration.count()) / 1000000.0);
} else if (duration.count() >= 1000) {
// Microseconds
std::printf("Listener (priority %d): %.3f μs\n",
listener->priority,
static_cast<double>(duration.count()) / 1000.0);
} else {
// Nanoseconds
std::printf("Listener (priority %d): %ld ns\n",
listener->priority,
duration.count());
}
}
}
#endif
2025-11-25 20:47:57 +01:00
};
2025-05-06 12:38:46 +02:00
2025-11-25 20:47:57 +01:00
// Specialized Event<void> with different Invoke signature
export template<>
class Event<void> : public EventBase<void> {
public:
using EventBase<void>::EventBase;
void Invoke() {
2025-11-25 21:17:21 +01:00
#ifdef CRAFTER_TIMING
// Clear previous timing data
this->listenerTimes.clear();
2025-11-26 01:38:30 +01:00
this->listeners.erase(std::remove(this->listeners.begin(), this->listeners.end(), nullptr), this->listeners.end());
for (const auto& listenerSlice : this->listeners) {
2025-11-25 20:47:57 +01:00
for (const auto& listener : listenerSlice.second) {
2025-11-25 21:17:21 +01:00
auto start = std::chrono::high_resolution_clock::now();
2025-11-25 20:47:57 +01:00
listener->function();
2025-11-25 21:17:21 +01:00
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
this->listenerTimes[listener] = duration;
2025-11-25 20:47:57 +01:00
}
}
2025-11-25 21:17:21 +01:00
#else
2025-11-26 01:23:40 +01:00
auto listenersCopy = this->listeners;
2025-11-26 01:22:56 +01:00
for (const auto& listenerSlice : listenersCopy) {
2025-11-25 21:17:21 +01:00
for (const auto& listener : listenerSlice.second) {
listener->function();
}
}
#endif
2025-11-25 20:47:57 +01:00
}
};
// Generic Event<T> with Invoke signature
export template<typename T>
class Event : public EventBase<T> {
public:
using EventBase<T>::EventBase;
2025-05-06 12:38:46 +02:00
void Invoke(T data) {
2025-11-25 21:17:21 +01:00
#ifdef CRAFTER_TIMING
// Clear previous timing data
this->listenerTimes.clear();
2025-11-26 01:38:30 +01:00
this->listeners.erase(std::remove(this->listeners.begin(), this->listeners.end(), nullptr), this->listeners.end());
for (const auto& listenerSlice : this->listeners) {
2025-11-25 21:17:21 +01:00
for (const auto& listener : listenerSlice.second) {
auto start = std::chrono::high_resolution_clock::now();
listener->function(data);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
this->listenerTimes[listener] = duration;
}
}
#else
2025-11-26 01:44:18 +01:00
this->listeners.erase(std::remove(this->listeners.begin(), this->listeners.end(), nullptr), this->listeners.end());
2025-11-26 01:22:56 +01:00
for (const auto& listenerSlice : listenersCopy) {
2025-11-25 01:53:40 +01:00
for (const auto& listener : listenerSlice.second) {
2025-05-06 12:38:46 +02:00
listener->function(data);
}
}
2025-11-25 21:17:21 +01:00
#endif
2025-05-06 12:38:46 +02:00
}
};
2025-11-25 20:47:57 +01:00
// Common implementations for EventListener<T>
2025-05-06 12:38:46 +02:00
template<typename T> void EventListener<T>::SetEvent(Event<T>* eventToListenTo, std::function<void(T)> listener, int priority)
{
this->priority = priority;
if (this->eventToListenTo != nullptr) {
eventToListenTo->RemoveListener(this);
}
this->eventToListenTo = eventToListenTo;
eventToListenTo->AddListener(this);
this->function = listener;
}
template<typename T> EventListener<T>::EventListener(Event<T>* eventToListenTo, std::function<void(T)> listener, int priority) : priority(priority), eventToListenTo(eventToListenTo), function(listener) {
eventToListenTo->AddListener(this);
}
template<typename T> EventListener<T>::~EventListener() {
if (eventToListenTo != nullptr) {
eventToListenTo->RemoveListener(this);
}
}
template<typename T> EventListener<T>::EventListener() {
eventToListenTo = nullptr;
}
2025-11-25 20:47:57 +01:00
// Specific implementations for EventListener<void>
2025-05-06 12:38:46 +02:00
void EventListener<void>::SetEvent(Event<void>* eventToListenTo, std::function<void()> listener, int priority)
{
this->priority = priority;
if (this->eventToListenTo != nullptr) {
eventToListenTo->RemoveListener(this);
}
this->eventToListenTo = eventToListenTo;
eventToListenTo->AddListener(this);
this->function = listener;
}
EventListener<void>::EventListener(Event<void>* eventToListenTo, std::function<void()> listener, int priority) : priority(priority), eventToListenTo(eventToListenTo), function(listener) {
eventToListenTo->AddListener(this);
}
EventListener<void>::~EventListener() {
if (eventToListenTo != nullptr) {
eventToListenTo->RemoveListener(this);
}
}
EventListener<void>::EventListener() {
eventToListenTo = nullptr;
}
}