...
This commit is contained in:
147
cxx/tests/fsm.cpp
Normal file
147
cxx/tests/fsm.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#include <cxxabi.h>
|
||||
#include <iostream>
|
||||
#include <typeinfo>
|
||||
|
||||
#include "../mcc_finite_state_machine.h"
|
||||
|
||||
using namespace mcc;
|
||||
|
||||
|
||||
struct S {
|
||||
};
|
||||
|
||||
|
||||
struct EV1 {
|
||||
static constexpr std::string_view ID = "EV1";
|
||||
|
||||
void onTransit()
|
||||
{
|
||||
//
|
||||
std::cout << "EV1::onTransit()\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct EV2 {
|
||||
static constexpr std::string_view ID = "EV2";
|
||||
};
|
||||
|
||||
struct EV3 {
|
||||
static constexpr std::string_view ID = "EV3";
|
||||
};
|
||||
|
||||
struct EVN {
|
||||
static constexpr std::string_view ID = "EVN";
|
||||
};
|
||||
|
||||
struct ST1;
|
||||
struct ST2;
|
||||
|
||||
struct STN {
|
||||
static constexpr std::string_view ID = "STN";
|
||||
using transition_t = fsm::fsm_transition_table_t<std::pair<EV1, STN>, std::pair<EV2, ST2>>;
|
||||
|
||||
void enter()
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct ST3 {
|
||||
static constexpr std::string_view ID = "ST3";
|
||||
using transition_t = fsm::fsm_transition_table_t<std::pair<EV1, ST1>, std::pair<EV2, ST2>>;
|
||||
|
||||
void enter(EV3& ev)
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct ST2 {
|
||||
static constexpr std::string_view ID = "ST2";
|
||||
using transition_t = fsm::fsm_transition_table_t<std::pair<EV2, STN>, std::pair<EV3, ST3>>;
|
||||
|
||||
void enter(EV2& ev)
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct ST1 {
|
||||
static constexpr std::string_view ID = "ST1";
|
||||
using transition_t =
|
||||
fsm::fsm_transition_table_t<std::pair<EV1, ST2>, std::pair<EV2, STN>, std::pair<EV3, STN>, std::pair<EV3, STN>>;
|
||||
|
||||
void enter()
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
int tests_fsm(int, char**)
|
||||
{
|
||||
std::cout << "\n\n\n------- mcc_finite_state_machine.h -------\n";
|
||||
|
||||
|
||||
|
||||
fsm::MccFiniteStateMachine fsmach(ST1{});
|
||||
|
||||
std::cout << "STATES: ";
|
||||
for (auto& el : fsmach.stateIDs()) {
|
||||
std::cout << std::quoted(el) << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
|
||||
std::cout << "EVENTS: ";
|
||||
for (auto& el : fsmach.eventIDs()) {
|
||||
std::cout << std::quoted(el) << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
|
||||
fsmach.dispatchEvent<EV1>();
|
||||
fsmach.dispatchEvent<EV3>();
|
||||
fsmach.dispatchEvent<EV2>();
|
||||
|
||||
// fsmach.dispatchEvent<EVN>();
|
||||
|
||||
using tab_t = fsm::fsm_transition_table_t<std::pair<EV1, ST2>, std::pair<EV3, ST1>>;
|
||||
|
||||
using st_t = tab_t::find_state_by_event_t<EV2>;
|
||||
|
||||
int status;
|
||||
char* aa = abi::__cxa_demangle(typeid(st_t).name(), NULL, NULL, &status);
|
||||
std::cout << "aa = " << aa << "\n";
|
||||
free(aa);
|
||||
|
||||
std::cout << "\nAfter std::move\n";
|
||||
auto fsmach1 = std::move(fsmach);
|
||||
std::cout << "STATES: ";
|
||||
for (auto& el : fsmach1.stateIDs()) {
|
||||
std::cout << std::quoted(el) << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
std::cout << "current state: " << std::quoted(fsmach1.currentStateID()) << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user