Part one - removed references and smart pointers

This commit is contained in:
2022-11-11 19:08:46 -08:00
parent 4c2fc4cfcf
commit 42645883cd
76 changed files with 3899 additions and 3707 deletions

View File

@ -1,119 +1,119 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
template<typename... A>
struct IEventListener {
/**
* Abstracted method for C++ template reasons. Invokes the listener.
*
* @param args Arguments to pass to the listener.
*/
virtual void invoke(A... args) = 0;
};
template <class T, typename... A>
struct EventListener : public IEventListener<A...> {
T *instance;
void (T::*callback)(A... args);
/**
* Construct a new event listener structure.
*
* @param instance Instance that the callback belongs to.
* @param callback Callback method that invokes back.
*/
EventListener(T *instance, void (T::*callback)(A... args)) :
instance(instance),
callback(callback)
{
}
void invoke(A... args) {
((*this->instance).*(this->callback))(args...);
}
};
template<typename...A>
class Event {
private:
std::vector<IEventListener<A...>*> listeners;
public:
/**
* Add a listener to this event.
*
* @tparam T The class that will receive the event.
* @param instance Instance of type T that will receive the callback.
* @param callback Callback method attached to T to receive the event.
* @return The initialized event listener. You don't really need this.
*/
template<class T>
EventListener<T, A...> * addListener(
T *instance,
void (T::*callback)(A... args)
) {
auto listener = new EventListener<T,A...>(instance, callback);
this->listeners.push_back(listener);
return listener;
}
/**
* Removes an event listener from this event.
*
* @tparam T The class that was once receiving the event.
* @param instance Instance of type T that did receive the callback.
* @param callback Callback method attached to T for the event.
*/
template<class T>
void removeListener(
T *instance,
void (T::*callback)(A... args)
) {
auto it = this->listeners.begin();
while(it != this->listeners.end()) {
auto listener = static_cast<EventListener<T,A...>*>(*it);
if(listener->instance != instance || listener->callback != callback) {
++it;
continue;
}
this->listeners.erase(it);
delete listener;
break;
}
}
/**
* Invokes the event and emits to all of the listeners.
*
* @param args Arguments for this event to pass to the listeners.
*/
void invoke(A... args) {
auto it = this->listeners.begin();
while(it != this->listeners.end()) {
(*it)->invoke(args...);
++it;
}
}
/**
* Disposes the event instance. Will also destroy all of the event
* listeners.
*/
~Event() {
auto it = this->listeners.begin();
while(it != this->listeners.end()) {
delete *it;
++it;
}
}
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
namespace Dawn {
template<typename... A>
struct IEventListener {
/**
* Abstracted method for C++ template reasons. Invokes the listener.
*
* @param args Arguments to pass to the listener.
*/
virtual void invoke(A... args) = 0;
};
template <class T, typename... A>
struct EventListener : public IEventListener<A...> {
T *instance;
void (T::*callback)(A... args);
/**
* Construct a new event listener structure.
*
* @param instance Instance that the callback belongs to.
* @param callback Callback method that invokes back.
*/
EventListener(T *instance, void (T::*callback)(A... args)) {
this->instance = instance;
this->callback = callback;
}
void invoke(A... args) {
((*this->instance).*(this->callback))(args...);
}
};
template<typename...A>
class Event {
private:
std::vector<IEventListener<A...>*> listeners;
public:
/**
* Add a listener to this event.
*
* @tparam T The class that will receive the event.
* @param instance Instance of type T that will receive the callback.
* @param callback Callback method attached to T to receive the event.
* @return The initialized event listener. You don't really need this.
*/
template<class T>
EventListener<T, A...> * addListener(
T *instance,
void (T::*callback)(A... args)
) {
auto listener = new EventListener<T,A...>(instance, callback);
this->listeners.push_back(listener);
return listener;
}
/**
* Removes an event listener from this event.
*
* @tparam T The class that was once receiving the event.
* @param instance Instance of type T that did receive the callback.
* @param callback Callback method attached to T for the event.
*/
template<class T>
void removeListener(
T *instance,
void (T::*callback)(A... args)
) {
auto it = this->listeners.begin();
while(it != this->listeners.end()) {
auto listener = static_cast<EventListener<T,A...>*>(*it);
if(listener->instance != instance || listener->callback != callback) {
++it;
continue;
}
this->listeners.erase(it);
delete listener;
break;
}
}
/**
* Invokes the event and emits to all of the listeners.
*
* @param args Arguments for this event to pass to the listeners.
*/
void invoke(A... args) {
auto it = this->listeners.begin();
while(it != this->listeners.end()) {
(*it)->invoke(args...);
++it;
}
}
/**
* Disposes the event instance. Will also destroy all of the event
* listeners.
*/
~Event() {
auto it = this->listeners.begin();
while(it != this->listeners.end()) {
delete *it;
++it;
}
}
};
}