12#ifndef MINESERVER_EVENT_HPP
13#define MINESERVER_EVENT_HPP
42#ifndef DOXYGEN_IGNORE_THIS
45 void error(
const char *format, ...);
58 std::string_view name, prefix, suffix;
60 name = __PRETTY_FUNCTION__;
61 prefix =
"auto type_name() [T = ";
63#elif defined(__GNUC__)
64 name = __PRETTY_FUNCTION__;
65 prefix =
"constexpr auto type_name() [with T = ";
67#elif defined(_MSC_VER)
69 prefix =
"auto __cdecl type_name<";
72 name.remove_prefix(prefix.size());
73 name.remove_suffix(suffix.size());
109 std::vector<subscription> subs;
111 const std::type_info *typeInfo;
133 static_assert(std::is_base_of_v<IEvent<T>, T>,
"Class doesn't derive from IEvent");
134 typeInfo = &
typeid(T);
147 for (
auto &sub : subs)
152 catch (
const std::exception &e)
185 s.callback = std::move(
func);
203 [
id](subscription
el) ->
bool
226 std::vector<EventHandler<void *> *> handlers;
233 { return *entry->getTypeInfo() == typeid(T); });
235 if (
loc == handlers.end())
268 auto loc = handlers.begin();
269 while (
loc != handlers.end())
272 loc = handlers.erase(
loc);
287 static_assert(std::is_base_of_v<IEvent<T>,
T>,
"Class doesn't derive from IEvent");
303 static_assert(std::is_base_of_v<IEvent<T>,
T>,
"Class doesn't derive from IEvent");
305 return handler->
subscribe(std::move(callback));
318 static_assert(std::is_base_of_v<IEvent<T>,
T>,
"Class doesn't derive from IEvent");
333 static_assert(std::is_base_of_v<IEvent<T>,
T>,
"Class doesn't derive from IEvent");
354 static_assert(std::is_base_of_v<IEvent, T>,
"Class doesn't derive from IEvent");
355 luabridge::getGlobalNamespace(state)
356 .beginNamespace(
"event")
358 if(!
ref.isFunction())
Handler for a type of event.
Definition event.h:85
EventHandler()
Construct a new Event Handler object.
Definition event.h:131
void fire(T &event)
Fire an event.
Definition event.h:145
std::function< void(T &) callbackType)
The type of function that should be passed.
Definition event.h:94
int subId
The Subscription Id.
Definition event.h:100
void unsubscribe(subId id)
Unsubscribe from an event.
Definition event.h:197
const std::type_info * getTypeInfo() const
Get the Type Info of T.
Definition event.h:216
subId subscribe(callbackType &&func)
Subscribe to an event.
Definition event.h:182
subId subscribe(const callbackType &func)
Subscribe to an event.
Definition event.h:166
Manager for Events.
Definition event.h:224
~EventsManager()
Destroy the Events Manager object.
Definition event.h:266
EventsManager()
Construct a new Events Manager object.
Definition event.cpp:15
static EventsManager * inst()
Gets the instance of the events manager.
Definition event.cpp:20
void unsubscribe(typename EventHandler< T >::subId subId)
Unsubscribe to T event.
Definition event.h:316
EventHandler< T >::subId subscribe(typename EventHandler< T >::callbackType &&callback)
Subscribe to T event.
Definition event.h:301
void fire(T &event)
Fire T event.
Definition event.h:331
EventHandler< T >::subId subscribe(const typename EventHandler< T >::callbackType &callback)
Subscribe to T event.
Definition event.h:285
Event interface.
Definition event.h:28
static void loadLua(lua_State *state)
Loads the event to the lua state.
Definition event.h:351
constexpr std::string_view type_name()
Gets the name of the type paramater.
Definition event.h:56
The logging namespace.
Definition logger.h:146
void error(const char *format,...)
Logs something at the ERROR level.
Definition logger.cpp:196