Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Delegate.h
- *
- * Created on: 06.08.2014
- * Author: julian
- */
- //TODO: all cases of functions
- #ifndef DELEGATE_H_
- #define DELEGATE_H_
- #include <functional>
- #include<string>
- namespace Events{
- #define delegate(name,type,...) typedef Events::Delegate<type (__VA_ARGS__)> name
- template<typename... Dummy>
- class Delegate;
- template<typename TRet,typename... Args>
- class Delegate<TRet(Args...)>
- {
- private:
- std::function<TRet (Args...)> func;
- public:
- template<typename TClass>
- Delegate(TRet (TClass::*fn)(Args...),TClass& instance)
- {
- this->func = [=](Args... args){ return (instance.*fn)(args...); };
- }
- template<typename TClass>
- Delegate(TRet (TClass::*fn)(Args...),TClass* instance)
- {
- this->func = [=](Args... args){ return (instance->*fn)(args...); };
- }
- bool operator ==(Delegate<TRet(Args...)>& other)
- {
- return (this->func.target_type() == other.func.target_type()/*TODO: &&
- this->func.target() == other.func.target()*/);
- }
- bool operator !=(Delegate<TRet(Args...)>& other)
- {
- return (this->func.target_type() != other.func.target_type() /*TODO:||
- this->func.target() != other.func.target()*/);
- }
- bool operator ==(Delegate<TRet(Args...)>* other)
- {
- return (this->func.target_type() == other->func.target_type()/*TODO: &&
- this->func.target() == other->func.target()*/);
- }
- bool operator !=(Delegate<TRet(Args...)>* other)
- {
- return (this->func.target_type() != other->func.target_type()/*TODO: ||
- this->func.target() != other->func.target()*/);
- }
- void operator()(Args... params)
- {
- func(params...);
- }
- /*TRet operator()(Args... params)
- {
- TODOfunc(params...);
- //TODO: return func(params...); Copy whole class?
- }*/
- };
- template<typename TRet>
- class Delegate<TRet ()>
- {
- private:
- std::function<TRet ()> func;
- public:
- template<typename TClass>
- Delegate(TRet (TClass::*fn)(),TClass& instance)
- {
- this->func = [=](){ return (instance.*fn)(); };
- }
- template<typename TClass>
- Delegate(TRet (TClass::*fn)(),TClass* instance)
- {
- this->func = [=](){ return (instance->*fn)(); };
- }
- Delegate(std::function<TRet ()> func)
- {
- this->func = func;
- }
- bool operator ==(Delegate<TRet()>& other)
- {
- return (this->func.target_type() == other.func.target_type() &&
- this->func.target() == other.func.target());
- }
- bool operator !=(Delegate<TRet()>& other)
- {
- return (this->func.target_type() != other.func.target_type() ||
- this->func.target() != other.func.target());
- }
- bool operator ==(Delegate<TRet()>* other)
- {
- return (this->func.target_type() == other->func.target_type() &&
- this->func.target() == other->func.target());
- }
- bool operator !=(Delegate<TRet()>* other)
- {
- return (this->func.target_type() != other->func.target_type() ||
- this->func.target() != other->func.target());
- }
- TRet operator()()
- {
- return func();
- }
- };
- } /* namespace Events */
- #endif /* DELEGATE_H_ */
- /*
- * Event.h
- *
- * Created on: 09.08.2014
- * Author: julian
- */
- #ifndef EVENT_H_
- #define EVENT_H_
- #include <vector>
- #include "Delegate.h"
- namespace Events {
- #define event(delegate,name) Events::Event<delegate> name;
- template<typename... Args>
- class Event;
- template<typename... Args>
- class Event<Delegate<void(Args...)>>{
- private:
- std::vector<Delegate<void(Args...)>> delegates;
- public:
- Event(){}
- bool operator==(std::nullptr_t)
- {
- return delegates.size() == 0;
- }
- bool operator!=(std::nullptr_t)
- {
- return delegates.size() != 0;
- }
- void operator +=(Delegate<void(Args...)>* del)
- {
- for(Delegate<void(Args...)> & dl : delegates)
- {
- if (dl == del)
- return;
- }
- delegates.push_back(*del);
- delete del;
- }
- void operator -=(Delegate<void(Args...)>* del)
- {
- for(Delegate<void,Args...>& dl : delegates)
- {
- if (dl == del)
- {
- delegates.erase(dl);
- break;
- }
- }
- delete del;
- }
- void operator()(Args... params)
- {
- for(Delegate<void(Args...)>& dl : delegates)
- {
- dl(params...);
- }
- }
- virtual ~Event()
- {
- }
- };
- } /* namespace Events */
- #endif /* EVENT_H_ */
Add Comment
Please, Sign In to add comment