rad  3.0.0
smEvent.hpp
Go to the documentation of this file.
1 
10 #ifndef RAD_SM_EVENT_HPP
11 #define RAD_SM_EVENT_HPP
12 
13 #include <rad/anyEvent.hpp>
14 #include <rad/exceptions.hpp>
15 #include <rad/getPayload.hpp>
16 
17 #include <scxml4cpp/Context.h>
18 #include <scxml4cpp/Event.h>
19 
20 namespace rad {
21 
25 class SMEvent : public scxml4cpp::Event {
26  public:
27  SMEvent();
28  explicit SMEvent(const AnyEvent& ev);
29  SMEvent(const SMEvent& e);
30  virtual ~SMEvent();
31  SMEvent& operator=(const SMEvent& e);
32 
33  AnyEvent const& GetEv() const;
34  void SetEv(AnyEvent const& ev);
35 
36  private:
37  UniqueEvent m_ev;
38 };
39 
40 template <typename EVENT>
41 const EVENT& GetLastEvent(scxml4cpp::Context* c) {
42  if (c == nullptr) {
43  throw rad::Exception("SCXML4CPP Context is NULL.");
44  }
45  SMEvent* e = dynamic_cast<SMEvent*>(c->getLastEvent());
46  if (e == nullptr) {
47  throw rad::Exception("SCXML4CPP Context is NULL.");
48  }
49  return static_cast<const EVENT&>(*e->getPayload());
50 }
51 
53 
54 template <typename EVENT>
55 typename EVENT::payload_t& GetLastEventPayload(scxml4cpp::Context* c) {
56  const EVENT& ev = GetLastEvent<EVENT>(c);
57  if (typeid(ev) != typeid(EVENT)) {
58  throw IncorrectEventType("Wrong event type");
59  }
60  return static_cast<EVENT&>(ev).GetPayload();
61 }
62 
63 template <typename EVENT>
64 typename EVENT::payload_t const* GetLastEventPayloadNothrow(scxml4cpp::Context* c) {
65  if (c == nullptr) {
66  return nullptr;
67  }
68  if (c->getLastEvent() == nullptr) {
69  return nullptr;
70  }
71  SMEvent* e = dynamic_cast<SMEvent*>(c->getLastEvent());
72  if (e == nullptr) {
73  return nullptr;
74  }
75  return rad::GetPayloadNothrow<EVENT>(e->GetEv());
76 }
77 
78 } // namespace rad
79 
80 #endif // RAD_SM_EVENT_HPP
rad::UniqueEvent
std::unique_ptr< AnyEvent > UniqueEvent
Definition: anyEvent.hpp:45
rad::SMEvent::SetEv
void SetEv(AnyEvent const &ev)
Definition: smEvent.cpp:62
rad::Exception
Definition: exceptions.hpp:45
rad::SMEvent::operator=
SMEvent & operator=(const SMEvent &e)
Definition: smEvent.cpp:46
scxml4cpp::Event::getPayload
void * getPayload() const
Definition: Event.cpp:101
rad::GetLastEvent
const EVENT & GetLastEvent(scxml4cpp::Context *c)
Definition: smEvent.hpp:41
rad::GetPayload
EVENT::payload_t & GetPayload(AnyEvent &ev)
Definition: getPayload.hpp:29
rad::GetLastEventPayloadNothrow
EVENT::payload_t const * GetLastEventPayloadNothrow(scxml4cpp::Context *c)
Definition: smEvent.hpp:64
rad::SMEvent
Definition: smEvent.hpp:25
scxml4cpp::Context::getLastEvent
Event * getLastEvent() const
Definition: Context.cpp:75
rad::GetLastAnyEvent
const rad::AnyEvent & GetLastAnyEvent(scxml4cpp::Context *c)
Definition: smEvent.cpp:69
rad
Definition: actionCallback.hpp:21
rad::AnyEvent
Definition: anyEvent.hpp:52
rad::SMEvent::~SMEvent
virtual ~SMEvent()
Definition: smEvent.cpp:38
rad::GetLastEventPayload
EVENT::payload_t & GetLastEventPayload(scxml4cpp::Context *c)
Definition: smEvent.hpp:55
rad::SMEvent::SMEvent
SMEvent()
Definition: smEvent.cpp:17
scxml4cpp::Event
Definition: Event.h:39
rad::SMEvent::GetEv
AnyEvent const & GetEv() const
Definition: smEvent.cpp:57
rad::IncorrectEventType
Definition: anyEvent.hpp:19
scxml4cpp::Context
Definition: Context.h:44