C++ Generic Event System - c++

I am trying to build a generic event system. The Delegates and Events should not know anything about the other and a Manager will handle everything.
With this in mind I created a templated delegate/listener is made up of a function pointer and templated parameters.
class IDelegate
{
public:
IDelegate() {};
virtual ~IDelegate() = 0;
virtual void exec() = 0;
};
template<class Class, typename... Args>
class Delegate : public IDelegate
{
public:
typedef (Class::*Function)(Args);
Delegate(Class* inst, Function func) : instance(inst), function(func) {};
~Delegate() { instance = nullptr };
void exec(Args args)
{
instance->function(args);
}
private:
Class* instance;
Function function;
};
I did something similar on the Event side. The Events being made up of an ID and the Arguments that will need to be past to the function pointer.
class IEvent
{
public:
IEvent() {};
virtual ~IEvent() = 0;
};
template<typename... Args>
class Event : IEvent
{
public:
Event(ID eventID, Args args) : id(eventID), arguments(args) {};
~Event() = default;
ID id;
Args arguments;
};
I choose to use templates in the hope of not needing to manually create every event/delegate class that may be needed.
Lastly I wanted to make a Manager that would be a singleton.
//EventHandler.h
#pragma once
#include <string>
#include <unordered_map>
#include <queue>
#include "Event.h"
typedef std::string ID;
typedef std::unordered_multimap<ID, void*> Listeners;
class EventHandler
{
public:
EventHandler(const EventHandler& copy) = delete;
~EventHandler();
EventHandler& operator= (const EventHandler& rhs) = delete;
void Initialize();
template<class Class, class TEvent>
void Run();
void Shutdown();
static Listeners::iterator& Register(ID id, IDelegate* listener);
static void Deregister(Listeners::iterator& iterator);
static void Post(IEvent* evnt);
private:
static Listeners listeners;
static std::queue<IEvent*> events;
static EventHandler* instance;
EventHandler() {};
EventHandler(const EventHandler& copy);
EventHandler& operator= (const EventHandler& rhs);
};
//EventHandler.cpp
#include "EventHandler.h"
EventHandler::~EventHandler()
{
instance = nullptr;
}
void EventHandler::Initialize()
{
instance = this;
}
void EventHandler::Run()
{
//TODO: Determine the Event and cast or instantiate to the right class
IEvent* evnt = events.front; //This should not be IEvent*, but Event<>*
events.pop();
listeners[evnt->id].exec(evnt->arguments); //The delegate may need to be casted too.
}
void EventHandler::Shutdown()
{
instance = nullptr;
}
Listeners::iterator& EventHandler::Register(ID id, IDelegate* listener)
{
Listeners::iterator iter = listeners.emplace(id, listener);
return iter;
}
void EventHandler::Deregister(Listeners::iterator& iterator)
{
listeners.erase(iterator);
}
void EventHandler::Post(IEvent* evnt)
{
events.emplace(evnt);
}
Where I'm running into trouble is figuring out what Event I'm actually using in Run(). If possible I would like to do this without a switch or something similar as that would defeat the use of the templated classes. I have considered making the function pointer all be the same signature as this would simplify some of the code, but would make the system less flexible.
Thank you for any help.

Related

Observer Pattern (C++): Cast Templated Class from Subclass to Baseclass of Tempalate

