inital commit
This commit is contained in:
commit
af547a319c
11 changed files with 205 additions and 0 deletions
165
Crafter.Event.cppm
Normal file
165
Crafter.Event.cppm
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
module;
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
export module Crafter.Event;
|
||||
|
||||
namespace Crafter {
|
||||
export template <class T>
|
||||
class Event;
|
||||
|
||||
export template<>
|
||||
class Event<void>;
|
||||
|
||||
export template<typename T>
|
||||
class EventListener {
|
||||
public:
|
||||
int priority;
|
||||
EventListener();
|
||||
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();
|
||||
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;
|
||||
};
|
||||
|
||||
export template<typename T>
|
||||
class Event {
|
||||
private:
|
||||
std::map<int, std::vector<EventListener<T>*>> listeners;
|
||||
public:
|
||||
~Event() {
|
||||
for (const auto& listenerSlice : listeners) {
|
||||
const std::vector<EventListener<T>*> slice = listenerSlice.second;
|
||||
for (const auto& listener : slice) {
|
||||
listener->eventToListenTo = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
listeners[listener->priority].erase(std::remove(listeners[listener->priority].begin(), listeners[listener->priority].end(), listener), listeners[listener->priority].end());
|
||||
}
|
||||
|
||||
void Invoke(T data) {
|
||||
for (const auto& listenerSlice : listeners) {
|
||||
const std::vector<EventListener<T>*> slice = listenerSlice.second;
|
||||
for (const auto& listener : slice) {
|
||||
listener->function(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export template<>
|
||||
class Event<void> {
|
||||
private:
|
||||
std::map<int, std::vector<EventListener<void>*>> listeners;
|
||||
|
||||
public:
|
||||
~Event() {
|
||||
for (const auto& listenerSlice : listeners) {
|
||||
const std::vector<EventListener<void>*> slice = listenerSlice.second;
|
||||
for (const auto& listener : slice) {
|
||||
listener->eventToListenTo = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AddListener(EventListener<void>* listener, int priority = 0) {
|
||||
if (listeners.contains(priority)) {
|
||||
listeners[priority].push_back(listener);
|
||||
}
|
||||
else {
|
||||
listeners[priority] = std::vector<EventListener<void>*>{ listener };
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveListener(EventListener<void>* listener) {
|
||||
listeners[listener->priority].erase(std::remove(listeners[listener->priority].begin(), listeners[listener->priority].end(), listener), listeners[listener->priority].end());
|
||||
}
|
||||
|
||||
void Invoke() {
|
||||
for (const auto& listenerSlice : listeners) {
|
||||
const std::vector<EventListener<void>*> slice = listenerSlice.second;
|
||||
for (const auto& listener : slice) {
|
||||
listener->function();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
BIN
bin/Crafter.Event.pcm
Normal file
BIN
bin/Crafter.Event.pcm
Normal file
Binary file not shown.
BIN
bin/Crafter.Thread-ThreadPool.pcm
Normal file
BIN
bin/Crafter.Thread-ThreadPool.pcm
Normal file
Binary file not shown.
BIN
bin/Crafter.Thread.pcm
Normal file
BIN
bin/Crafter.Thread.pcm
Normal file
Binary file not shown.
BIN
bin/libcrafter-event.a
Normal file
BIN
bin/libcrafter-event.a
Normal file
Binary file not shown.
BIN
bin/libcrafter-thread.a
Normal file
BIN
bin/libcrafter-thread.a
Normal file
Binary file not shown.
BIN
build/Crafter.Event.o
Normal file
BIN
build/Crafter.Event.o
Normal file
Binary file not shown.
1
build/Crafter.Thread
Submodule
1
build/Crafter.Thread
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 79b152bd67194ed11b56eb6cd3f5019f862296dd
|
||||
BIN
build/debug/Crafter.Event.o
Normal file
BIN
build/debug/Crafter.Event.o
Normal file
Binary file not shown.
1
build/debug/Crafter.Thread
Submodule
1
build/debug/Crafter.Thread
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 79b152bd67194ed11b56eb6cd3f5019f862296dd
|
||||
38
project.json
Normal file
38
project.json
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
{
|
||||
"name": "crafter-event",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "base",
|
||||
"standard": "c++26",
|
||||
"source_files": [],
|
||||
"module_files": ["Crafter.Event"],
|
||||
"additional_files": [],
|
||||
"build_dir": "./build",
|
||||
"output_dir": "./bin",
|
||||
"type":"library"
|
||||
},
|
||||
{
|
||||
"name": "debug",
|
||||
"extends": ["base"],
|
||||
"optimization_level": "0",
|
||||
"debug":true,
|
||||
"dependencies": [
|
||||
{
|
||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Thread.git",
|
||||
"configuration":"debug"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "release",
|
||||
"extends": ["base"],
|
||||
"optimization_level": "3",
|
||||
"dependencies": [
|
||||
{
|
||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Thread.git",
|
||||
"configuration":"release"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue