rad  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
event.hpp
Go to the documentation of this file.
1 
9 #ifndef RAD_EVENT_HPP
10 #define RAD_EVENT_HPP
11 
12 #include <memory>
13 #include "rad/anyEvent.hpp"
14 #include "rad/detail/Holder.hpp"
15 
16 namespace rad {
17 
21 template <typename EVENT, typename = void>
22 class Event {
23  public:
24  char const* GetId() const { return EVENT::id; }
25 };
26 
31 template <typename EVENT>
32 class Event<EVENT, typename std::enable_if<!std::is_void<typename EVENT::payload_t>::value>::type> {
33  using payload_t = typename EVENT::payload_t;
34 
35  public:
37 
38  Event(Event const& e) = default;
39  Event(Event& e) = default;
40  Event(Event&& e) = default;
41  Event& operator=(Event const& e) = default;
43 
47  template <class... Args>
48  // typename std::enable_if<!std::is_same<Event<EVENT>, Args...>::value>::type>
49  Event(Args&&... args) : m_ptr{new detail::HolderTImpl<EVENT>{std::forward<Args>(args)...}} {}
50 
51  char const* GetId() const { return EVENT::id; }
52 
53  payload_t& GetPayload() {
54  return static_cast<detail::HolderTImpl<EVENT>*>(m_ptr.get())->m_payload;
55  }
56 
57  payload_t const& GetPayload() const { return const_cast<Event<EVENT>*>(this)->getPayload(); }
58 
59  AnyEvent MakeAny() {
60  // @todo: Move to a free function make_any(Event<EVENT>);
61  return AnyEvent(m_ptr);
62  }
63 
64  private:
65  // Use Holder to allow easy conversion to AnyEvent and vice-versa
66  std::shared_ptr<detail::Holder> m_ptr;
67 };
68 
69 } // namespace rad
70 
71 #endif // EVENT_EVENT_HPP
EVENT::payload_t & GetPayload(AnyEvent &ev)
Definition: getPayload.hpp:29
char const * GetId() const
Definition: event.hpp:24
Definition: event.hpp:22
Definition: anyEvent.hpp:52