I am working on developing an observer pattern infrastructure and was hoping to get some advice on a design issue I have run into. The idea behind this pattern is to allow users to create "Transactions" from any primitive type they would like. Users can then develop their own custom Observers to do things like logging / build statistics / etc... Everything is then hooked up at runtime using factory methods depending on what they would like based on a configuration. This all works really well, until a user would like to provide a baseclass pointer based observer and have it work for all derived types. When observers are added I check if the types match and throw an exception if it is the wrong type.
Below is a simple example of the pattern and the use case that is breaking it.
Thanks so much in advance!
#include <cassert>
#include <stdio.h>
#include <vector>
template<typename T>
class Observerable;
template<typename T>
struct ObserverInfo {
const T& data;
const Observerable<T>& observer;
};
class IObserver {
public:
virtual ~IObserver() {}
};
template<typename T>
class Observer : public IObserver {
public:
virtual void Annouce(const ObserverInfo<T>& info);
};
class DumperBase {
public:
virtual void Dump() const = 0;
};
class MyTransaction : public DumperBase {
public:
void Dump() const override { printf("Hello\n"); }
};
class IObserverable {
public:
virtual ~IObserverable() {}
virtual void AttachObserver(IObserver* observer) = 0;
};
template<typename T>
class Observerable : public IObserverable {
public:
void AttachObserver(IObserver* observer) override {
// ?? Is there a way I can make this work for base clases?
auto casted_observer = dynamic_cast<Observer<T>*>(observer);
assert(casted_observer);
m_observers.emplace_back(casted_observer);
}
void Push(T transaction) {
Annouce({transaction, *this});
// Do something real with T
}
void Annouce(const ObserverInfo<T>& info) {
for (auto& observer : m_observers) {
observer->Annouce(info);
}
}
private:
std::vector<Observer<T>*> m_observers;
};
class MyTransactionObserver : public Observer<DumperBase*> {
void Annouce(const ObserverInfo<DumperBase*>& info) override {
info.data->Dump();
}
};
int main() {
Observerable<MyTransaction*> observerable{};
Observer<DumperBase*> observer{};
observerable.AttachObserver(&observer);
MyTransaction transaction{};
observerable.Push(&transaction);
return 0;
}

Pass base class to function delegate that recives the super class

I have this map:
map<IEvent, EventHandler, IEventCompare>
Where EventHandler is defined as typedef void (*EventHandler)(IEvent);
IEvent is a class that describes a general event.
Now I want to add to this map a function that receives CreationEvent, a class that inherits IEvent. The function is defined so:
void onCreate(CreationEvent);
But when I try to add it to the map, I get a compilation error
E0167 argument of type "void (Engine::IObject::*)(Engine::CreationEvent)" is incompatible with parameter of type "Engine::EventHandler"
And if I try to explicitly convert it to EventHandler:
E0171 invalid type conversion
I can declare onCreate with IEvent, but I would like to avoid it since it will require me to assume the type of event, and it is not well defined.
Is there a way to do what I try?
IEvent:
/**
* Represents an Event, such as collision between 2 objects or click on an object.
*/
class IEvent
{
public:
IEvent(string name) { this->name = name; };
/**
* Copy constructor.
*/
IEvent(const IEvent& other) { this->name = other.name;};
string getName() const { return this->name; };
protected:
string name;
};
CreationEvent:
class CreationEvent : public IEvent
{
public:
CreationEvent();
std::chrono::time_point<std::chrono::system_clock> getCreateTime() const;
private:
std::chrono::time_point<std::chrono::system_clock> creationTime; /**< The creation time of this event.*/
};
Notes:
Everything is inside namespace Engine, and the map is declared inside IObject.
If I get your idea right, you want:
Have typed events with base event class.
Have handlers with base handler class.
Handlers can receive event of certain type.
Consider the next example. For the simplicity I used std::vector instead of std::map, and put it inside event class.
This code contains ugliness, leaks and must not be used in a "production" without modifications.
#include <iostream>
#include <vector>
//***********************************************************//
struct event;
struct handler
{
};
struct event_handler
{
event_handler(handler* receiver) : receiver_{ receiver } {}
handler* receiver_;
virtual void invoke(event& evt) = 0;
};
template <typename T, typename U>
struct event_handler_impl : event_handler
{
typedef void (T::* handler_function)(U&);
event_handler_impl(handler* receiver, handler_function function) :
event_handler{ receiver_ },
function_{ function } {}
void invoke(event& evt) {
T* typed_receiver = static_cast<T*>(receiver_);
U& typed_event = static_cast<U&>(evt);
(typed_receiver->*function_)(typed_event);
}
handler_function function_;
};
struct event
{
void subscribe(event_handler* hdlr)
{
//TODO: Check. Is double added?
handlers_.push_back(hdlr);
}
void sent()
{
for (auto& item : handlers_)
{
item->invoke(*this);
}
}
std::vector<event_handler*> handlers_;
};
//*****************************EXAMPLE***********************//
struct creation_event : public event
{
int creation_id{};
};
struct bar_handler : public handler
{
void handle_creation(creation_event& evt)
{
std::cout << "bar" << evt.creation_id << std::endl;
}
};
struct foo_handler : public handler
{
void handle_creation(creation_event& evt)
{
std::cout << "foo" << evt.creation_id << std::endl;
}
};
template<typename T, typename U>
void subscribe_to_event(U& evt, T* reciver, void (T::* handler_function)(U&))
{
evt.subscribe(new event_handler_impl<T, U>(reciver, handler_function));
}
int main()
{
creation_event evt;
bar_handler bar;
foo_handler foo;
subscribe_to_event(evt, &foo, &foo_handler::handle_creation);
subscribe_to_event(evt, &bar, &bar_handler::handle_creation);
evt.sent();
evt.creation_id = 1;
evt.sent();
return 0;
}
The only tricky part is:
template <typename T, typename U>
struct event_handler_impl : event_handler
Here we generating classes for storing our typed “callback” and using polymorphism to store those classes inside our std::vector since they are all child classes for handler.
As a suggestion - consider using smart pointers instead of raw pointers. Also you can put function void subscribe_to_even(…) to the handler base class, so you can remove second parameter and just pass "this" to the event_handler_impl - new event_handler_impl<T, U>(this, handler_function)

