thezxtreme

generic shit

Jul 16th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. /*
  2.  * Delegate.h
  3.  *
  4.  *  Created on: 06.08.2014
  5.  *      Author: julian
  6.  */
  7. //TODO: all cases of functions
  8. #ifndef DELEGATE_H_
  9. #define DELEGATE_H_
  10. #include <functional>
  11. #include<string>
  12. namespace Events{
  13. #define delegate(name,type,...) typedef Events::Delegate<type (__VA_ARGS__)> name
  14. template<typename... Dummy>
  15. class Delegate;
  16. template<typename TRet,typename... Args>
  17. class Delegate<TRet(Args...)>
  18. {
  19. private:
  20.     std::function<TRet (Args...)> func;
  21. public:
  22.  
  23.     template<typename TClass>
  24.     Delegate(TRet (TClass::*fn)(Args...),TClass& instance)
  25.     {
  26.         this->func = [=](Args... args){ return (instance.*fn)(args...); };
  27.     }
  28.     template<typename TClass>
  29.     Delegate(TRet (TClass::*fn)(Args...),TClass* instance)
  30.     {
  31.         this->func = [=](Args... args){ return (instance->*fn)(args...); };
  32.     }
  33.  
  34.     bool operator ==(Delegate<TRet(Args...)>& other)
  35.     {
  36.         return (this->func.target_type() == other.func.target_type()/*TODO: &&
  37.                 this->func.target() == other.func.target()*/);
  38.     }
  39.     bool operator !=(Delegate<TRet(Args...)>& other)
  40.     {
  41.         return (this->func.target_type() != other.func.target_type() /*TODO:||
  42.                 this->func.target() != other.func.target()*/);
  43.     }
  44.     bool operator ==(Delegate<TRet(Args...)>* other)
  45.     {
  46.         return (this->func.target_type() == other->func.target_type()/*TODO: &&
  47.                 this->func.target() == other->func.target()*/);
  48.     }
  49.     bool operator !=(Delegate<TRet(Args...)>* other)
  50.     {
  51.         return (this->func.target_type() != other->func.target_type()/*TODO: ||
  52.                 this->func.target() != other->func.target()*/);
  53.     }
  54.     void operator()(Args... params)
  55.     {
  56.         func(params...);
  57.     }
  58.     /*TRet operator()(Args... params)
  59.     {
  60.         TODOfunc(params...);
  61.         //TODO: return func(params...); Copy whole class?
  62.     }*/
  63. };
  64.  
  65. template<typename TRet>
  66. class Delegate<TRet ()>
  67. {
  68. private:
  69.     std::function<TRet ()> func;
  70. public:
  71.     template<typename TClass>
  72.     Delegate(TRet (TClass::*fn)(),TClass& instance)
  73.     {
  74.         this->func = [=](){ return (instance.*fn)(); };
  75.     }
  76.     template<typename TClass>
  77.     Delegate(TRet (TClass::*fn)(),TClass* instance)
  78.     {
  79.         this->func = [=](){ return (instance->*fn)(); };
  80.     }
  81.     Delegate(std::function<TRet ()> func)
  82.     {
  83.         this->func = func;
  84.     }
  85.  
  86.     bool operator ==(Delegate<TRet()>& other)
  87.     {
  88.  
  89.         return (this->func.target_type() == other.func.target_type() &&
  90.                 this->func.target() == other.func.target());
  91.     }
  92.     bool operator !=(Delegate<TRet()>& other)
  93.     {
  94.         return (this->func.target_type() != other.func.target_type() ||
  95.                 this->func.target() != other.func.target());
  96.     }
  97.     bool operator ==(Delegate<TRet()>* other)
  98.     {
  99.         return (this->func.target_type() == other->func.target_type() &&
  100.                 this->func.target() == other->func.target());
  101.     }
  102.     bool operator !=(Delegate<TRet()>* other)
  103.     {
  104.         return (this->func.target_type() != other->func.target_type() ||
  105.                 this->func.target() != other->func.target());
  106.     }
  107.     TRet operator()()
  108.     {
  109.         return func();
  110.     }
  111. };
  112. } /* namespace Events */
  113.  
  114. #endif /* DELEGATE_H_ */
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. /*
  132.  * Event.h
  133.  *
  134.  *  Created on: 09.08.2014
  135.  *      Author: julian
  136.  */
  137.  
  138. #ifndef EVENT_H_
  139. #define EVENT_H_
  140. #include <vector>
  141. #include "Delegate.h"
  142. namespace Events {
  143. #define event(delegate,name) Events::Event<delegate> name;
  144. template<typename... Args>
  145. class Event;
  146. template<typename... Args>
  147. class Event<Delegate<void(Args...)>>{
  148. private:
  149.     std::vector<Delegate<void(Args...)>> delegates;
  150. public:
  151.     Event(){}
  152.     bool operator==(std::nullptr_t)
  153.     {
  154.         return delegates.size() == 0;
  155.     }
  156.     bool operator!=(std::nullptr_t)
  157.     {
  158.         return delegates.size() != 0;
  159.     }
  160.     void operator +=(Delegate<void(Args...)>* del)
  161.     {
  162.         for(Delegate<void(Args...)> & dl : delegates)
  163.         {
  164.             if (dl == del)
  165.                 return;
  166.         }
  167.         delegates.push_back(*del);
  168.         delete del;
  169.     }
  170.     void operator -=(Delegate<void(Args...)>* del)
  171.     {
  172.         for(Delegate<void,Args...>& dl : delegates)
  173.         {
  174.             if (dl == del)
  175.             {
  176.                 delegates.erase(dl);
  177.                 break;
  178.             }
  179.         }
  180.         delete del;
  181.     }
  182.  
  183.     void operator()(Args... params)
  184.     {
  185.         for(Delegate<void(Args...)>& dl : delegates)
  186.         {
  187.             dl(params...);
  188.         }
  189.     }
  190.     virtual ~Event()
  191.     {
  192.  
  193.     }
  194. };
  195.  
  196. } /* namespace Events */
  197.  
  198. #endif /* EVENT_H_ */
Add Comment
Please, Sign In to add comment