- Trying to create a new eventHandler using >> overloaded operator, lost myself trying to figure out the required syntax
- link >> new eventHandler(&App::testFunction, App);
- class Link
- {
- public:
- eventListener* EventListener;
- Link()
- {
- this->EventListener = new eventListener();
- }
- // PROBLEM
- // I am lost here, tried different syntaxes but with no success
- //template<template<class G> class T, class F>
- template <class T>
- Link operator>>=(T& ev)
- {
- cout << "something";
- // Here there is no way to declare the proper eventHandler
- eventHandler<?>* event = (eventHandler<?>)ev;
- // I need something like T<F>
- event->eventName = 'onTest';
- this->eventListener->add(event);
- return *this;
- }
- };
- template<class T, class F>
- T operator>>(T& lhs, F& rhs)
- {
- return T(lhs)>>=rhs;
- }
- class App
- {
- public:
- void testFunction(e evt)
- {
- cout << "it works!" << "n";
- }
- };
- int main()
- {
- App* app = new App;
- Link* link = new link;
- Link link1;
- eventHandler<App>* ev = new eventHandler<App>(app, &App::testFunction);
- link1 >> ev;
- // this line should echo "it works!"
- link1.EventListener->triggerEvent("onTest");
- // PART 2
- // HOW CAN I USE?
- // link >> ev;
- // when link is a Link*
- return 0;
- }
- #include <iostream>
- #include <stdio.h>
- #include "events/events.h"
- using namespace std;
- class Link
- {
- public:
- eventListener* EventListener;
- public:
- Link()
- {
- this->EventListener = new eventListener();
- }
- template<class T>
- Link operator>>=(const T& ev)
- {
- ev->eventName = "onReceive";
- this->EventListener->add(ev);
- return *this;
- }
- };
- template<class T, class F>
- T operator>>(T& lhs, const F& rhs)
- {
- return T(lhs)<<=rhs;
- }
- class App
- {
- public:
- void testReceive(e evt)
- {
- cout << "it works" << "n" << evt.value;
- }
- };
- class demo
- {
- public:
- Link* parent;
- void testit(char* msg)
- {
- parent->EventListener->triggerEvent("onReceive", this, msg);
- }
- };
- int main()
- {
- App* app = new App;
- Link link;
- eventHandler<App>* ev = new eventHandler<App>(app, &App::testReceive);
- link >> ev;
- demo d;
- d.parent = &link;
- // should output "it works!"
- d.testit("here");
- return 0;
- }
- #ifndef EVENTS_H
- #define EVENTS_H
- #include <stdio.h>
- #include <string.h>
- #include "clist.h"
- #include <string>
- enum scope {global = 0, scoped};
- struct e
- {
- void* target;
- void* value;
- };
- class efunctor //abstract
- {
- public:
- std::string eventname;
- virtual void operator()(e evt)
- { }
- virtual void Call(e evt)
- { }
- };
- template <class T>
- class eventHandler : public efunctor
- {
- private:
- T* scope;
- void (T::*eventMethod)(e);
- public:
- std::string name;
- std::string eventname;
- eventHandler(std::string eventnam, T* objscope, void(T::*func)(e))
- {
- this->scope = objscope;
- this->eventMethod = func;
- this->eventname = eventnam;
- }
- eventHandler(T* objscope, void(T::*func)(e))
- {
- this->scope = objscope;
- this->eventMethod = func;
- }
- eventHandler(void(T::*func)(e))
- {
- this->eventMethod = func;
- }
- void operator()(e evt)
- {
- (scope->*eventMethod)(evt);
- }
- void Call(e evt)
- {
- (scope->*eventMethod)(evt);
- }
- };
- class eventListener
- {
- private:
- clist< clist<efunctor* > > methods;
- public:
- template <class T>
- void add(T other)
- {
- other->name = ToString(this->methods[other->eventname].length());
- methods[other->eventname][methods[other->eventname].length()] = other;
- }
- template <class T>
- void remove(T other)
- {
- methods[other->eventname]->remove(other->name);
- }
- template <class F>
- void triggerEvent(std::string name, void* target, F result)
- {
- e evt;
- evt.target = target;
- evt.value = (char*)result;
- for(methods[name].iterateStart();
- !methods[name].eoi();
- methods[name].next())
- {
- (*(methods[name].getCurrentIteration()))(evt);
- }
- }
- };
- #endif
- event<void(std::string)> onTest;
- onTest >> std::bind(std::mem_fn(&App::testFunction), std::ref(app), _1);
- #include <iostream>
- #include <memory>
- #include <algorithm>
- #include <vector>
- struct event_base
- {
- virtual ~event_base() {}
- virtual void call(std::string const&) = 0;
- };
- template <typename T>
- struct event: event_base
- {
- event(T* o, void (T::*m)(std::string const&)):
- object(o),
- member(m)
- {
- }
- private:
- void call(std::string const& arg) {
- (this->object->*member)(arg);
- }
- T* object;
- void (T::*member)(std::string const&);
- };
- template <typename T>
- event_base* make_event(T* object, void (T::*member)(std::string const&)) {
- return new event<T>(object, member);
- }
- class event_listener
- {
- public:
- ~event_listener() { std::for_each(events.begin(), events.end(), &deleter); }
- void add(event_base* event) { this->events.push_back(event); }
- void trigger(std::string const& argument) const {
- for (std::vector<event_base*>::const_iterator it(events.begin()), end(events.end());
- it != end; ++it) {
- (*it)->call(argument);
- }
- }
- private:
- static void deleter(event_base* ptr) { delete ptr; }
- std::vector<event_base*> events;
- };
- class Link
- {
- public:
- event_listener listener;
- void operator>>= (event_base* ev) { this->listener.add(ev); }
- };
- void operator<< (Link& link, event_base* ev) {
- link >>= ev;
- }
- class App
- {
- public:
- void onTest(std::string const& arg) { std::cout << "App::onTest(" << arg << ")n"; }
- };
- int main()
- {
- std::auto_ptr<Link> link(new Link);
- std::auto_ptr<App> app(new App);
- *link << make_event(app.get(), &App::onTest);
- link->listener.trigger("hello event");
- }
- template<typename T>
- const link &operator>>=(const eventHandler<T>& ev)
- {
- ev->eventName = "onTest";
- this->EventListener = &ev;
- }