Function pointer from QMap

I'm trying to implement factory method pattern in my QT project following this example: https://www.codeproject.com/Articles/363338/Factory-Pattern-in-Cplusplus
#include <QCoreApplication>
#include <QDebug>
class IAnimal
{
public:
virtual int GetNumberOfLegs() const = 0;
virtual void Speak() = 0;
virtual void Free() = 0;
};
typedef IAnimal* (__stdcall *CreateAnimalFn)(void);
// IAnimal implementations
class Cat : public IAnimal
{
public:
int GetNumberOfLegs() const { return 4; }
void Speak() { qDebug() << "Meow" << endl; }
void Free() { delete this; }
static IAnimal * __stdcall Create() { return new Cat(); }
};
class Dog : public IAnimal
{
public:
int GetNumberOfLegs() const { return 4; }
void Speak() { qDebug() << "Woof" << endl; }
void Free() { delete this; }
static IAnimal * __stdcall Create() { return new Dog(); }
};
Factory class:
// Factory for creating instances of IAnimal
class AnimalFactory
{
private:
AnimalFactory();
AnimalFactory(const AnimalFactory &) { }
AnimalFactory &operator=(const AnimalFactory &) { return *this; }
typedef QMap<QString,CreateAnimalFn> FactoryMap;
FactoryMap m_FactoryMap;
public:
~AnimalFactory() { m_FactoryMap.clear(); }
static AnimalFactory *Get()
{
static AnimalFactory instance;
return &instance;
}
void Register(const QString &animalName, CreateAnimalFn pfnCreate);
IAnimal *CreateAnimal(const QString &animalName);
};
AnimalFactory::AnimalFactory()
{
Register("Cat", &Cat::Create);
Register("Dog", &Dog::Create);
}
void AnimalFactory::Register(const QString &animalName, CreateAnimalFn pfnCreate)
{
m_FactoryMap[animalName] = pfnCreate;
}
IAnimal *AnimalFactory::CreateAnimal(const QString &animalName)
{
FactoryMap::iterator it = m_FactoryMap.find(animalName);
if( it != m_FactoryMap.end() )
return it.value();
return NULL;
}
However I am encountering such an error:
cannot convert 'IAnimal* (__attribute__((__stdcall__)) *)()' to 'IAnimal*' in return
return it.value();
Only existing anwser (Insert function pointer into QMap (Qt)) suggests making Create() functions static which doesn't seem to help.
I will be very grateful for any piece of advice.
That's a little bit complicated. You're writing C++, so you shouldn't be copying Java. C++ is way more expressive here.
You won't need the explicit Create/Free methods - the compiler can generate them for you, automatically.
You definitely need a virtual destructor, otherwise the interface will be useless. Any class you intend to derive from must have a virtual destructor, with very few specialized exceptions from this rule.
All implementations of virtual methods should be declared override, including the destructor, but not virtual as that'd violate DRY.
The classes can carry their names, so that the factory can register them just by knowing their types. This is an optional behavior of the factory.
#include <QtCore>
class IAnimal {
public:
virtual int GetNumberOfLegs() const = 0;
virtual QString Speaks() = 0;
virtual ~IAnimal() {}
};
class Cat : public IAnimal {
public:
int GetNumberOfLegs() const override { return 4; }
QString Speaks() override { return QStringLiteral("Meow"); }
static auto className() { return "Cat"; }
};
class Dog : public IAnimal {
public:
int GetNumberOfLegs() const override { return 4; }
QString Speaks() override { return QStringLiteral("Woof"); }
static auto className() { return "Dog"; }
};
Now we can have a generic factory. Note that all sane C++ container types manage their data. You don't need to explicitly clear them on destruction. We're leveraging C++11. The Register method will only accept types that derive from Interface, and that method automatically generates a construction function using the lambda expression.
The lifetime of the instance should be controlled explicitly by instantiating it in main().
#include <type_traits>
#include <typeindex>
#include <map>
template <class Interface> class Factory {
template <class C, class T = void> struct enable_if_I :
std::enable_if<std::is_base_of<Interface, C>::value, T> {};
using create_fn = Interface* (*)();
std::map<QByteArray, create_fn, std::less<>> m_creators;
std::map<std::type_index, QByteArray> m_names;
static Factory *&instance_ref() { // assume no inline static yet
static Factory *m_instance;
return m_instance;
}
Factory(const Factory &) = delete;
Factory &operator=(const Factory &) = delete;
public:
Factory() {
Q_ASSERT(!instance());
instance_ref() = this;
}
virtual ~Factory() { instance_ref() = {}; }
In general, registration requires the type and name of the derived class. This presupposes nothing about whether the class has a className() member. The factory stores both the factory function and the name. This allows name look up without having className as a virtual method of the interface.
template <class T> typename enable_if_I<T>::type Register(const QByteArray &name) {
m_creators[name] = +[]()->Interface* { return new T(); };
m_names[{typeid(T)}] = name;
}
When class names are known, we can leverage them to register one or more classes, given just their types.
template <class T1> typename enable_if_I<T1>::type Register() {
this->Register<T1>(T1::className());
}
template <class T1, class T2, class...T> typename enable_if_I<T1>::type Register() {
this->Register<T1>(T1::className());
this->Register<T2, T...>();
}
The instance creation methods are optimized not to copy the name given, no matter the format. This is why we use the std::map<K, V, std::less<>> map with a transparent comparator. QByteArray provides operator< that takes various types on the right-hand side, and to exploit this, the type of the key (here: name) must reach the comparator.
template <typename T> static Interface *CreateA(T &&t) {
return instance() ? instance()->Create(std::forward<T>(t)) : nullptr;
}
Interface *Create(QLatin1String name) const { return Create(name.data()); }
template <typename T> Interface *Create(T &&name) const;
static const QByteArray &NameOfA(const Interface * obj);
const QByteArray &NameOf(const Interface *) const;
static Factory *instance() { return instance_ref(); }
};
template <class Interface>
template <typename T> Interface *Factory<Interface>::Create(T &&name) const {
auto it = m_creators.find(name);
return (it != m_creators.end()) ? it->second() : nullptr;
}
namespace detail {
const QByteArray & null() { static const QByteArray n; return n; }
}
template <class Interface>
const QByteArray &Factory<Interface>::NameOfA(const Interface *obj) {
return instance() ? instance()->NameOf(obj) : detail::null();
}
template <class Interface>
const QByteArray &Factory<Interface>::NameOf(const Interface *obj) const {
auto it = m_names.find(typeid(*obj));
return (it != m_names.end()) ? it->second : detail::null();
}
The generic factory takes the interface and concrete types and registers them all in the constructor. This makes building the factories simple.
template <class Interface, class ...Types>
class GenericFactory : public Factory<Interface> {
public:
GenericFactory() {
this->template Register<Types...>();
}
};
using AnimalFactory = GenericFactory<IAnimal, Cat, Dog>;
Example of use, with asserts to indicate desired behavior. Note that to destroy the objects, one merely has to delete their instance. The compiler will generate the calls.
int main() {
Q_ASSERT(!AnimalFactory::instance());
{
AnimalFactory animals;
Q_ASSERT(AnimalFactory::instance());
auto *dog1 = AnimalFactory::CreateA("Dog");
Q_ASSERT(dynamic_cast<Dog*>(dog1));
Q_ASSERT(AnimalFactory::NameOfA(dog1) == Dog::className());
Q_ASSERT(dog1->Speaks() == QStringLiteral("Woof"));
auto *dog2 = AnimalFactory::CreateA(QLatin1String("Dog"));
Q_ASSERT(dynamic_cast<Dog*>(dog2));
auto *cat = AnimalFactory::CreateA("Cat");
Q_ASSERT(dynamic_cast<Cat*>(cat));
Q_ASSERT(cat->Speaks() == QStringLiteral("Meow"));
Q_ASSERT(AnimalFactory::NameOfA(cat) == Cat::className());
delete cat;
delete dog2;
delete dog1;
}
Q_ASSERT(!AnimalFactory::instance());
}

