Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #ifndef VULKANLAB_EVENT_H
  2. #define VULKANLAB_EVENT_H
  3.  
  4. #include <functional>
  5. #include <unordered_map>
  6.  
  7. template<typename... Args>
  8. class Event {
  9. public:
  10.     /**
  11.      * Event constructor
  12.      */
  13.     Event() : currentId(0) {}
  14.  
  15.     /**
  16.      * Event copy constructor
  17.      * @param other
  18.      */
  19.     Event(const Event &other) : currentId(0) {
  20.     }
  21.  
  22.     /*
  23.      * Event destructor
  24.      */
  25.     ~Event() {}
  26.  
  27.     /**
  28.      * Connect a function to the event.
  29.      * @tparam T
  30.      * @param instance
  31.      * @param func
  32.      * @return int - Connect function response
  33.      */
  34.     template<typename T>
  35.     int attach(T *instance, void (T::*func)(Args...)) {
  36.         return this->connect([=](Args... args) {
  37.             (instance->*func)(args...);
  38.         });
  39.     }
  40.  
  41.     /**
  42.      * Connect a std::function to the event.
  43.      * @param std::function - slot
  44.      * @return int - currentId
  45.      */
  46.     int connect(std::function<void(Args...)> const &slot) {
  47.         this->slots.insert(std::make_pair(++this->currentId, slot));
  48.         return this->currentId;
  49.     }
  50.  
  51.     /**
  52.      * Disconnect a previously connected function
  53.      * @param id
  54.      */
  55.     void disconnect(int id) const {
  56.         this->slots.erase(id);
  57.     }
  58.  
  59.     /**
  60.      * Call all the connected functions
  61.      * @param p
  62.      */
  63.     void emit(Args... p) {
  64.         for (auto const &it : this->slots) {
  65.             it.second(std::forward<Args>(p)...);
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Assignment operator, creates a new signal.
  71.      * @param other
  72.      * @return
  73.      */
  74.     Event &operator=(Event const &other) {
  75.         this->disconnectAll();
  76.     }
  77.  
  78.     /**
  79.      * Disconnect all the previously connected functions.
  80.      */
  81.     void disconnectAll() const {
  82.         this->slots.clear();
  83.     }
  84.  
  85.     /**
  86.      * Return the current event id.
  87.      * @return
  88.      */
  89.     int getCurrentId() const {
  90.         return this->currentId;
  91.     }
  92.  
  93.     /**
  94.      * Set the current event id.
  95.      * @param currentId
  96.      */
  97.     void setCurrentId(int currentId) {
  98.         this->currentId = currentId;
  99.     }
  100.  
  101. private:
  102.     int currentId;
  103.     std::unordered_map<int, std::function<void(Args...)>> slots;
  104. };
  105.  
  106. #endif //VULKANLAB_EVENT_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement