2023-09-19 15:59:49 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include "xf/timeout.h"
|
|
|
|
#include "xf/initialevent.h"
|
|
|
|
#include "xf/behavior.h"
|
2023-10-17 15:12:51 +02:00
|
|
|
#include "trace/trace.h"
|
2023-09-19 15:59:49 +02:00
|
|
|
|
2023-10-01 17:52:37 +02:00
|
|
|
|
|
|
|
XFBehavior::XFBehavior() {
|
2023-10-17 14:18:38 +02:00
|
|
|
this->deleteOnTerminate_ = false;
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
XFBehavior::~XFBehavior() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void XFBehavior::startBehavior() {
|
2023-10-11 12:08:34 +02:00
|
|
|
GEN(XFInitialEvent());
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 18:28:47 +02:00
|
|
|
/**
|
|
|
|
* @brief Pushes the given event to the dispatcher.
|
|
|
|
*
|
|
|
|
* If the event has no behavior assigned, the behavior of this object is assigned.
|
|
|
|
* @param pEvent Event to push to the dispatcher.
|
|
|
|
*/
|
2023-10-01 17:52:37 +02:00
|
|
|
void XFBehavior::pushEvent(XFEvent *pEvent) {
|
2023-10-17 14:18:38 +02:00
|
|
|
if(pEvent->getBehavior()==nullptr) {
|
|
|
|
pEvent->setBehavior(this);
|
|
|
|
}
|
2023-10-11 12:08:34 +02:00
|
|
|
this->getDispatcher()->pushEvent(pEvent);
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool XFBehavior::deleteOnTerminate() const {
|
2023-10-17 14:18:38 +02:00
|
|
|
return this->deleteOnTerminate_;
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void XFBehavior::setDeleteOnTerminate(bool deleteBehaviour) {
|
2023-10-17 14:18:38 +02:00
|
|
|
this->deleteOnTerminate_ = deleteBehaviour;
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const XFEvent *XFBehavior::getCurrentEvent() const {
|
2023-10-17 14:18:38 +02:00
|
|
|
return this->pCurrentEvent_;
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface::XFDispatcher *XFBehavior::getDispatcher() {
|
2023-10-11 12:08:34 +02:00
|
|
|
return interface::XFDispatcher::getInstance();
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 18:28:47 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns a reference to the actually processed timeout.
|
|
|
|
*
|
|
|
|
* Will work only if the current event is of type IXFEvent::Timeout.
|
|
|
|
*
|
|
|
|
* @return Pointer to the currently processed timeout or nullptr if the current event is not a timeout.
|
|
|
|
*/
|
2023-10-01 17:52:37 +02:00
|
|
|
const XFTimeout *XFBehavior::getCurrentTimeout() {
|
2023-10-11 12:08:34 +02:00
|
|
|
if(pCurrentEvent_->getEventType() == XFEvent::Timeout) {
|
2023-10-17 14:18:38 +02:00
|
|
|
return (XFTimeout*) this->pCurrentEvent_;
|
2023-10-11 12:08:34 +02:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void XFBehavior::setCurrentEvent(const XFEvent *pEvent) {
|
2023-10-17 14:18:38 +02:00
|
|
|
this->pCurrentEvent_ = pEvent;
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 18:28:47 +02:00
|
|
|
/**
|
|
|
|
* @brief Processes the given event.
|
|
|
|
*
|
|
|
|
* The dispatcher calls this method every time a new event
|
|
|
|
* or timeout arrives. The process method stores the actual
|
|
|
|
* event using the #_pCurrentEvent and then calls
|
|
|
|
* processEvent().
|
|
|
|
*
|
|
|
|
* In case you intend to call process() inside your state machine you
|
|
|
|
* are doing something wrong! Call GEN() or pushEvent() instead!
|
|
|
|
*
|
|
|
|
* @param pEvent Event to process
|
|
|
|
* @return if the behavior is terminated (basically a boolean)
|
|
|
|
*/
|
2023-10-01 17:52:37 +02:00
|
|
|
XFBehavior::TerminateBehavior XFBehavior::process(const XFEvent *pEvent) {
|
2023-10-17 14:18:38 +02:00
|
|
|
this->setCurrentEvent(pEvent);
|
2023-10-21 18:28:47 +02:00
|
|
|
|
|
|
|
// Get status of the event processing
|
2023-10-11 12:08:34 +02:00
|
|
|
XFEventStatus status = XFEventStatus::Unknown;
|
2023-10-17 14:18:38 +02:00
|
|
|
status = this->processEvent();
|
2023-10-21 18:28:47 +02:00
|
|
|
|
|
|
|
// Check if event was consumed and if it should be deleted
|
2023-10-17 14:18:38 +02:00
|
|
|
if(status == XFEventStatus::Consumed && pEvent->deleteAfterConsume()) {
|
|
|
|
delete pEvent;
|
|
|
|
}
|
2023-10-21 18:28:47 +02:00
|
|
|
|
|
|
|
// Check if the behavior is terminated and return the result
|
2023-10-17 14:18:38 +02:00
|
|
|
XFBehavior::TerminateBehavior terminateBehavior = false;
|
|
|
|
if(status == XFEventStatus::Terminate) {
|
|
|
|
terminateBehavior = true;
|
2023-10-11 12:08:34 +02:00
|
|
|
}
|
2023-10-17 14:18:38 +02:00
|
|
|
return terminateBehavior;
|
2023-10-01 17:52:37 +02:00
|
|
|
}
|