Keep track of each created template based singelton

for my project I need to create singletons of a generic type.
Those singletons manage the generic types in a std::map with ID to Object.
Here is my used code:
template <typename tComponent>
class InternalComponent {
public:
static InternalComponent& getInstance() {
static InternalComponent s_result;
return s_result;
}
void add(const tComponent& component, int id) {
m_components[id] = component;
}
void remove(int id) {
std::lock_guard<std::mutex> lock(m_mutex);
auto it = m_components.find(id);
if (it == m_components.end()) {
throw std::runtime_error("Component can't be found.");
}
m_components.erase(it, m_components.end());
}
void replace(const tComponent& component, int id) {
auto it = m_components.find(id);
if (it == m_components.end()) {
throw std::runtime_error("Component can't be found.");
}
m_components[id] = component;
}
tComponent* get(int id) {
return &m_components[id];
}
private:
InternalComponent() {};
InternalComponent(const InternalComponent&);
InternalComponent & operator = (const InternalComponent &);
std::mutex m_mutex;
std::map<int, tComponent> m_components;
};
In order to delete all Components with a specific ID from each singleton, I have to keep track of each created instance of the singleton.
At this point I'm stuck.
The first problem is the generic type which can't be saved to a vector.
I would bypass this with an Baseclass and derive InternalComponent from it.
However I would still be unable to save the references to a vector.
Also I'm unsure how to check if the singleton is created for the first time, without using an if-statement in each getInstance call, to avoid duplicates entries in my list of created singletons.
My final question would be: How can I manage each created instance of an InternalComponent in a single list.
I figured out how I can keep track of all my created template based singleton.
#include <iostream>
#include <vector>
class Base {
public:
virtual void delete(int id) = 0;
};
std::vector<Base*> test;
template<typename T>
class S : public Base
{
public:
void delete(int id) override {
//delete the component
};
static S& getInstance()
{
static S instance;
return instance;
}
private:
S() {
test.push_back(this);
};
public:
S(S const&) = delete;
void operator=(S const&) = delete;
};
int main()
{
S<int>::getInstance();
S<char>::getInstance();
S<char>::getInstance();
for (auto s : test) {
s->delete(666);
}
exit(0);
}
I use an abstract class to later store a template based class inside a vector. The class provides the functions which are later needed. The constructor only gets called once, which allows me to store the this pointer and avoiding uneeded checks.

