Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. // MoveTest.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <set>
  7. #include <functional>
  8.  
  9. using namespace std;
  10.  
  11. template <typename ...Args>
  12.     using delegate = std::function<void(Args...)>;
  13.  
  14.  
  15. template <typename ...Args>
  16. class Event
  17. {
  18.     private:
  19.         using func = delegate<Args...>;
  20.         std::set<func> eventSet;
  21.  
  22.     public:
  23.         void operator+=(func f)
  24.         {
  25.             eventSet.insert(f);
  26.         }
  27.  
  28.         void operator-=(func f)
  29.         {
  30.             eventSet.erase(f);
  31.         }
  32.  
  33.         void operator()(Args...a)
  34.         {
  35.             for (auto x : eventSet)
  36.                 x(a...);
  37.         }
  38. };
  39.  
  40. void PrintNum(int x) {
  41.     std::cout << x << std::endl;
  42. }
  43.  
  44. int main() {
  45.  
  46.     int a = 45;
  47.  
  48.     Event<int> event;
  49.  
  50.     event += ([&](int x) { PrintNum(x); });
  51.  
  52.     event(3);
  53.  
  54.     system("pause");
  55.     return 0;
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement