Mana
Event.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "common/Setup.h"
10 #include <functional>
11 #include <unordered_map>
12 
13 namespace mana
14 {
15  using EventNameType = uint32_t;
16  static constexpr EventNameType EventInvalidName = 0;
17 
18  template <typename... Arguments>
20  {
21  public:
22  using FunctionType = void(Arguments...);
23 
24  public:
25  Event() = default;
26  virtual ~Event() = default;
27 
28  [[nodiscard]] EventNameType Add(const std::function<FunctionType>& function)
29  {
30  static EventNameType index = 1;
31  mFunction[index] = function;
32  return index++;
33  }
34 
35  void Remove(const EventNameType name)
36  {
37  mFunction.erase(name);
38  }
39 
40  [[nodiscard]] bool Contain(const EventNameType name) const
41  {
42  return mFunction.contain(name);
43  }
44 
45  void Broadcast(Arguments... arguments) const
46  {
47  for (const auto& function : mFunction)
48  {
49  function.second(arguments...);
50  }
51  }
52 
53  private:
54  std::unordered_map<EventNameType, std::function<FunctionType>> mFunction;
55  };
56 }
Definition: Event.h:20
bool Contain(const EventNameType name) const
Definition: Event.h:40
void(Arguments...) FunctionType
Definition: Event.h:22
EventNameType Add(const std::function< FunctionType > &function)
Definition: Event.h:28
virtual ~Event()=default
Event()=default
void Remove(const EventNameType name)
Definition: Event.h:35
void Broadcast(Arguments... arguments) const
Definition: Event.h:45
Definition: CodeBuffer.cpp:12
uint32_t EventNameType
Definition: Event.h:15
Definition: Noncopyable.h:18