Make variable class member outside of constructor

Basically, I need to set a variable outside of the constructor and make it accessible to the entire class.
It would need to work something like this:
#include <iostream>
#include <string>
template <typename MT>
class CallbackFunction
{
void (*func)(MT);
MT *data;
public:
void SetCallbackData (void (*f)(MT), MT *d)
{
func = f;
data = d;
}
void Call()
{
func(data);
}
};
class Callback
{
public:
template <typename T>
void SetCallback(CallbackFunction <T> *func)
{
// Need to make this a class member;
CallbackFunction <T> *CallbackClass = func;
}
void Call()
{
CallbackClass->Call();
}
};
template <typename CT>
Callback *NewCallback(void (*func)(CT), CT *data)
{
Callback *cb;
CallbackFunction <CT> *cf;
cf->SetCallbackData(func, data);
cb->SetCallback <CT> (cf);
return cb;
};
void Call(Callback *CallbackFunc)
{
CallbackFunc->Call();
}
void foo(std::string str)
{
std::cout << str << "\n";
}
int main()
{
std::string *str;
str->append("Hello, World!");
Call( NewCallback(foo, str) );
return 0;
}
More details:
I know it's buggy, and it doesn't compile, I'll sort out those bugs when I find a solution to my problem. Which is:
I need to find a way to declare a template variable inside a member function of the class "Callback". I need to do this because the class "Callback" cannot be a template, it needs to remain a simple class. So because the class "Callback" is not a template, I need to make one of it's member functions a template instead. So that member function can declare a variable of the type defined (with the template) when the function is called, and this variable needs to be accessible to the entire class.
So in a nice list:
class "Callback" cannot be a template,
variable CallbackClass must be accessible to the entire class,
but remain inside of the class.
#include <iostream>
#include <string>
#include <memory>
template <typename MT>
class CallbackFunction
{
typedef void (*func_ptr)(MT);
func_ptr f_ptr;
typedef std::shared_ptr<MT> data_ptr;
data_ptr data_p;
public:
void SetCallbackData (func_ptr f_ptr_, MT *d)
{
f_ptr = f_ptr_;
data_p.reset(d);
}
void Call()
{
if ( f_ptr ) f_ptr(data);
}
};
template<class T>
class Callback
{
public:
template <typename T>
void SetCallback(CallbackFunction <T> *func)
{
f_ptr.reset(func);
}
void Call()
{
if ( f_ptr ) f_ptr->Call();
}
typedef std::shared_ptr<CallbackFunction<T>> func_ptr;
static func_ptr f_ptr;
};
I would implement this using polymorphism. Your programming skills seem good so I will just sketch the direction to solution, feel free to ask for more help if needed.
// your callbackobjects inherit from this class, the sole purpose of this
// class is to provide the Call interface. The derived classes implement
// their custom version of Call().
class CallBackObject{
public:
virtual void Call(){};
};
class Callback
{
CallBackObject *callBackObject;
public:
void SetCallback(CallBackObject *o)
{
callBackObject = o;
}
void Call()
{
callBackObject -> Call();
}
};
Create an abstract interface Callback class and have your CallbackFunction<T> inherit from this. Have your Callback class hold a pointer to this abstract interface. Finally, have your Callback::SetCallback assign func to this pointer.
Here's some code to illustrate the idea:
class ICallback
{
public:
virtual ~ICallback() {}
virtual void Call() = 0;
};
template <typename MT>
class CallbackFunction : public ICallback
{
typedef void (*callback)(MT);
callback myfunc;
MT *data;
public:
CallbackFunction (callback f, MT *d) :
myfunc (f),
data (d)
{}
void Call()
{
if(myfunc && data)
{
myfunc(*data);
}
else throw std::logic_error("Callback function or data is null!");
}
};
Then have Callback hold a ICallback*:
class Callback
{
ICallback *mycallback;
public:
template <typename T>
void SetCallback(CallbackFunction <T> *func)
{
// Need to make this a class member;
// CallbackFunction <T> *CallbackClass = func;
mycallback = func;
}
void Call()
{
mycallback->Call();
}
};
The idea is to make all instantiated templates of CallbackFunction <T> a kind-of ICallback. Now the class using ICallback can take any class CallbackFunction <T> without needing to know what T is.