Add event

This commit is contained in:
2025-06-06 23:13:31 -05:00
parent c8aef96367
commit 8dc8b9ea86
4 changed files with 90 additions and 7 deletions

View File

@ -7,7 +7,7 @@
#include <filesystem>
#include <map>
#include <vector>
#include <functional>
class TextException : public std::exception {
@ -29,6 +29,60 @@
};
namespace Tesses::Framework
{
template<typename...TArgs>
class Event {
public:
virtual void Invoke(TArgs... args)=0;
virtual ~Event()
{}
};
template<typename...TArgs>
class FunctionalEvent : public Event<TArgs...> {
std::function<void(TArgs...)> cb;
public:
FunctionalEvent(std::function<void(TArgs...)> cb)
{
this->cb = cb;
}
void Invoke(TArgs... args)
{
this->cb(args...);
}
};
template<typename...TArgs>
class EventList : public Event<TArgs...> {
std::vector<std::shared_ptr<Event<TArgs...>>> items;
public:
void operator+=(std::shared_ptr<Event<TArgs...>> event)
{
for(std::shared_ptr<Event<TArgs...>>& item : this->items)
{
if(item.get() == event.get()) return;
}
this->items.push_back(event);
}
void operator-=(std::shared_ptr<Event<TArgs...>> event)
{
for(auto i = this->items.begin(); i != this->items.end(); i++)
{
if(i->get() == event.get())
{
this->items.erase(i);
return;
}
}
}
void Invoke(TArgs... args)
{
for(auto& item : this->items)
{
item->Invoke(args...);
}
}
};
extern EventList<uint64_t> OnItteraton;
void TF_Init();
void TF_InitWithConsole();
void TF_ConnectToSelf(uint16_t port);