State system + match d_t_rock_boat

This commit is contained in:
robojumper
2024-05-31 02:50:17 +02:00
parent fda931e2ae
commit 0e3b13ee3e
31 changed files with 1013 additions and 144 deletions
+74
View File
@@ -0,0 +1,74 @@
#include <common.h>
#include <s/s_StateInterfaces.hpp>
#include <s/s_StateMethod.hpp>
#include <s/s_StateID.hpp>
sStateMethod_c::sStateMethod_c(sStateIDChkIf_c &checker, sStateFctIf_c &factory, const sStateIDIf_c &initialState) :
mpStateChk(checker),
mpStateFct(factory),
mInitFinalizeLock(false),
mExecutionLock(false),
mIsValid(false),
mStateChanged(false),
mRefreshStateMethod(false),
mpNewStateID(&initialState),
mpOldStateID(&sStateID::null),
mpStateID(&initialState),
mpState(nullptr) {
}
sStateMethod_c::~sStateMethod_c() {}
void sStateMethod_c::initializeStateMethod() {
if (!mpNewStateID->isNull() && !mInitFinalizeLock && !mIsValid) {
mInitFinalizeLock = true;
mpStateID = mpNewStateID;
int ret = initializeStateLocalMethod();
if (ret != 0) {
mIsValid = true;
} else {
mIsValid = false;
}
mInitFinalizeLock = false;
}
}
void sStateMethod_c::executeStateMethod() {
if (!mExecutionLock) {
int i = 2;
do {
if (mRefreshStateMethod) {
i--;
}
mRefreshStateMethod = false;
// We only want to execute if we have a valid next state
if (!mpNewStateID->isNull()) {
mExecutionLock = true;
executeStateLocalMethod();
mExecutionLock = false;
}
} while (mRefreshStateMethod && mStateChanged && i);
}
}
void sStateMethod_c::finalizeStateMethod() {
if (!mpNewStateID->isNull() && mIsValid && !mInitFinalizeLock) {
mInitFinalizeLock = true;
mpOldStateID = mpStateID;
finalizeStateLocalMethod();
mIsValid = false;
mInitFinalizeLock = false;
}
}
void sStateMethod_c::changeStateMethod(const sStateIDIf_c &newID) {
if (!newID.isNull()) {
mpNewStateID = &newID;
changeStateLocalMethod(newID);
mStateChanged = true;
}
}