diff --git a/README.md b/README.md index e69e5348b8ba62be08606c94acc69b88d35d3e20..b9acb16c99935b45a7fde51a41432805ce92ee60 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,8 @@ cpp-tbox,全称: C++ Treasure Box,C++开发百宝箱,是基于事件的服 - mqtt,MQTT客户端库; - terminal, 终端,类似shell的命令终端,可实现运行时与程序进行命令交互; - main,应用程序框架,实现了完备的程序启动流程与框架,让开发者只需关心业务代码; -- sample,基于main实现的应用程序示例; -- timer,定时器模块,实现了4种常用的定时器:CRON定时器、单次定时器、星期定时器、工作日定时器; -- action,动作模块,解决异步模式下的顺序性动作执行困难问题; +- alarm,闹钟模块,实现了4种常用的闹钟:CRON闹钟、单次闹钟、星期循环闹钟、工作日闹钟; +- flow,动作模块,解决异步模式下的顺序性动作执行困难问题; #### 外部库依赖 @@ -43,7 +42,11 @@ cpp-tbox,全称: C++ Treasure Box,C++开发百宝箱,是基于事件的服 **如果构建** -进入到cpp-tbox的顶层目录,执行命令: `STAGING_DIR=$HOME/.local make modules RELEASE=1`。 +进入到cpp-tbox的顶层目录,执行命令: +``` +STAGING_DIR=$HOME/.local make modules RELEASE=1 +``` + 完成之后,所有的头文件导出到 `$HOME/.local/include/`,所有的库文件输出到 `$HOME/.local/lib/`。 如果你没有指定 `STAGING_DIR` 参数,它默认为 `.staging`。 diff --git a/config.mk b/config.mk index f3418b318b9414cf24f54cc6ad2546b339a8de66..1ac189a81b6ba50a0028743c6d223d2304441ed5 100644 --- a/config.mk +++ b/config.mk @@ -9,5 +9,5 @@ MODULES += coroutine MODULES += mqtt MODULES += terminal MODULES += main -MODULES += action -MODULES += timer +MODULES += flow +MODULES += alarm diff --git a/examples/timer/Makefile b/examples/alarm/Makefile similarity index 100% rename from examples/timer/Makefile rename to examples/alarm/Makefile diff --git a/examples/timer/cron_timer/Makefile b/examples/alarm/cron_alarm/Makefile similarity index 76% rename from examples/timer/cron_timer/Makefile rename to examples/alarm/cron_alarm/Makefile index 9de17246789345fe5f06ea90a5da51055619d38b..5bb426895de1d67bd60c2217eb1373ced1a95339 100644 --- a/examples/timer/cron_timer/Makefile +++ b/examples/alarm/cron_alarm/Makefile @@ -1,10 +1,10 @@ -EXE_NAME := example/timer/cron_timer +EXE_NAME := example/alarm/cron_alarm CPP_SRC_FILES := main.cpp CXXFLAGS := -DLOG_MODULE_ID='"$(EXE_NAME)"' $(CXXFLAGS) LDFLAGS += \ - -ltbox_timer \ + -ltbox_alarm \ -ltbox_event \ -ltbox_base \ diff --git a/examples/timer/cron_timer/main.cpp b/examples/alarm/cron_alarm/main.cpp similarity index 94% rename from examples/timer/cron_timer/main.cpp rename to examples/alarm/cron_alarm/main.cpp index 129e29712387abd32f2339de97551af99f80e588..42f699c73214559737ad4450baca4be1504e1f8d 100644 --- a/examples/timer/cron_timer/main.cpp +++ b/examples/alarm/cron_alarm/main.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include using namespace std; using namespace tbox; @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) { Loop* sp_loop = Loop::New(); SetScopeExitAction([sp_loop] { delete sp_loop; }); - timer::CronTimer tmr(sp_loop); + alarm::CronAlarm tmr(sp_loop); tmr.initialize(cron_expr); if (argc >= 4) { LogInfo("timezone:%d", timezone_offset_minutes); diff --git a/examples/timer/oneshot_timer/Makefile b/examples/alarm/oneshot_alarm/Makefile similarity index 75% rename from examples/timer/oneshot_timer/Makefile rename to examples/alarm/oneshot_alarm/Makefile index 8a119c9e8869362fe854f34873cba77c57bde045..32104f174b1aa089ab9c16f32376d59a554f98b8 100644 --- a/examples/timer/oneshot_timer/Makefile +++ b/examples/alarm/oneshot_alarm/Makefile @@ -1,10 +1,10 @@ -EXE_NAME := example/timer/oneshot_timer +EXE_NAME := example/alarm/oneshot_alarm CPP_SRC_FILES := main.cpp CXXFLAGS := -DLOG_MODULE_ID='"$(EXE_NAME)"' $(CXXFLAGS) LDFLAGS += \ - -ltbox_timer \ + -ltbox_alarm \ -ltbox_event \ -ltbox_base \ diff --git a/examples/timer/oneshot_timer/main.cpp b/examples/alarm/oneshot_alarm/main.cpp similarity index 94% rename from examples/timer/oneshot_timer/main.cpp rename to examples/alarm/oneshot_alarm/main.cpp index f9b9323f53cf8979fc1533f96a1cb2f08aa3f6f1..6f9a0074438fd9565edf29222759917c2a92c3b6 100644 --- a/examples/timer/oneshot_timer/main.cpp +++ b/examples/alarm/oneshot_alarm/main.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include using namespace std; using namespace tbox; @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) { Loop* sp_loop = Loop::New(); SetScopeExitAction([sp_loop] { delete sp_loop; }); - timer::OneshotTimer tmr(sp_loop); + alarm::OneshotAlarm tmr(sp_loop); tmr.initialize(seconds_from_00); if (argc >= 4) { LogInfo("timezone:%d", timezone_offset_minutes); diff --git a/examples/timer/weekly_timer/Makefile b/examples/alarm/weekly_alarm/Makefile similarity index 75% rename from examples/timer/weekly_timer/Makefile rename to examples/alarm/weekly_alarm/Makefile index caed9d75185ca4c18327040ff0ddc93633f732a6..a8100ef06de1681dda0d4bb5a2962b767b50e83b 100644 --- a/examples/timer/weekly_timer/Makefile +++ b/examples/alarm/weekly_alarm/Makefile @@ -1,10 +1,10 @@ -EXE_NAME := example/timer/weekly_timer +EXE_NAME := example/alarm/weekly_alarm CPP_SRC_FILES := main.cpp CXXFLAGS := -DLOG_MODULE_ID='"$(EXE_NAME)"' $(CXXFLAGS) LDFLAGS += \ - -ltbox_timer \ + -ltbox_alarm \ -ltbox_event \ -ltbox_base \ diff --git a/examples/timer/weekly_timer/main.cpp b/examples/alarm/weekly_alarm/main.cpp similarity index 94% rename from examples/timer/weekly_timer/main.cpp rename to examples/alarm/weekly_alarm/main.cpp index 109c7e719c8d6fb51c8795aac368684279acce14..f79623c1e69474e8a1dca649839e8410dee5ebb4 100644 --- a/examples/timer/weekly_timer/main.cpp +++ b/examples/alarm/weekly_alarm/main.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include using namespace std; using namespace tbox; @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) { Loop* sp_loop = Loop::New(); SetScopeExitAction([sp_loop] { delete sp_loop; }); - timer::WeeklyTimer tmr(sp_loop); + alarm::WeeklyAlarm tmr(sp_loop); tmr.initialize(seconds_from_00, week_mask); if (argc >= 4) { LogInfo("timezone:%d", timezone_offset_minutes); diff --git a/modules/timer/3rd-party/ccronexpr.cpp b/modules/alarm/3rd-party/ccronexpr.cpp similarity index 100% rename from modules/timer/3rd-party/ccronexpr.cpp rename to modules/alarm/3rd-party/ccronexpr.cpp diff --git a/modules/timer/3rd-party/ccronexpr.h b/modules/alarm/3rd-party/ccronexpr.h similarity index 100% rename from modules/timer/3rd-party/ccronexpr.h rename to modules/alarm/3rd-party/ccronexpr.h diff --git a/modules/timer/Makefile b/modules/alarm/Makefile similarity index 57% rename from modules/timer/Makefile rename to modules/alarm/Makefile index 836fade6af799d810eb7cc38facd8a77abf18b9a..10042a9a31f349e6a8808b69078d500eef9c8cb8 100644 --- a/modules/timer/Makefile +++ b/modules/alarm/Makefile @@ -1,26 +1,26 @@ -LIB_NAME = timer +LIB_NAME = alarm LIB_VERSION_X = 0 LIB_VERSION_Y = 0 LIB_VERSION_Z = 1 HEAD_FILES = \ - timer.h \ - weekly_timer.h \ - oneshot_timer.h \ - cron_timer.h \ - workday_timer.h \ + alarm.h \ + weekly_alarm.h \ + oneshot_alarm.h \ + cron_alarm.h \ + workday_alarm.h \ workday_calendar.h \ CPP_SRC_FILES = \ - timer.cpp \ - weekly_timer.cpp \ - oneshot_timer.cpp \ - cron_timer.cpp \ + alarm.cpp \ + weekly_alarm.cpp \ + oneshot_alarm.cpp \ + cron_alarm.cpp \ 3rd-party/ccronexpr.cpp \ - workday_timer.cpp \ + workday_alarm.cpp \ workday_calendar.cpp \ -CXXFLAGS := -DLOG_MODULE_ID='"timer"' $(CXXFLAGS) +CXXFLAGS := -DLOG_MODULE_ID='"alarm"' $(CXXFLAGS) TEST_CPP_SRC_FILES = \ workday_calendar_test.cpp \ diff --git a/modules/timer/timer.cpp b/modules/alarm/alarm.cpp similarity index 89% rename from modules/timer/timer.cpp rename to modules/alarm/alarm.cpp index 406bd7e183eaaaa31c407d44a90b8bba8a664553..11eb81613c605cb2ef3f6ac18b4820b1c4f288f4 100644 --- a/modules/timer/timer.cpp +++ b/modules/alarm/alarm.cpp @@ -1,4 +1,4 @@ -#include "timer.h" +#include "alarm.h" #include @@ -8,32 +8,32 @@ #include namespace tbox { -namespace timer { +namespace alarm { -Timer::Timer(event::Loop *wp_loop) : +Alarm::Alarm(event::Loop *wp_loop) : wp_loop_(wp_loop), sp_timer_ev_(wp_loop->newTimerEvent()) { sp_timer_ev_->setCallback([this] { onTimeExpired(); }); } -Timer::~Timer() { +Alarm::~Alarm() { TBOX_ASSERT(cb_level_ == 0); //!< 防止回调中析构 cleanup(); delete sp_timer_ev_; } -void Timer::setTimezone(int offset_minutes) { +void Alarm::setTimezone(int offset_minutes) { timezone_offset_seconds_ = offset_minutes * 60; using_independ_timezone_ = true; } -bool Timer::isEnabled() const { +bool Alarm::isEnabled() const { return state_ == State::kRunning; } -bool Timer::enable() { +bool Alarm::enable() { if (state_ == State::kInited) { if (onEnable()) return activeTimer(); @@ -43,7 +43,7 @@ bool Timer::enable() { return false; } -bool Timer::disable() { +bool Alarm::disable() { if (state_ == State::kRunning) { if (onDisable()) { state_ = State::kInited; @@ -53,7 +53,7 @@ bool Timer::disable() { return false; } -void Timer::cleanup() { +void Alarm::cleanup() { if (state_ < State::kInited) return; @@ -66,7 +66,7 @@ void Timer::cleanup() { target_utc_ts_ = 0; } -void Timer::refresh() { +void Alarm::refresh() { if (state_ == State::kRunning) { state_ = State::kInited; sp_timer_ev_->disable(); @@ -74,7 +74,7 @@ void Timer::refresh() { } } -uint32_t Timer::remainSeconds() const { +uint32_t Alarm::remainSeconds() const { if (state_ == State::kRunning) { struct timeval utc_tv; int ret = gettimeofday(&utc_tv, nullptr); @@ -102,7 +102,7 @@ int GetSystemTimezoneOffsetSeconds() { } } -bool Timer::activeTimer() { +bool Alarm::activeTimer() { //! 使用 gettimeofday() 获取当前0时区的时间戳,精确到微秒 struct timeval utc_tv; int ret = gettimeofday(&utc_tv, nullptr); @@ -132,7 +132,7 @@ bool Timer::activeTimer() { return true; } -void Timer::onTimeExpired() { +void Alarm::onTimeExpired() { state_ = State::kInited; activeTimer(); diff --git a/modules/timer/timer.h b/modules/alarm/alarm.h similarity index 91% rename from modules/timer/timer.h rename to modules/alarm/alarm.h index aeef0e71b46405658d0d5770cfed052e308d4e22..8ee916fe0505d93b5169d7e987f0258ed1e6fa70 100644 --- a/modules/timer/timer.h +++ b/modules/alarm/alarm.h @@ -1,21 +1,21 @@ -#ifndef TBOX_TIMER_TIMER_H_20221022 -#define TBOX_TIMER_TIMER_H_20221022 +#ifndef TBOX_ALARM_ALARM_H_20221022 +#define TBOX_ALARM_ALARM_H_20221022 #include #include #include namespace tbox { -namespace timer { +namespace alarm { /** * 定时器基类 */ -class Timer +class Alarm { public: - explicit Timer(event::Loop *wp_loop); - virtual ~Timer(); + explicit Alarm(event::Loop *wp_loop); + virtual ~Alarm(); using Callback = std::function; void setCallback(const Callback &cb) { cb_ = cb; } @@ -96,4 +96,4 @@ class Timer } } -#endif //TBOX_TIMER_TIMER_H_20221022 +#endif //TBOX_ALARM_ALARM_H_20221022 diff --git a/modules/timer/cron_timer.cpp b/modules/alarm/cron_alarm.cpp similarity index 72% rename from modules/timer/cron_timer.cpp rename to modules/alarm/cron_alarm.cpp index 3631663ea753acfc74bb8f4b1b3ffed55c35bf96..fc6ae3fd211fb063594b52da04a9782e5eea2546 100644 --- a/modules/timer/cron_timer.cpp +++ b/modules/alarm/cron_alarm.cpp @@ -1,34 +1,33 @@ -#include "cron_timer.h" +#include "cron_alarm.h" #include #include #include -#include #include #include "3rd-party/ccronexpr.h" namespace tbox { -namespace timer { +namespace alarm { -CronTimer::CronTimer(event::Loop *loop) : - Timer(loop), +CronAlarm::CronAlarm(event::Loop *loop) : + Alarm(loop), sp_cron_expr_(malloc(sizeof(cron_expr))) { TBOX_ASSERT(sp_cron_expr_ != nullptr); memset(sp_cron_expr_, 0, sizeof(cron_expr)); } -CronTimer::~CronTimer() +CronAlarm::~CronAlarm() { free(sp_cron_expr_); } -bool CronTimer::initialize(const std::string &cron_expr_str) +bool CronAlarm::initialize(const std::string &cron_expr_str) { if (state_ == State::kRunning) { - LogWarn("timer is running state, disable first"); + LogWarn("alarm is running state, disable first"); return false; } @@ -46,7 +45,7 @@ bool CronTimer::initialize(const std::string &cron_expr_str) return true; } -int CronTimer::calculateWaitSeconds(uint32_t curr_local_ts) { +int CronAlarm::calculateWaitSeconds(uint32_t curr_local_ts) { auto next_local_ts = cron_next(static_cast(sp_cron_expr_), curr_local_ts); return next_local_ts - curr_local_ts; } diff --git a/modules/timer/cron_timer.h b/modules/alarm/cron_alarm.h similarity index 73% rename from modules/timer/cron_timer.h rename to modules/alarm/cron_alarm.h index 2652487d1b5105e37fa6833db2ba10210db3504d..9c1e90b4178a65f3a8d1499a9fcb9fb83f6fb29d 100644 --- a/modules/timer/cron_timer.h +++ b/modules/alarm/cron_alarm.h @@ -1,17 +1,17 @@ -#ifndef TBOX_TIMER_CRON_TIMER_H -#define TBOX_TIMER_CRON_TIMER_H +#ifndef TBOX_ALARM_CRON_ALARM_H +#define TBOX_ALARM_CRON_ALARM_H #include -#include "timer.h" +#include "alarm.h" namespace tbox { -namespace timer { +namespace alarm { /* - * @brief The linux cron timer. + * @brief The linux cron alarm. * - * CronTimer allow the user to make plans by linux cron expression. + * CronAlarm allow the user to make plans by linux cron expression. * Linux-cron tab expression * * * * * * - - - - - - @@ -28,18 +28,18 @@ namespace timer { * * Loop *loop = Loop::New(); * - * CronTimer tmr(loop); + * CronAlarm tmr(loop); * tmr.initialize("18 28 14 * * *"); // every day at 14:28:18 * tmr.setCallback([] { std::cout << "timeout" << std::endl; }); * tmr.enable(); * * loop->runLoop(); */ -class CronTimer : public Timer +class CronAlarm : public Alarm { public: - explicit CronTimer(event::Loop *wp_loop); - virtual ~CronTimer(); + explicit CronAlarm(event::Loop *wp_loop); + virtual ~CronAlarm(); bool initialize(const std::string &cron_expr_str); @@ -53,4 +53,4 @@ class CronTimer : public Timer } } -#endif //TBOX_TIMER_CRON_TIMER_H +#endif //TBOX_ALARM_CRON_ALARM_H diff --git a/modules/timer/oneshot_timer.cpp b/modules/alarm/oneshot_alarm.cpp similarity index 73% rename from modules/timer/oneshot_timer.cpp rename to modules/alarm/oneshot_alarm.cpp index 3e1fb377f45e9d173f37fb606d373a38a709efa7..adab44a309cd3b0e87185cd80041d37f79a05b8e 100644 --- a/modules/timer/oneshot_timer.cpp +++ b/modules/alarm/oneshot_alarm.cpp @@ -1,21 +1,20 @@ -#include "oneshot_timer.h" +#include "oneshot_alarm.h" #include #include #include -#include namespace tbox { -namespace timer { +namespace alarm { namespace { constexpr auto kSecondsOfDay = 60 * 60 * 24; } -bool OneshotTimer::initialize(int seconds_of_day) { +bool OneshotAlarm::initialize(int seconds_of_day) { if (state_ == State::kRunning) { - LogWarn("timer is running state, disable first"); + LogWarn("alarm is running state, disable first"); return false; } @@ -29,7 +28,7 @@ bool OneshotTimer::initialize(int seconds_of_day) { return true; } -int OneshotTimer::calculateWaitSeconds(uint32_t curr_local_ts) { +int OneshotAlarm::calculateWaitSeconds(uint32_t curr_local_ts) { int curr_seconds = curr_local_ts % kSecondsOfDay; #if 1 @@ -42,7 +41,7 @@ int OneshotTimer::calculateWaitSeconds(uint32_t curr_local_ts) { return wait_seconds; } -void OneshotTimer::onTimeExpired() { +void OneshotAlarm::onTimeExpired() { state_ = State::kInited; ++cb_level_; diff --git a/modules/timer/oneshot_timer.h b/modules/alarm/oneshot_alarm.h similarity index 69% rename from modules/timer/oneshot_timer.h rename to modules/alarm/oneshot_alarm.h index 010cc3861de5ffb8076e374c576e5107852c5071..6b8d736b7f5dbba70f24bbbee4ce9bf0c2c8d0f9 100644 --- a/modules/timer/oneshot_timer.h +++ b/modules/alarm/oneshot_alarm.h @@ -1,15 +1,15 @@ -#ifndef TBOX_TIMER_ONESHOT_TIMER_H_20221023 -#define TBOX_TIMER_ONESHOT_TIMER_H_20221023 +#ifndef TBOX_ALARM_ONESHOT_ALARM_H_20221023 +#define TBOX_ALARM_ONESHOT_ALARM_H_20221023 -#include "timer.h" +#include "alarm.h" namespace tbox { -namespace timer { +namespace alarm { /* - * @brief The oneshot timer. + * @brief The oneshot alarm. * - * OneshotTimer allow the user to make plans which execute at today or tomorrow + * OneshotAlarm allow the user to make plans which execute at today or tomorrow * If the specified time is later than the local time, the system will execute today. * Otherwise it will be executed tomorrow. * @@ -17,17 +17,17 @@ namespace timer { * * Loop *loop = Loop::New(); * - * OneshotTimer tmr(loop); + * OneshotAlarm tmr(loop); * tmr.initialize(30600); // at 08:30 * tmr.setCallback([] { std::cout << "time is up" << endl;}); * tmr.enable(); * * loop->runLoop(); */ -class OneshotTimer : public Timer +class OneshotAlarm : public Alarm { public: - using Timer::Timer; + using Alarm::Alarm; /** * \brief 初始化 @@ -48,4 +48,4 @@ class OneshotTimer : public Timer } } -#endif //TBOX_TIMER_ONESHOT_TIMER_H_20221023 +#endif //TBOX_ALARM_ONESHOT_ALARM_H_20221023 diff --git a/modules/timer/weekly_timer.cpp b/modules/alarm/weekly_alarm.cpp similarity index 82% rename from modules/timer/weekly_timer.cpp rename to modules/alarm/weekly_alarm.cpp index bb76a414b61e858c8217ca70ec70a8f545679be2..e884ce6816580c7e49a7dff5f46d32ac579aeca8 100644 --- a/modules/timer/weekly_timer.cpp +++ b/modules/alarm/weekly_alarm.cpp @@ -1,22 +1,21 @@ -#include "weekly_timer.h" +#include "weekly_alarm.h" #include #include #include -#include namespace tbox { -namespace timer { +namespace alarm { namespace { constexpr auto kSecondsOfDay = 60 * 60 * 24; constexpr auto kSecondsOfWeek = kSecondsOfDay * 7; } -bool WeeklyTimer::initialize(int seconds_of_day, const std::string &week_mask) { +bool WeeklyAlarm::initialize(int seconds_of_day, const std::string &week_mask) { if (state_ == State::kRunning) { - LogWarn("timer is running state, disable first"); + LogWarn("alarm is running state, disable first"); return false; } @@ -41,7 +40,7 @@ bool WeeklyTimer::initialize(int seconds_of_day, const std::string &week_mask) { return true; } -int WeeklyTimer::calculateWaitSeconds(uint32_t curr_local_ts) { +int WeeklyAlarm::calculateWaitSeconds(uint32_t curr_local_ts) { int curr_week = (((curr_local_ts % kSecondsOfWeek) / kSecondsOfDay) + 4) % 7; int curr_seconds = curr_local_ts % kSecondsOfDay; diff --git a/modules/timer/weekly_timer.h b/modules/alarm/weekly_alarm.h similarity index 76% rename from modules/timer/weekly_timer.h rename to modules/alarm/weekly_alarm.h index 86cd8ca098d860a9d28b688cd866145b544e2ef8..6de34ace1fb4ab5b09607c3f8a86b8187289f783 100644 --- a/modules/timer/weekly_timer.h +++ b/modules/alarm/weekly_alarm.h @@ -1,23 +1,23 @@ -#ifndef TBOX_TIMER_WEEKLY_TIMER_H_20221019 -#define TBOX_TIMER_WEEKLY_TIMER_H_20221019 +#ifndef TBOX_ALARM_WEEKLY_ALARM_H_20221019 +#define TBOX_ALARM_WEEKLY_ALARM_H_20221019 #include -#include "timer.h" +#include "alarm.h" namespace tbox { -namespace timer { +namespace alarm { /* - * @brief The weekly timer. + * @brief The weekly alarm. * - * WeeklyTimer allow the user to make plans weekly. + * WeeklyAlarm allow the user to make plans weekly. * * code example: * * Loop *loop = Loop::New(); * - * WeeklyTimer tmr(loop); + * WeeklyAlarm tmr(loop); * tmr.initialize(30600, "1111111"); // everyday at 08:30 * tmr.setCallback([] { std::cout << "time is up" << endl;}); * tmr.enable(); @@ -25,10 +25,10 @@ namespace timer { * loop->runLoop(); */ -class WeeklyTimer : public Timer +class WeeklyAlarm : public Alarm { public: - using Timer::Timer; + using Alarm::Alarm; /** * \brief 初始化 @@ -53,4 +53,4 @@ class WeeklyTimer : public Timer } } -#endif //TBOX_TIMER_WEEKLY_TIMER_H_20221019 +#endif //TBOX_ALARM_WEEKLY_ALARM_H_20221019 diff --git a/modules/timer/workday_timer.cpp b/modules/alarm/workday_alarm.cpp similarity index 79% rename from modules/timer/workday_timer.cpp rename to modules/alarm/workday_alarm.cpp index 4fddf91351a199a14c87430a670bf22969a5e6b0..3f043a8d2dd713b9006d876e868ad1d4a6665dda 100644 --- a/modules/timer/workday_timer.cpp +++ b/modules/alarm/workday_alarm.cpp @@ -1,24 +1,23 @@ -#include "workday_timer.h" +#include "workday_alarm.h" #include #include #include -#include #include "workday_calendar.h" namespace tbox { -namespace timer { +namespace alarm { namespace { constexpr auto kSecondsOfDay = 60 * 60 * 24; constexpr auto kSecondsOfWeek = kSecondsOfDay * 7; } -bool WorkdayTimer::initialize(int seconds_of_day, WorkdayCalendar *wp_calendar, bool workday) { +bool WorkdayAlarm::initialize(int seconds_of_day, WorkdayCalendar *wp_calendar, bool workday) { if (state_ == State::kRunning) { - LogWarn("timer is running state, disable first"); + LogWarn("alarm is running state, disable first"); return false; } @@ -40,7 +39,7 @@ bool WorkdayTimer::initialize(int seconds_of_day, WorkdayCalendar *wp_calendar, return true; } -int WorkdayTimer::calculateWaitSeconds(uint32_t curr_local_ts) { +int WorkdayAlarm::calculateWaitSeconds(uint32_t curr_local_ts) { int curr_days = curr_local_ts / kSecondsOfDay; int curr_seconds = curr_local_ts % kSecondsOfDay; @@ -58,12 +57,12 @@ int WorkdayTimer::calculateWaitSeconds(uint32_t curr_local_ts) { return -1; } -bool WorkdayTimer::onEnable() { +bool WorkdayAlarm::onEnable() { wp_calendar_->subscribe(this); return true; } -bool WorkdayTimer::onDisable() { +bool WorkdayAlarm::onDisable() { wp_calendar_->unsubscribe(this); return true; } diff --git a/modules/timer/workday_timer.h b/modules/alarm/workday_alarm.h similarity index 78% rename from modules/timer/workday_timer.h rename to modules/alarm/workday_alarm.h index 6d9db328b61de887f76d3721f06227e931b7ece4..709e38a13931ab8135954aa40cf5a9b420d3865d 100644 --- a/modules/timer/workday_timer.h +++ b/modules/alarm/workday_alarm.h @@ -1,19 +1,19 @@ -#ifndef TBOX_TIMER_WORKDAY_TIMER_H_20221024 -#define TBOX_TIMER_WORKDAY_TIMER_H_20221024 +#ifndef TBOX_ALARM_WORKDAY_ALARM_H_20221024 +#define TBOX_ALARM_WORKDAY_ALARM_H_20221024 -#include "timer.h" +#include "alarm.h" #include "workday_calendar.h" namespace tbox { -namespace timer { +namespace alarm { /** * \brief 工作日、节假日定时器 */ -class WorkdayTimer : public Timer +class WorkdayAlarm : public Alarm { public: - using Timer::Timer; + using Alarm::Alarm; /** * \brief 初始化 @@ -26,7 +26,7 @@ class WorkdayTimer : public Timer * \return true 成功 * false 失败 * - * \warnning 必须要确保 wp_calendar 指向的日历对象的生命期比本 WorkdayTimer 长 + * \warnning 必须要确保 wp_calendar 指向的日历对象的生命期比本 WorkdayAlarm 长 * 否则会出异常 */ bool initialize(int seconds_of_day, WorkdayCalendar *wp_calendar, bool workday); @@ -45,4 +45,4 @@ class WorkdayTimer : public Timer } } -#endif //TBOX_TIMER_WORKDAY_TIMER_H_20221024 +#endif //TBOX_ALARM_WORKDAY_ALARM_H_20221024 diff --git a/modules/timer/workday_calendar.cpp b/modules/alarm/workday_calendar.cpp similarity index 55% rename from modules/timer/workday_calendar.cpp rename to modules/alarm/workday_calendar.cpp index 565d83c82839e35208d4eb16be16eb7e7bd0d3f5..99127735f16cd718022bc34927f776548142dc1d 100644 --- a/modules/timer/workday_calendar.cpp +++ b/modules/alarm/workday_calendar.cpp @@ -3,32 +3,32 @@ #include #include -#include "timer.h" +#include "alarm.h" namespace tbox { -namespace timer { +namespace alarm { void WorkdayCalendar::updateSpecialDays(const std::map &special_days) { special_days_ = special_days; - for (auto timer : watch_timers_) - timer->refresh(); + for (auto alarm : watch_alarms_) + alarm->refresh(); } void WorkdayCalendar::updateWeekMask(uint8_t week_mask) { week_mask_ = week_mask; - for (auto timer : watch_timers_) - timer->refresh(); + for (auto alarm : watch_alarms_) + alarm->refresh(); } -void WorkdayCalendar::subscribe(Timer *timer) { - TBOX_ASSERT(timer != nullptr); - watch_timers_.push_back(timer); +void WorkdayCalendar::subscribe(Alarm *alarm) { + TBOX_ASSERT(alarm != nullptr); + watch_alarms_.push_back(alarm); } -void WorkdayCalendar::unsubscribe(Timer *timer) { - TBOX_ASSERT(timer != nullptr); - auto iter = std::remove(watch_timers_.begin(), watch_timers_.end(), timer); - watch_timers_.erase(iter, watch_timers_.end()); +void WorkdayCalendar::unsubscribe(Alarm *alarm) { + TBOX_ASSERT(alarm != nullptr); + auto iter = std::remove(watch_alarms_.begin(), watch_alarms_.end(), alarm); + watch_alarms_.erase(iter, watch_alarms_.end()); } bool WorkdayCalendar::isWorkay(int day_index) const { diff --git a/modules/timer/workday_calendar.h b/modules/alarm/workday_calendar.h similarity index 79% rename from modules/timer/workday_calendar.h rename to modules/alarm/workday_calendar.h index f9cc033766c8c32bdea8bb11d6c86f783280d1ea..5f7432dafff6e18dbb01ed440ab2717658ecf300 100644 --- a/modules/timer/workday_calendar.h +++ b/modules/alarm/workday_calendar.h @@ -1,19 +1,19 @@ -#ifndef TBOX_TIMER_WORKDAY_CALENDAR_H_20221024 -#define TBOX_TIMER_WORKDAY_CALENDAR_H_20221024 +#ifndef TBOX_ALARM_WORKDAY_CALENDAR_H_20221024 +#define TBOX_ALARM_WORKDAY_CALENDAR_H_20221024 #include #include #include namespace tbox { -namespace timer { +namespace alarm { -class Timer; +class Alarm; /** * \brief 工作日的日历 * - * 它用于向 WorkdayTimer 提供指定日期是否为工作日查询的功能 + * 它用于向 WorkdayAlarm 提供指定日期是否为工作日查询的功能 */ class WorkdayCalendar { public: @@ -34,8 +34,8 @@ class WorkdayCalendar { */ void updateWeekMask(uint8_t week_mask); - void subscribe(Timer *timer); - void unsubscribe(Timer *timer); + void subscribe(Alarm *alarm); + void unsubscribe(Alarm *alarm); /** * \brief 查询指定日期是否为工作日 @@ -50,10 +50,10 @@ class WorkdayCalendar { private: uint8_t week_mask_ = 0b00111110; //!< 默认周一到周五是工作日 std::map special_days_; //!< 特殊的放假与补班日期 - std::vector watch_timers_; + std::vector watch_alarms_; }; } } -#endif //TBOX_TIMER_WORKDAY_CALENDAR_H_20221024 +#endif //TBOX_ALARM_WORKDAY_CALENDAR_H_20221024 diff --git a/modules/timer/workday_calendar_test.cpp b/modules/alarm/workday_calendar_test.cpp similarity index 98% rename from modules/timer/workday_calendar_test.cpp rename to modules/alarm/workday_calendar_test.cpp index 874711144447c6377a4ace2292d788a8c6aa3177..35f420dd92512f0045fdb8ab3a599a756052b66c 100644 --- a/modules/timer/workday_calendar_test.cpp +++ b/modules/alarm/workday_calendar_test.cpp @@ -2,7 +2,7 @@ #include namespace tbox { -namespace timer { +namespace alarm { //! 模拟 2022年国庆的放假 TEST(WorkdayCalendar, Base) { diff --git a/modules/action/Makefile b/modules/flow/Makefile similarity index 90% rename from modules/action/Makefile rename to modules/flow/Makefile index 78fb12ae9785e635abb18ad69be61680b0562e82..1f4db056cf4f639717167d464e50412fe6e7f3ae 100644 --- a/modules/action/Makefile +++ b/modules/flow/Makefile @@ -1,9 +1,10 @@ -LIB_NAME = action +LIB_NAME = flow LIB_VERSION_X = 0 LIB_VERSION_Y = 0 LIB_VERSION_Z = 1 HEAD_FILES = \ + state_machine.h \ action.h \ event.h \ action_executor.h \ @@ -22,6 +23,7 @@ HEAD_FILES = \ actions/event_action.h \ CPP_SRC_FILES = \ + state_machine.cpp \ action.cpp \ action_executor.cpp \ event_publisher_impl.cpp \ @@ -39,6 +41,7 @@ CPP_SRC_FILES = \ CXXFLAGS := -DLOG_MODULE_ID='"action"' $(CXXFLAGS) TEST_CPP_SRC_FILES = \ + state_machine_test.cpp \ event_publisher_impl_test.cpp \ action_executor_test.cpp \ actions/sleep_action_test.cpp \ @@ -51,7 +54,7 @@ TEST_CPP_SRC_FILES = \ actions/loop_action_test.cpp \ actions/loop_if_action_test.cpp \ -TEST_LDFLAGS := $(LDFLAGS) -ltbox_action -ltbox_event -ltbox_base +TEST_LDFLAGS := $(LDFLAGS) -ltbox_flow -ltbox_event -ltbox_base ENABLE_SHARED_LIB = no include ../../tools/lib_common.mk diff --git a/modules/action/action.cpp b/modules/flow/action.cpp similarity index 99% rename from modules/action/action.cpp rename to modules/flow/action.cpp index 983cb9df446d71d4f6dc31accfb5e3bdc9ffba61..f0c397c0ff2ab8f9fdd65a67389da54bb2e40632 100644 --- a/modules/action/action.cpp +++ b/modules/flow/action.cpp @@ -6,7 +6,7 @@ #include namespace tbox { -namespace action { +namespace flow { Action::Action(event::Loop &loop) : loop_(loop) diff --git a/modules/action/action.h b/modules/flow/action.h similarity index 94% rename from modules/action/action.h rename to modules/flow/action.h index 6fa8de675c6781b4af6f536de484b851bdb43ee1..8e1bce9e2a5075991cb48010f57d18ff8593d4a8 100644 --- a/modules/action/action.h +++ b/modules/flow/action.h @@ -1,5 +1,5 @@ -#ifndef TBOX_ACTION_ACTION_H_20221001 -#define TBOX_ACTION_ACTION_H_20221001 +#ifndef TBOX_FLOW_FLOW_H_20221001 +#define TBOX_FLOW_FLOW_H_20221001 #include #include @@ -9,7 +9,7 @@ #include namespace tbox { -namespace action { +namespace flow { //! 动作类 class Action { @@ -85,4 +85,4 @@ std::string ToString(Action::Result result); } } -#endif //TBOX_ACTION_ACTION_H_20221001 +#endif //TBOX_FLOW_FLOW_H_20221001 diff --git a/modules/action/action_executor.cpp b/modules/flow/action_executor.cpp similarity index 99% rename from modules/action/action_executor.cpp rename to modules/flow/action_executor.cpp index 64ee59882c5197655a697af50253bc765f672f2b..8424e821628957af2ba80f07a32c93803c16534b 100644 --- a/modules/action/action_executor.cpp +++ b/modules/flow/action_executor.cpp @@ -5,7 +5,7 @@ #include "action.h" namespace tbox { -namespace action { +namespace flow { ActionExecutor::ActionExecutor() { } diff --git a/modules/action/action_executor.h b/modules/flow/action_executor.h similarity index 93% rename from modules/action/action_executor.h rename to modules/flow/action_executor.h index 5f86a816b37c1948714ad66c2f807c783bbf0762..e5cf196e97d49f8647324b74cffdf1ae5b1990e8 100644 --- a/modules/action/action_executor.h +++ b/modules/flow/action_executor.h @@ -1,12 +1,12 @@ -#ifndef TBOX_ACTION_EXECUTOR_H_20221112 -#define TBOX_ACTION_EXECUTOR_H_20221112 +#ifndef TBOX_FLOW_EXECUTOR_H_20221112 +#define TBOX_FLOW_EXECUTOR_H_20221112 #include #include #include namespace tbox { -namespace action { +namespace flow { class Action; @@ -83,4 +83,4 @@ class ActionExecutor { } } -#endif //TBOX_ACTION_EXECUTOR_H_20221112 +#endif //TBOX_FLOW_EXECUTOR_H_20221112 diff --git a/modules/action/action_executor_test.cpp b/modules/flow/action_executor_test.cpp similarity index 99% rename from modules/action/action_executor_test.cpp rename to modules/flow/action_executor_test.cpp index 6577d90ef1ded55c82a63b619fbcfa842b81dd95..82b42a90d622a36299ad7cf6063993c9322f1423 100644 --- a/modules/action/action_executor_test.cpp +++ b/modules/flow/action_executor_test.cpp @@ -8,7 +8,7 @@ #include "actions/sleep_action.h" namespace tbox { -namespace action { +namespace flow { TEST(ActionExecutor, OneAction) { auto loop = event::Loop::New(); diff --git a/modules/action/actions/event_action.cpp b/modules/flow/actions/event_action.cpp similarity index 96% rename from modules/action/actions/event_action.cpp rename to modules/flow/actions/event_action.cpp index 5b38a61f03d9ec6d1af2b32d45bd1b414559ec3c..70d8f38ece671debc85a13a232f1b73958e832cd 100644 --- a/modules/action/actions/event_action.cpp +++ b/modules/flow/actions/event_action.cpp @@ -1,7 +1,7 @@ #include "event_action.h" namespace tbox { -namespace action { +namespace flow { EventAction::EventAction(event::Loop &loop, EventPublisher &pub) : Action(loop), pub_(pub) diff --git a/modules/action/actions/event_action.h b/modules/flow/actions/event_action.h similarity index 78% rename from modules/action/actions/event_action.h rename to modules/flow/actions/event_action.h index c1b04936d69dae62e535ea4501e39a0a59e5a18e..07f0f40d01360b73575ade666958659aedc08e16 100644 --- a/modules/action/actions/event_action.h +++ b/modules/flow/actions/event_action.h @@ -1,12 +1,12 @@ -#ifndef TBOX_ACTION_EVENT_ACTION_H_20221105 -#define TBOX_ACTION_EVENT_ACTION_H_20221105 +#ifndef TBOX_FLOW_EVENT_FLOW_H_20221105 +#define TBOX_FLOW_EVENT_FLOW_H_20221105 #include "../action.h" #include "../event_subscriber.h" #include "../event_publisher.h" namespace tbox { -namespace action { +namespace flow { class EventAction : public Action, public EventSubscriber { @@ -28,4 +28,4 @@ class EventAction : public Action, } } -#endif //TBOX_ACTION_EVENT_ACTION_H_20221105 +#endif //TBOX_FLOW_EVENT_FLOW_H_20221105 diff --git a/modules/action/actions/fallback_action.cpp b/modules/flow/actions/fallback_action.cpp similarity index 98% rename from modules/action/actions/fallback_action.cpp rename to modules/flow/actions/fallback_action.cpp index a4c32a2f21700507b92ffea66ee6d74465170101..33e67d234d17570f3958e1a7791bff41f931d661 100644 --- a/modules/action/actions/fallback_action.cpp +++ b/modules/flow/actions/fallback_action.cpp @@ -6,7 +6,7 @@ #include namespace tbox { -namespace action { +namespace flow { using namespace std::placeholders; diff --git a/modules/action/actions/fallback_action.h b/modules/flow/actions/fallback_action.h similarity index 83% rename from modules/action/actions/fallback_action.h rename to modules/flow/actions/fallback_action.h index 9e43759fc061d44816d11cdc81b496a05d1b1fdd..331b1aab3e53048cda30580ddebdefef26833b64 100644 --- a/modules/action/actions/fallback_action.h +++ b/modules/flow/actions/fallback_action.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_FALLBACK_ACTION_H_20221031 -#define TBOX_ACTION_FALLBACK_ACTION_H_20221031 +#ifndef TBOX_FLOW_FALLBACK_FLOW_H_20221031 +#define TBOX_FLOW_FALLBACK_FLOW_H_20221031 #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool FallbackAction(acton_vec) { @@ -46,4 +46,4 @@ class FallbackAction : public Action { } } -#endif //TBOX_ACTION_FALLBACK_ACTION_H_20221031 +#endif //TBOX_FLOW_FALLBACK_FLOW_H_20221031 diff --git a/modules/action/actions/fallback_action_test.cpp b/modules/flow/actions/fallback_action_test.cpp similarity index 99% rename from modules/action/actions/fallback_action_test.cpp rename to modules/flow/actions/fallback_action_test.cpp index 7a0762fda1d1e35b4dcc93a041815f8532d99d6a..2264c92e1f96911fdfaccb1eb1b29eee4f54b479 100644 --- a/modules/action/actions/fallback_action_test.cpp +++ b/modules/flow/actions/fallback_action_test.cpp @@ -7,7 +7,7 @@ #include "sleep_action.h" namespace tbox { -namespace action { +namespace flow { TEST(FallbackAction, AllFail) { auto loop = event::Loop::New(); diff --git a/modules/action/actions/if_else_action.cpp b/modules/flow/actions/if_else_action.cpp similarity index 99% rename from modules/action/actions/if_else_action.cpp rename to modules/flow/actions/if_else_action.cpp index 136eae115dfacf8755f75d3d5c5fe7b297933c7a..01d081f8256868969f1ca8c8060761808e7979c5 100644 --- a/modules/action/actions/if_else_action.cpp +++ b/modules/flow/actions/if_else_action.cpp @@ -5,7 +5,7 @@ #include namespace tbox { -namespace action { +namespace flow { using namespace std::placeholders; diff --git a/modules/action/actions/if_else_action.h b/modules/flow/actions/if_else_action.h similarity index 87% rename from modules/action/actions/if_else_action.h rename to modules/flow/actions/if_else_action.h index 6f6fec0fc740c789afe6663ad5f67fe66546e3ce..4000765bbc644bafc50607cc4f16b97912f36403 100644 --- a/modules/action/actions/if_else_action.h +++ b/modules/flow/actions/if_else_action.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_IF_ELSE_H_20221022 -#define TBOX_ACTION_IF_ELSE_H_20221022 +#ifndef TBOX_FLOW_IF_ELSE_H_20221022 +#define TBOX_FLOW_IF_ELSE_H_20221022 #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool IfElseAction(cond_action, if_action, else_acton) { @@ -45,4 +45,4 @@ class IfElseAction : public Action { } } -#endif //TBOX_ACTION_IF_ELSE_H_20221022 +#endif //TBOX_FLOW_IF_ELSE_H_20221022 diff --git a/modules/action/actions/if_else_action_test.cpp b/modules/flow/actions/if_else_action_test.cpp similarity index 99% rename from modules/action/actions/if_else_action_test.cpp rename to modules/flow/actions/if_else_action_test.cpp index dd7fcb6af7ea7c33613b6a53476bc6333f6ecdea..bbfd7bf0169bd979f86f093e41cdb846677484a4 100644 --- a/modules/action/actions/if_else_action_test.cpp +++ b/modules/flow/actions/if_else_action_test.cpp @@ -6,7 +6,7 @@ #include "nondelay_action.h" namespace tbox { -namespace action { +namespace flow { TEST(IfElseAction, CondSucc) { auto loop = event::Loop::New(); diff --git a/modules/action/actions/invert_action.cpp b/modules/flow/actions/invert_action.cpp similarity index 97% rename from modules/action/actions/invert_action.cpp rename to modules/flow/actions/invert_action.cpp index bdd240cc6975bf963e3a99fe450b215d3279f3cc..38305c46264278f93837264717c63b33d71d5001 100644 --- a/modules/action/actions/invert_action.cpp +++ b/modules/flow/actions/invert_action.cpp @@ -4,7 +4,7 @@ #include namespace tbox { -namespace action { +namespace flow { InvertAction::InvertAction(event::Loop &loop, Action *child) : Action(loop), child_(child) diff --git a/modules/action/actions/invert_action.h b/modules/flow/actions/invert_action.h similarity index 81% rename from modules/action/actions/invert_action.h rename to modules/flow/actions/invert_action.h index cc88969ad3c4866fd19e983914a0544512d6f691..d15ff0b8e918b5187557ce0b6b89ea50d4889e47 100644 --- a/modules/action/actions/invert_action.h +++ b/modules/flow/actions/invert_action.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_INVERT_H_20221022 -#define TBOX_ACTION_INVERT_H_20221022 +#ifndef TBOX_FLOW_INVERT_H_20221022 +#define TBOX_FLOW_INVERT_H_20221022 #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool InvertAction(child) { @@ -34,4 +34,4 @@ class InvertAction : public Action { } } -#endif //TBOX_ACTION_INVERT_H_20221022 +#endif //TBOX_FLOW_INVERT_H_20221022 diff --git a/modules/action/actions/invert_action_test.cpp b/modules/flow/actions/invert_action_test.cpp similarity index 97% rename from modules/action/actions/invert_action_test.cpp rename to modules/flow/actions/invert_action_test.cpp index 53ca326243049852e7eda364bbd0229016958699..222fc0e09b011b3a5b00a3de9a68354905457654 100644 --- a/modules/action/actions/invert_action_test.cpp +++ b/modules/flow/actions/invert_action_test.cpp @@ -6,7 +6,7 @@ #include "nondelay_action.h" namespace tbox { -namespace action { +namespace flow { /** * return !false; diff --git a/modules/action/actions/loop_action.cpp b/modules/flow/actions/loop_action.cpp similarity index 98% rename from modules/action/actions/loop_action.cpp rename to modules/flow/actions/loop_action.cpp index d0d1ff5f230199d03e3487108263822005f31c6b..5716db738ada76acc81f3ef23993c948fccbcdf3 100644 --- a/modules/action/actions/loop_action.cpp +++ b/modules/flow/actions/loop_action.cpp @@ -3,7 +3,7 @@ #include namespace tbox { -namespace action { +namespace flow { LoopAction::LoopAction(event::Loop &loop, Action *child, Mode mode) : Action(loop), diff --git a/modules/action/actions/loop_action.h b/modules/flow/actions/loop_action.h similarity index 82% rename from modules/action/actions/loop_action.h rename to modules/flow/actions/loop_action.h index f979a4d9caa6f55198ba62505805fdc77b24be24..58594b4dbba359121db2783f179355e89de53986 100644 --- a/modules/action/actions/loop_action.h +++ b/modules/flow/actions/loop_action.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_LOOP_ACTION_H_20221017 -#define TBOX_ACTION_LOOP_ACTION_H_20221017 +#ifndef TBOX_FLOW_LOOP_FLOW_H_20221017 +#define TBOX_FLOW_LOOP_FLOW_H_20221017 #include "../action.h" namespace tbox { -namespace action { +namespace flow { class LoopAction : public Action { public: @@ -34,4 +34,4 @@ class LoopAction : public Action { } } -#endif //TBOX_ACTION_LOOP_ACTION_H_20221017 +#endif //TBOX_FLOW_LOOP_FLOW_H_20221017 diff --git a/modules/action/actions/loop_action_test.cpp b/modules/flow/actions/loop_action_test.cpp similarity index 98% rename from modules/action/actions/loop_action_test.cpp rename to modules/flow/actions/loop_action_test.cpp index ccfc27bc8194203e4b7604a392371d951ac6d30b..dccc867fd9e17fa6ecfba6fea6fad5f084daeb6b 100644 --- a/modules/action/actions/loop_action_test.cpp +++ b/modules/flow/actions/loop_action_test.cpp @@ -9,7 +9,7 @@ #include "sequence_action.h" namespace tbox { -namespace action { +namespace flow { /** * int loop_times = 0; diff --git a/modules/action/actions/loop_if_action.cpp b/modules/flow/actions/loop_if_action.cpp similarity index 98% rename from modules/action/actions/loop_if_action.cpp rename to modules/flow/actions/loop_if_action.cpp index 31cfe231368fa86da23c32853435f93c92c06fc0..ca1c474bf86556f77220659ee983dae30c4c2ea2 100644 --- a/modules/action/actions/loop_if_action.cpp +++ b/modules/flow/actions/loop_if_action.cpp @@ -4,7 +4,7 @@ #include namespace tbox { -namespace action { +namespace flow { LoopIfAction::LoopIfAction(event::Loop &loop, Action *cond_action, Action *exec_action) : Action(loop), diff --git a/modules/action/actions/loop_if_action.h b/modules/flow/actions/loop_if_action.h similarity index 83% rename from modules/action/actions/loop_if_action.h rename to modules/flow/actions/loop_if_action.h index 195af578f81b0136fc151e9dbf08c072061f222b..7b89e73c53de5b219cc19d504a399c432b1158f8 100644 --- a/modules/action/actions/loop_if_action.h +++ b/modules/flow/actions/loop_if_action.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_LOOP_IF_ACTION_H_20221108 -#define TBOX_ACTION_LOOP_IF_ACTION_H_20221108 +#ifndef TBOX_FLOW_LOOP_IF_FLOW_H_20221108 +#define TBOX_FLOW_LOOP_IF_FLOW_H_20221108 #include "../action.h" namespace tbox { -namespace action { +namespace flow { class LoopIfAction : public Action { public: @@ -36,4 +36,4 @@ class LoopIfAction : public Action { } } -#endif //TBOX_ACTION_LOOP_IF_ACTION_H_20221108 +#endif //TBOX_FLOW_LOOP_IF_FLOW_H_20221108 diff --git a/modules/action/actions/loop_if_action_test.cpp b/modules/flow/actions/loop_if_action_test.cpp similarity index 98% rename from modules/action/actions/loop_if_action_test.cpp rename to modules/flow/actions/loop_if_action_test.cpp index 638ab8fd6ec4f19280042f252d6166924881f5aa..1893a3dcbf5f7bb403b840181d41d6ee9c6b7d7b 100644 --- a/modules/action/actions/loop_if_action_test.cpp +++ b/modules/flow/actions/loop_if_action_test.cpp @@ -8,7 +8,7 @@ #include "sequence_action.h" namespace tbox { -namespace action { +namespace flow { /** * int remain = 10; diff --git a/modules/action/actions/nondelay_action.cpp b/modules/flow/actions/nondelay_action.cpp similarity index 94% rename from modules/action/actions/nondelay_action.cpp rename to modules/flow/actions/nondelay_action.cpp index a44cf2a8ed8f383179028c187088f44f193f0e8a..5a4ddb70ae9b3f8b9c37930c274bbb348e222b18 100644 --- a/modules/action/actions/nondelay_action.cpp +++ b/modules/flow/actions/nondelay_action.cpp @@ -3,7 +3,7 @@ #include namespace tbox { -namespace action { +namespace flow { NondelayAction::NondelayAction(event::Loop &loop, const Func &func) : Action(loop), func_(func) diff --git a/modules/action/actions/nondelay_action.h b/modules/flow/actions/nondelay_action.h similarity index 68% rename from modules/action/actions/nondelay_action.h rename to modules/flow/actions/nondelay_action.h index bdfd91a5d9275c72309f700591948e5ae28620cc..2d04423d7d969d6ddbaf250a1becc214152ae149 100644 --- a/modules/action/actions/nondelay_action.h +++ b/modules/flow/actions/nondelay_action.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_NONDELAY_ACTION_H_20221003 -#define TBOX_ACTION_NONDELAY_ACTION_H_20221003 +#ifndef TBOX_FLOW_NONDELAY_FLOW_H_20221003 +#define TBOX_FLOW_NONDELAY_FLOW_H_20221003 #include "../action.h" namespace tbox { -namespace action { +namespace flow { class NondelayAction : public Action { public: @@ -23,4 +23,4 @@ class NondelayAction : public Action { } } -#endif //TBOX_ACTION_NONDELAY_ACTION_H_20221003 +#endif //TBOX_FLOW_NONDELAY_FLOW_H_20221003 diff --git a/modules/action/actions/nondelay_action_test.cpp b/modules/flow/actions/nondelay_action_test.cpp similarity index 98% rename from modules/action/actions/nondelay_action_test.cpp rename to modules/flow/actions/nondelay_action_test.cpp index 60443d214a6d020c4e3e2859e8a0314235aac898..8c8efa831115234b275ce301ca9a944ed4412f51 100644 --- a/modules/action/actions/nondelay_action_test.cpp +++ b/modules/flow/actions/nondelay_action_test.cpp @@ -5,7 +5,7 @@ #include "nondelay_action.h" namespace tbox { -namespace action { +namespace flow { TEST(NonDelayAction, True) { auto loop = event::Loop::New(); diff --git a/modules/action/actions/parallel_action.cpp b/modules/flow/actions/parallel_action.cpp similarity index 98% rename from modules/action/actions/parallel_action.cpp rename to modules/flow/actions/parallel_action.cpp index 9ca866dc011aa3f9a05283a9ca49b3a622abedbb..f071d6656a97dbaa9df6d96597683405576dee43 100644 --- a/modules/action/actions/parallel_action.cpp +++ b/modules/flow/actions/parallel_action.cpp @@ -5,7 +5,7 @@ #include namespace tbox { -namespace action { +namespace flow { using namespace std::placeholders; diff --git a/modules/action/actions/parallel_action.h b/modules/flow/actions/parallel_action.h similarity index 82% rename from modules/action/actions/parallel_action.h rename to modules/flow/actions/parallel_action.h index 12355e0e5d4be58c8bb710eae92988a1389dd5b7..aa8542ccffbb12820476eb55c9759239e002bf18 100644 --- a/modules/action/actions/parallel_action.h +++ b/modules/flow/actions/parallel_action.h @@ -1,5 +1,5 @@ -#ifndef TBOX_ACTION_PARALLEL_ACTION_H_20221005 -#define TBOX_ACTION_PARALLEL_ACTION_H_20221005 +#ifndef TBOX_FLOW_PARALLEL_FLOW_H_20221005 +#define TBOX_FLOW_PARALLEL_FLOW_H_20221005 #include "../action.h" @@ -7,7 +7,7 @@ #include namespace tbox { -namespace action { +namespace flow { class ParallelAction : public Action { public: @@ -40,4 +40,4 @@ class ParallelAction : public Action { } } -#endif //TBOX_ACTION_PARALLEL_ACTION_H_20221005 +#endif //TBOX_FLOW_PARALLEL_FLOW_H_20221005 diff --git a/modules/action/actions/parallel_action_test.cpp b/modules/flow/actions/parallel_action_test.cpp similarity index 99% rename from modules/action/actions/parallel_action_test.cpp rename to modules/flow/actions/parallel_action_test.cpp index e6eb56eef322503c00d5308c8768efe4f8ea4144..c338dbf9788340849fc5caf4da6ca9d789c201ce 100644 --- a/modules/action/actions/parallel_action_test.cpp +++ b/modules/flow/actions/parallel_action_test.cpp @@ -7,7 +7,7 @@ #include "sleep_action.h" namespace tbox { -namespace action { +namespace flow { TEST(ParallelAction, TwoSleepAction) { auto loop = event::Loop::New(); diff --git a/modules/action/actions/sequence_action.cpp b/modules/flow/actions/sequence_action.cpp similarity index 98% rename from modules/action/actions/sequence_action.cpp rename to modules/flow/actions/sequence_action.cpp index 704601549089750e64dd5e521745d7d2526e1ae7..99aabe4bd08b943a68f8030b49af6ce1a59c90d5 100644 --- a/modules/action/actions/sequence_action.cpp +++ b/modules/flow/actions/sequence_action.cpp @@ -6,7 +6,7 @@ #include namespace tbox { -namespace action { +namespace flow { using namespace std::placeholders; diff --git a/modules/action/actions/sequence_action.h b/modules/flow/actions/sequence_action.h similarity index 83% rename from modules/action/actions/sequence_action.h rename to modules/flow/actions/sequence_action.h index 537a1b8c6c2a687629f3a0c386e39bd6f29f0c0a..1774f49ca15dbfd4361f8724680eec01269137ad 100644 --- a/modules/action/actions/sequence_action.h +++ b/modules/flow/actions/sequence_action.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_SEQUENCE_ACTION_H_20221002 -#define TBOX_ACTION_SEQUENCE_ACTION_H_20221002 +#ifndef TBOX_FLOW_SEQUENCE_FLOW_H_20221002 +#define TBOX_FLOW_SEQUENCE_FLOW_H_20221002 #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool SequenceAction(acton_vec) { @@ -46,4 +46,4 @@ class SequenceAction : public Action { } } -#endif //TBOX_ACTION_SEQUENCE_ACTION_H_20221002 +#endif //TBOX_FLOW_SEQUENCE_FLOW_H_20221002 diff --git a/modules/action/actions/sequence_action_test.cpp b/modules/flow/actions/sequence_action_test.cpp similarity index 99% rename from modules/action/actions/sequence_action_test.cpp rename to modules/flow/actions/sequence_action_test.cpp index d58ea01b7d32bde214064e30b26415b11d88f716..028ed70f5680f16bd8fdb96dcc773ee19f5bd5d6 100644 --- a/modules/action/actions/sequence_action_test.cpp +++ b/modules/flow/actions/sequence_action_test.cpp @@ -7,7 +7,7 @@ #include "sleep_action.h" namespace tbox { -namespace action { +namespace flow { TEST(SequenceAction, AllSucc) { auto loop = event::Loop::New(); diff --git a/modules/action/actions/sleep_action.cpp b/modules/flow/actions/sleep_action.cpp similarity index 98% rename from modules/action/actions/sleep_action.cpp rename to modules/flow/actions/sleep_action.cpp index 0a440c47e5882afcca908488386b082abde48355..e17e3d026c504017d631a0dea656227fcb0e239b 100644 --- a/modules/action/actions/sleep_action.cpp +++ b/modules/flow/actions/sleep_action.cpp @@ -5,7 +5,7 @@ #include namespace tbox { -namespace action { +namespace flow { SleepAction::SleepAction(event::Loop &loop, const std::chrono::milliseconds &time_span) : Action(loop), diff --git a/modules/action/actions/sleep_action.h b/modules/flow/actions/sleep_action.h similarity index 84% rename from modules/action/actions/sleep_action.h rename to modules/flow/actions/sleep_action.h index c8b7a6842fd231b8d98e799da02cb8ca9f6e9f61..edf1f645ee467b82df1da61452cffb1ffd34b5e5 100644 --- a/modules/action/actions/sleep_action.h +++ b/modules/flow/actions/sleep_action.h @@ -1,11 +1,11 @@ -#ifndef TBOX_ACTION_SLEEP_ACTION_H_20221002 -#define TBOX_ACTION_SLEEP_ACTION_H_20221002 +#ifndef TBOX_FLOW_SLEEP_FLOW_H_20221002 +#define TBOX_FLOW_SLEEP_FLOW_H_20221002 #include "../action.h" #include namespace tbox { -namespace action { +namespace flow { class SleepAction : public Action { public: @@ -37,4 +37,4 @@ class SleepAction : public Action { } } -#endif //TBOX_ACTION_SLEEP_ACTION_H_20221002 +#endif //TBOX_FLOW_SLEEP_FLOW_H_20221002 diff --git a/modules/action/actions/sleep_action_test.cpp b/modules/flow/actions/sleep_action_test.cpp similarity index 99% rename from modules/action/actions/sleep_action_test.cpp rename to modules/flow/actions/sleep_action_test.cpp index 150c164e0e4b0eb25ba6c39864b5f4d5ffc0c803..d4ddbf8bd3018079749277224d856e517a981fce 100644 --- a/modules/action/actions/sleep_action_test.cpp +++ b/modules/flow/actions/sleep_action_test.cpp @@ -8,7 +8,7 @@ #include "sequence_action.h" namespace tbox { -namespace action { +namespace flow { //! 检查任务有无执行 TEST(SleepAction, Basic) { diff --git a/modules/action/event.h b/modules/flow/event.h similarity index 69% rename from modules/action/event.h rename to modules/flow/event.h index 8518ebf37b3427c2688d672326c6868ff729f3d9..c33df6a40a2fd9e8a16b610ceae83f0d6ad6283e 100644 --- a/modules/action/event.h +++ b/modules/flow/event.h @@ -1,8 +1,8 @@ -#ifndef TBOX_ACTION_EVENT_H_20221022 -#define TBOX_ACTION_EVENT_H_20221022 +#ifndef TBOX_FLOW_EVENT_H_20221022 +#define TBOX_FLOW_EVENT_H_20221022 namespace tbox { -namespace action { +namespace flow { struct Event { using ID = int; @@ -22,4 +22,4 @@ struct Event { } } -#endif //TBOX_ACTION_EVENT_H_20221022 +#endif //TBOX_FLOW_EVENT_H_20221022 diff --git a/modules/action/event_publisher.h b/modules/flow/event_publisher.h similarity index 66% rename from modules/action/event_publisher.h rename to modules/flow/event_publisher.h index 89ba9ea54ae33340aaf5fece3f8d9df7dec183f1..db131248721dd7162527524d40c1f011554ca344 100644 --- a/modules/action/event_publisher.h +++ b/modules/flow/event_publisher.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_EVENT_PUBLISHER_H_20221001 -#define TBOX_ACTION_EVENT_PUBLISHER_H_20221001 +#ifndef TBOX_FLOW_EVENT_PUBLISHER_H_20221001 +#define TBOX_FLOW_EVENT_PUBLISHER_H_20221001 #include "event.h" namespace tbox { -namespace action { +namespace flow { class EventSubscriber; @@ -21,4 +21,4 @@ class EventPublisher { } } -#endif //TBOX_ACTION_EVENT_PUBLISHER_H_20221001 +#endif //TBOX_FLOW_EVENT_PUBLISHER_H_20221001 diff --git a/modules/action/event_publisher_impl.cpp b/modules/flow/event_publisher_impl.cpp similarity index 98% rename from modules/action/event_publisher_impl.cpp rename to modules/flow/event_publisher_impl.cpp index b17e1c7664b0ca62c87b270b7ddb89aa6992930b..169d1ce47135355a196baf91491ede78b93b25b6 100644 --- a/modules/action/event_publisher_impl.cpp +++ b/modules/flow/event_publisher_impl.cpp @@ -5,7 +5,7 @@ #include namespace tbox { -namespace action { +namespace flow { void EventPublisherImpl::subscribe(EventSubscriber *subscriber) { auto iter_end = subscriber_vec_.end(); diff --git a/modules/action/event_publisher_impl.h b/modules/flow/event_publisher_impl.h similarity index 72% rename from modules/action/event_publisher_impl.h rename to modules/flow/event_publisher_impl.h index c1aab2186ac2f7bf1c3d8d18f0fab0e83bdd25f1..a2125191c5c72e8cfb5b5c8194040bfc701ccb93 100644 --- a/modules/action/event_publisher_impl.h +++ b/modules/flow/event_publisher_impl.h @@ -1,11 +1,11 @@ -#ifndef TBOX_ACTION_EVENT_PUBLISHER_H_20221002 -#define TBOX_ACTION_EVENT_PUBLISHER_H_20221002 +#ifndef TBOX_FLOW_EVENT_PUBLISHER_H_20221002 +#define TBOX_FLOW_EVENT_PUBLISHER_H_20221002 #include #include "event_publisher.h" namespace tbox { -namespace action { +namespace flow { class EventPublisherImpl : public EventPublisher { public: @@ -23,4 +23,4 @@ class EventPublisherImpl : public EventPublisher { } } -#endif //TBOX_ACTION_EVENT_PUBLISHER_H_20221002 +#endif //TBOX_FLOW_EVENT_PUBLISHER_H_20221002 diff --git a/modules/action/event_publisher_impl_test.cpp b/modules/flow/event_publisher_impl_test.cpp similarity index 100% rename from modules/action/event_publisher_impl_test.cpp rename to modules/flow/event_publisher_impl_test.cpp diff --git a/modules/action/event_subscriber.h b/modules/flow/event_subscriber.h similarity index 51% rename from modules/action/event_subscriber.h rename to modules/flow/event_subscriber.h index 78ecdf5aafc37cafb440ec9cae35cd08de055528..ad74f7aa3fe9bc0334643276d1486d2cd730f79d 100644 --- a/modules/action/event_subscriber.h +++ b/modules/flow/event_subscriber.h @@ -1,10 +1,10 @@ -#ifndef TBOX_ACTION_EVENT_SUBSCRIBER_H_20221001 -#define TBOX_ACTION_EVENT_SUBSCRIBER_H_20221001 +#ifndef TBOX_FLOW_EVENT_SUBSCRIBER_H_20221001 +#define TBOX_FLOW_EVENT_SUBSCRIBER_H_20221001 #include "event.h" namespace tbox { -namespace action { +namespace flow { class EventSubscriber { public: @@ -17,4 +17,4 @@ class EventSubscriber { } } -#endif //TBOX_ACTION_EVENT_SUBSCRIBER_H_20221001 +#endif //TBOX_FLOW_EVENT_SUBSCRIBER_H_20221001 diff --git a/modules/util/state_machine.cpp b/modules/flow/state_machine.cpp similarity index 99% rename from modules/util/state_machine.cpp rename to modules/flow/state_machine.cpp index c64a453f7fd9e15aff5f18efaeafe587ec45b79d..5c39d2eab45cce6b55c9b95cda7a0279af94205b 100644 --- a/modules/util/state_machine.cpp +++ b/modules/flow/state_machine.cpp @@ -9,7 +9,7 @@ #include namespace tbox { -namespace util { +namespace flow { using namespace std; diff --git a/modules/util/state_machine.h b/modules/flow/state_machine.h similarity index 91% rename from modules/util/state_machine.h rename to modules/flow/state_machine.h index 383a91d40b267f518fe7d8c6bf86baa776613a89..ce1cf3e90231ab5a7297f447141c6c4fab24db99 100644 --- a/modules/util/state_machine.h +++ b/modules/flow/state_machine.h @@ -1,10 +1,11 @@ -#ifndef TBOX_STATE_MACHINE_H_20220320 -#define TBOX_STATE_MACHINE_H_20220320 +#ifndef TBOX_FLOW_STATE_MACHINE_H_20220320 +#define TBOX_FLOW_STATE_MACHINE_H_20220320 #include +#include "event.h" namespace tbox { -namespace util { +namespace flow { //! HFSM,多层级有限状态机 class StateMachine { @@ -15,19 +16,6 @@ class StateMachine { //! StateID < 0 表示无效状态 using EventID = int; //! EventID = 0 表示任意事件,仅在 addRoute(), addEvent() 时使用 - struct Event { - EventID id = 0; - void *extra = nullptr; - - Event() { } - - template - Event(ET e) : id(static_cast(e)) { } - - template - Event(ET e, DT *p) : id(static_cast(e)), extra(p) { } - }; - //! 动作执行函数 using ActionFunc = std::function; //! 条件判定函数,返回true表示条件成立,false表示条件不成立 @@ -162,4 +150,4 @@ class StateMachine { } } -#endif //TBOX_STATE_MACHINE_H_20220320 +#endif //TBOX_FLOW_STATE_MACHINE_H_20220320 diff --git a/modules/util/state_machine_test.cpp b/modules/flow/state_machine_test.cpp similarity index 47% rename from modules/util/state_machine_test.cpp rename to modules/flow/state_machine_test.cpp index e106e9c7c48d8d4216827cf78281753276dfe6bb..f8e877f7cc63b44b069ec9a4bff7c626d27e32cc 100644 --- a/modules/util/state_machine_test.cpp +++ b/modules/flow/state_machine_test.cpp @@ -5,11 +5,11 @@ using namespace std; namespace tbox { -namespace util { +namespace flow { using SM = StateMachine; -enum State { +enum StateId { STATE_TERM = 0, STATE_A, STATE_B, @@ -17,7 +17,7 @@ enum State { STATE_D, }; -enum Event { +enum EventId { EVENT_ANY = 0, EVENT_1, EVENT_2, @@ -72,8 +72,8 @@ TEST(StateMachine, RouteWithGuard) sm.newState(STATE_C, nullptr, nullptr); bool condition = false; - sm.addRoute(STATE_A, EVENT_1, STATE_B, [&](SM::Event){ return !condition; }, nullptr); - sm.addRoute(STATE_A, EVENT_1, STATE_C, [&](SM::Event){ return condition; }, nullptr); + sm.addRoute(STATE_A, EVENT_1, STATE_B, [&](Event){ return !condition; }, nullptr); + sm.addRoute(STATE_A, EVENT_1, STATE_C, [&](Event){ return condition; }, nullptr); sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, nullptr); sm.addRoute(STATE_C, EVENT_2, STATE_A, nullptr, nullptr); sm.setInitState(STATE_A); @@ -106,8 +106,8 @@ TEST(StateMachine, StateWithEnterAndExitAction) int b_enter_counter = 0; int b_exit_counter = 0; - sm.newState(STATE_A, [&](SM::Event){ ++a_enter_counter; }, [&](SM::Event){ ++a_exit_counter; }); - sm.newState(STATE_B, [&](SM::Event){ ++b_enter_counter; }, [&](SM::Event){ ++b_exit_counter; }); + sm.newState(STATE_A, [&](Event){ ++a_enter_counter; }, [&](Event){ ++a_exit_counter; }); + sm.newState(STATE_B, [&](Event){ ++b_enter_counter; }, [&](Event){ ++b_exit_counter; }); sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, nullptr); sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, nullptr); @@ -137,8 +137,8 @@ TEST(StateMachine, EventWithAction) int count_1 = 0; int count_2 = 0; - sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, [&](SM::Event){ ++count_1; }); - sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, [&](SM::Event){ ++count_2; }); + sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, [&](Event){ ++count_1; }); + sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, [&](Event){ ++count_2; }); sm.setInitState(STATE_A); sm.start(); @@ -178,8 +178,8 @@ TEST(StateMachine, Restart) int b_enter_counter = 0; int b_exit_counter = 0; - sm.newState(STATE_A, [&](SM::Event){ ++a_enter_counter; }, [&](SM::Event){ ++a_exit_counter; }); - sm.newState(STATE_B, [&](SM::Event){ ++b_enter_counter; }, [&](SM::Event){ ++b_exit_counter; }); + sm.newState(STATE_A, [&](Event){ ++a_enter_counter; }, [&](Event){ ++a_exit_counter; }); + sm.newState(STATE_B, [&](Event){ ++b_enter_counter; }, [&](Event){ ++b_exit_counter; }); sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, nullptr); sm.setInitState(STATE_A); @@ -198,21 +198,21 @@ TEST(StateMachine, Restart) TEST(StateMachine, EnumClass) { SM sm; - enum class State { k1 = 1, k2 }; - enum class Event { k1 = 1, k2 }; + enum class StateId { k1 = 1, k2 }; + enum class EventId { k1 = 1, k2 }; int enter_counter = 0; int exit_counter = 0; - sm.newState(State::k1, [&](SM::Event){ ++enter_counter; }, [&](SM::Event){ ++exit_counter; }); - sm.newState(State::k2, nullptr, nullptr); + sm.newState(StateId::k1, [&](Event){ ++enter_counter; }, [&](Event){ ++exit_counter; }); + sm.newState(StateId::k2, nullptr, nullptr); - sm.addRoute(State::k1, Event::k1, State::k2, nullptr, nullptr); - sm.addRoute(State::k2, Event::k2, State::k1, nullptr, nullptr); - sm.setInitState(State::k1); + sm.addRoute(StateId::k1, EventId::k1, StateId::k2, nullptr, nullptr); + sm.addRoute(StateId::k2, EventId::k2, StateId::k1, nullptr, nullptr); + sm.setInitState(StateId::k1); sm.start(); - sm.run(Event::k1); - sm.run(Event::k2); + sm.run(EventId::k1); + sm.run(EventId::k2); sm.stop(); EXPECT_EQ(enter_counter, 2); @@ -221,48 +221,48 @@ TEST(StateMachine, EnumClass) TEST(StateMachine, SubSM) { - enum class State { kTerm, kInit, k1, k2 }; - enum class Event { kTerm, k1, k2 }; + enum class StateId { kTerm, kInit, k1, k2 }; + enum class EventId { kTerm, k1, k2 }; SM sm, sub_sm; - sm.newState(State::kInit, nullptr, nullptr); - sm.newState(State::k1, nullptr, nullptr); - sm.newState(State::k2, nullptr, nullptr); + sm.newState(StateId::kInit, nullptr, nullptr); + sm.newState(StateId::k1, nullptr, nullptr); + sm.newState(StateId::k2, nullptr, nullptr); - sm.addRoute(State::kInit, Event::k1, State::k1, nullptr, nullptr); - sm.addRoute(State::kInit, Event::k2, State::k2, nullptr, nullptr); - sm.addRoute(State::k1, Event::k1, State::kTerm, nullptr, nullptr); - sm.addRoute(State::k1, Event::k2, State::k2, nullptr, nullptr); - sm.addRoute(State::k2, Event::k1, State::k1, nullptr, nullptr); - sm.addRoute(State::k2, Event::k2, State::k1, nullptr, nullptr); + sm.addRoute(StateId::kInit, EventId::k1, StateId::k1, nullptr, nullptr); + sm.addRoute(StateId::kInit, EventId::k2, StateId::k2, nullptr, nullptr); + sm.addRoute(StateId::k1, EventId::k1, StateId::kTerm, nullptr, nullptr); + sm.addRoute(StateId::k1, EventId::k2, StateId::k2, nullptr, nullptr); + sm.addRoute(StateId::k2, EventId::k1, StateId::k1, nullptr, nullptr); + sm.addRoute(StateId::k2, EventId::k2, StateId::k1, nullptr, nullptr); - sub_sm.newState(State::kInit, nullptr, nullptr); - sub_sm.newState(State::k1, nullptr, nullptr); - sub_sm.newState(State::k2, nullptr, nullptr); + sub_sm.newState(StateId::kInit, nullptr, nullptr); + sub_sm.newState(StateId::k1, nullptr, nullptr); + sub_sm.newState(StateId::k2, nullptr, nullptr); - sub_sm.addRoute(State::kInit, Event::k1, State::k1, nullptr, nullptr); - sub_sm.addRoute(State::kInit, Event::k2, State::k2, nullptr, nullptr); - sub_sm.addRoute(State::k1, Event::k2, State::k2, nullptr, nullptr); - sub_sm.addRoute(State::k1, Event::k1, State::kTerm, nullptr, nullptr); - sub_sm.addRoute(State::k2, Event::k1, State::k1, nullptr, nullptr); - sub_sm.addRoute(State::k2, Event::k2, State::kTerm, nullptr, nullptr); + sub_sm.addRoute(StateId::kInit, EventId::k1, StateId::k1, nullptr, nullptr); + sub_sm.addRoute(StateId::kInit, EventId::k2, StateId::k2, nullptr, nullptr); + sub_sm.addRoute(StateId::k1, EventId::k2, StateId::k2, nullptr, nullptr); + sub_sm.addRoute(StateId::k1, EventId::k1, StateId::kTerm, nullptr, nullptr); + sub_sm.addRoute(StateId::k2, EventId::k1, StateId::k1, nullptr, nullptr); + sub_sm.addRoute(StateId::k2, EventId::k2, StateId::kTerm, nullptr, nullptr); - sm.setSubStateMachine(State::k1, &sub_sm); + sm.setSubStateMachine(StateId::k1, &sub_sm); { //! 直通 sm.start(); - EXPECT_EQ(sm.currentState(), State::kInit); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_EQ(sm.currentState(), StateId::kInit); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); - EXPECT_TRUE(sm.run(Event::k1)); - EXPECT_EQ(sm.currentState(), State::k1); - EXPECT_EQ(sub_sm.currentState(), State::k1); + EXPECT_TRUE(sm.run(EventId::k1)); + EXPECT_EQ(sm.currentState(), StateId::k1); + EXPECT_EQ(sub_sm.currentState(), StateId::k1); - EXPECT_TRUE(sm.run(Event::k1)); - EXPECT_EQ(sm.currentState(), State::kTerm); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_TRUE(sm.run(EventId::k1)); + EXPECT_EQ(sm.currentState(), StateId::kTerm); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); EXPECT_TRUE(sm.isTerminated()); @@ -272,24 +272,24 @@ TEST(StateMachine, SubSM) { //! 在子状态机里转了一下 sm.start(); - EXPECT_EQ(sm.currentState(), State::kInit); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_EQ(sm.currentState(), StateId::kInit); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); - EXPECT_TRUE(sm.run(Event::k1)); - EXPECT_EQ(sm.currentState(), State::k1); - EXPECT_EQ(sub_sm.currentState(), State::k1); + EXPECT_TRUE(sm.run(EventId::k1)); + EXPECT_EQ(sm.currentState(), StateId::k1); + EXPECT_EQ(sub_sm.currentState(), StateId::k1); - EXPECT_TRUE(sm.run(Event::k2)); - EXPECT_EQ(sm.currentState(), State::k1); - EXPECT_EQ(sub_sm.currentState(), State::k2); + EXPECT_TRUE(sm.run(EventId::k2)); + EXPECT_EQ(sm.currentState(), StateId::k1); + EXPECT_EQ(sub_sm.currentState(), StateId::k2); - EXPECT_TRUE(sm.run(Event::k1)); - EXPECT_EQ(sm.currentState(), State::k1); - EXPECT_EQ(sub_sm.currentState(), State::k1); + EXPECT_TRUE(sm.run(EventId::k1)); + EXPECT_EQ(sm.currentState(), StateId::k1); + EXPECT_EQ(sub_sm.currentState(), StateId::k1); - EXPECT_TRUE(sm.run(Event::k1)); - EXPECT_EQ(sm.currentState(), State::kTerm); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_TRUE(sm.run(EventId::k1)); + EXPECT_EQ(sm.currentState(), StateId::kTerm); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); EXPECT_TRUE(sm.isTerminated()); sm.stop(); @@ -298,32 +298,32 @@ TEST(StateMachine, SubSM) { //! 从S2进的S1的子状态机,再出来回到S2,再进去 sm.start(); - EXPECT_EQ(sm.currentState(), State::kInit); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_EQ(sm.currentState(), StateId::kInit); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); - EXPECT_TRUE(sm.run(Event::k2)); - EXPECT_EQ(sm.currentState(), State::k2); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_TRUE(sm.run(EventId::k2)); + EXPECT_EQ(sm.currentState(), StateId::k2); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); - EXPECT_TRUE(sm.run(Event::k2)); - EXPECT_EQ(sm.currentState(), State::k1); - EXPECT_EQ(sub_sm.currentState(), State::k2); + EXPECT_TRUE(sm.run(EventId::k2)); + EXPECT_EQ(sm.currentState(), StateId::k1); + EXPECT_EQ(sub_sm.currentState(), StateId::k2); - EXPECT_TRUE(sm.run(Event::k2)); - EXPECT_EQ(sm.currentState(), State::k2); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_TRUE(sm.run(EventId::k2)); + EXPECT_EQ(sm.currentState(), StateId::k2); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); - EXPECT_TRUE(sm.run(Event::k2)); - EXPECT_EQ(sm.currentState(), State::k1); - EXPECT_EQ(sub_sm.currentState(), State::k2); + EXPECT_TRUE(sm.run(EventId::k2)); + EXPECT_EQ(sm.currentState(), StateId::k1); + EXPECT_EQ(sub_sm.currentState(), StateId::k2); - EXPECT_TRUE(sm.run(Event::k1)); - EXPECT_EQ(sm.currentState(), State::k1); - EXPECT_EQ(sub_sm.currentState(), State::k1); + EXPECT_TRUE(sm.run(EventId::k1)); + EXPECT_EQ(sm.currentState(), StateId::k1); + EXPECT_EQ(sub_sm.currentState(), StateId::k1); - EXPECT_TRUE(sm.run(Event::k1)); - EXPECT_EQ(sm.currentState(), State::kTerm); - EXPECT_EQ(sub_sm.currentState(), State::kTerm); + EXPECT_TRUE(sm.run(EventId::k1)); + EXPECT_EQ(sm.currentState(), StateId::kTerm); + EXPECT_EQ(sub_sm.currentState(), StateId::kTerm); sm.stop(); } @@ -331,156 +331,156 @@ TEST(StateMachine, SubSM) TEST(StateMachine, StateChangedCallback) { - enum class State { kTerm, kInit, k1 }; - enum class Event { kAny, k1 }; + enum class StateId { kTerm, kInit, k1 }; + enum class EventId { kAny, k1 }; SM sm; - sm.newState(State::kInit, nullptr, nullptr); - sm.newState(State::k1, nullptr, nullptr); + sm.newState(StateId::kInit, nullptr, nullptr); + sm.newState(StateId::k1, nullptr, nullptr); - sm.addRoute(State::kInit, Event::k1, State::k1, nullptr, nullptr); - sm.addRoute(State::k1, Event::k1, State::kTerm, nullptr, nullptr); + sm.addRoute(StateId::kInit, EventId::k1, StateId::k1, nullptr, nullptr); + sm.addRoute(StateId::k1, EventId::k1, StateId::kTerm, nullptr, nullptr); - State from, to; - Event event; + StateId from, to; + EventId event; sm.setStateChangedCallback( - [&] (SM::StateID f, SM::StateID t, SM::Event e) { - from = static_cast(f); - to = static_cast(t); - event = static_cast(e.id); + [&] (SM::StateID f, SM::StateID t, Event e) { + from = static_cast(f); + to = static_cast(t); + event = static_cast(e.id); } ); sm.start(); - sm.run(Event::k1); - EXPECT_EQ(from, State::kInit); - EXPECT_EQ(to, State::k1); - EXPECT_EQ(event, Event::k1); - - sm.run(Event::k1); - EXPECT_EQ(from, State::k1); - EXPECT_EQ(to, State::kTerm); - EXPECT_EQ(event, Event::k1); + sm.run(EventId::k1); + EXPECT_EQ(from, StateId::kInit); + EXPECT_EQ(to, StateId::k1); + EXPECT_EQ(event, EventId::k1); + + sm.run(EventId::k1); + EXPECT_EQ(from, StateId::k1); + EXPECT_EQ(to, StateId::kTerm); + EXPECT_EQ(event, EventId::k1); } //! 测试起始状态有子状态的情况 TEST(StateMachine, InitStateHasSubMachine) { - enum class State { kTerm, kInit }; - enum class Event { kAny, k1 }; + enum class StateId { kTerm, kInit }; + enum class EventId { kAny, k1 }; SM sm; SM sub_sm; int step = 0; - sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 0); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 6); ++step; }); - sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 8); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 9); ++step; }); - sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 7); ++step; }); + sm.newState(StateId::kInit, [&](Event){ EXPECT_EQ(step, 0); ++step; }, [&](Event){ EXPECT_EQ(step, 6); ++step; }); + sm.newState(StateId::kTerm, [&](Event){ EXPECT_EQ(step, 8); ++step; }, [&](Event){ EXPECT_EQ(step, 9); ++step; }); + sm.addRoute(StateId::kInit, EventId::k1, StateId::kTerm, nullptr, [&](Event){ EXPECT_EQ(step, 7); ++step; }); - sub_sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 1); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 2); ++step; }); - sub_sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 4); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 5); ++step; }); - sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 3); ++step; }); + sub_sm.newState(StateId::kInit, [&](Event){ EXPECT_EQ(step, 1); ++step; }, [&](Event){ EXPECT_EQ(step, 2); ++step; }); + sub_sm.newState(StateId::kTerm, [&](Event){ EXPECT_EQ(step, 4); ++step; }, [&](Event){ EXPECT_EQ(step, 5); ++step; }); + sub_sm.addRoute(StateId::kInit, EventId::k1, StateId::kTerm, nullptr, [&](Event){ EXPECT_EQ(step, 3); ++step; }); - sm.setSubStateMachine(State::kInit, &sub_sm); + sm.setSubStateMachine(StateId::kInit, &sub_sm); #if 0 sm.setStateChangedCallback( - [&] (SM::StateID f, SM::StateID t, SM::EventID e) { + [&] (SM::StateID f, SM::StateID t, EventID e) { cout << f << "-->" << t << "," << e << endl; } ); sub_sm.setStateChangedCallback( - [&] (SM::StateID f, SM::StateID t, SM::EventID e) { + [&] (SM::StateID f, SM::StateID t, EventID e) { cout << " " << f << "-->" << t << "," << e << endl; } ); #endif sm.start(); - sm.run(Event::k1); + sm.run(EventId::k1); sm.stop(); } //! 测试各种Action之间的执行顺序 TEST(StateMachine, SubSMActionOrder) { - enum class State { kTerm, kInit, k1}; - enum class Event { kAny, k1 }; + enum class StateId { kTerm, kInit, k1}; + enum class EventId { kAny, k1 }; SM sm; SM sub_sm; int step = 0; - sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 0); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 1); ++step; }); - sm.newState(State::k1, [&](SM::Event){ EXPECT_EQ(step, 3); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 9); ++step; }); - sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 11); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 12); ++step; }); + sm.newState(StateId::kInit, [&](Event){ EXPECT_EQ(step, 0); ++step; }, [&](Event){ EXPECT_EQ(step, 1); ++step; }); + sm.newState(StateId::k1, [&](Event){ EXPECT_EQ(step, 3); ++step; }, [&](Event){ EXPECT_EQ(step, 9); ++step; }); + sm.newState(StateId::kTerm, [&](Event){ EXPECT_EQ(step, 11); ++step; }, [&](Event){ EXPECT_EQ(step, 12); ++step; }); - sm.addRoute(State::kInit, Event::k1, State::k1, nullptr, [&](SM::Event){ EXPECT_EQ(step, 2); ++step; }); - sm.addRoute(State::k1, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 10); ++step; }); + sm.addRoute(StateId::kInit, EventId::k1, StateId::k1, nullptr, [&](Event){ EXPECT_EQ(step, 2); ++step; }); + sm.addRoute(StateId::k1, EventId::k1, StateId::kTerm, nullptr, [&](Event){ EXPECT_EQ(step, 10); ++step; }); - sub_sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 4); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 5); ++step; }); - sub_sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 7); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 8); ++step; }); - sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 6); ++step; }); + sub_sm.newState(StateId::kInit, [&](Event){ EXPECT_EQ(step, 4); ++step; }, [&](Event){ EXPECT_EQ(step, 5); ++step; }); + sub_sm.newState(StateId::kTerm, [&](Event){ EXPECT_EQ(step, 7); ++step; }, [&](Event){ EXPECT_EQ(step, 8); ++step; }); + sub_sm.addRoute(StateId::kInit, EventId::k1, StateId::kTerm, nullptr, [&](Event){ EXPECT_EQ(step, 6); ++step; }); - sm.setSubStateMachine(State::k1, &sub_sm); + sm.setSubStateMachine(StateId::k1, &sub_sm); #if 0 sm.setStateChangedCallback( - [&] (SM::StateID f, SM::StateID t, SM::EventID e) { + [&] (SM::StateID f, SM::StateID t, EventID e) { cout << f << "-->" << t << "," << e << endl; } ); sub_sm.setStateChangedCallback( - [&] (SM::StateID f, SM::StateID t, SM::EventID e) { + [&] (SM::StateID f, SM::StateID t, EventID e) { cout << " " << f << "-->" << t << "," << e << endl; } ); #endif sm.start(); - sm.run(Event::k1); - sm.run(Event::k1); + sm.run(EventId::k1); + sm.run(EventId::k1); sm.stop(); } TEST(StateMachine, EventExtra) { - enum class State { kTerm, kInit }; + enum class StateId { kTerm, kInit }; SM sm; int extra_data = 100; int count = 0; - sm.newState(State::kInit, - [&](SM::Event e){ + sm.newState(StateId::kInit, + [&](Event e){ ++count; EXPECT_EQ(e.id, 0); EXPECT_EQ(e.extra, nullptr); }, - [&](SM::Event e){ + [&](Event e){ ++count; EXPECT_EQ(e.id, 10); EXPECT_EQ(e.extra, &extra_data); } ); - sm.newState(State::kTerm, - [&](SM::Event e){ + sm.newState(StateId::kTerm, + [&](Event e){ ++count; EXPECT_EQ(e.id, 10); EXPECT_EQ(e.extra, &extra_data); }, - [&](SM::Event e){ + [&](Event e){ ++count; EXPECT_EQ(e.id, 0); EXPECT_EQ(e.extra, nullptr); } ); - sm.addRoute(State::kInit, 10, State::kTerm, - [&](SM::Event e){ + sm.addRoute(StateId::kInit, 10, StateId::kTerm, + [&](Event e){ ++count; EXPECT_EQ(e.id, 10); EXPECT_EQ(e.extra, &extra_data); return true; }, - [&](SM::Event e){ + [&](Event e){ ++count; EXPECT_EQ(e.id, 10); EXPECT_EQ(e.extra, &extra_data); @@ -488,7 +488,7 @@ TEST(StateMachine, EventExtra) ); sm.setStateChangedCallback( - [&] (SM::StateID f, SM::StateID t, SM::Event e) { + [&] (SM::StateID f, SM::StateID t, Event e) { ++count; EXPECT_EQ(e.id, 10); EXPECT_EQ(e.extra, &extra_data); @@ -496,60 +496,60 @@ TEST(StateMachine, EventExtra) ); sm.start(); - sm.run(SM::Event(10, &extra_data)); + sm.run(Event(10, &extra_data)); sm.stop(); EXPECT_EQ(count, 7); } TEST(StateMachine, InnerEvent) { - enum class State { kTerm, k1, k2 }; - enum class Event { kAny, k1 }; + enum class StateId { kTerm, k1, k2 }; + enum class EventId { kAny, k1 }; SM sm; bool is_k1_event_run = false; bool is_k2_event_run = false; - sm.setInitState(State::k1); - sm.newState(State::k1, nullptr, nullptr); - sm.newState(State::k2, nullptr, nullptr); - sm.addEvent(State::k1, Event::k1, [&](SM::Event) { is_k1_event_run = true; return -1; }); - sm.addRoute(State::k1, Event::k1, State::k2, nullptr, nullptr); - sm.addEvent(State::k2, Event::k1, [&](SM::Event) { is_k2_event_run = true; return -1; }); + sm.setInitState(StateId::k1); + sm.newState(StateId::k1, nullptr, nullptr); + sm.newState(StateId::k2, nullptr, nullptr); + sm.addEvent(StateId::k1, EventId::k1, [&](Event) { is_k1_event_run = true; return -1; }); + sm.addRoute(StateId::k1, EventId::k1, StateId::k2, nullptr, nullptr); + sm.addEvent(StateId::k2, EventId::k1, [&](Event) { is_k2_event_run = true; return -1; }); sm.start(); - sm.run(Event::k1); + sm.run(EventId::k1); EXPECT_TRUE(is_k1_event_run); EXPECT_FALSE(is_k2_event_run); - EXPECT_EQ(sm.currentState(), State::k2); + EXPECT_EQ(sm.currentState(), StateId::k2); is_k1_event_run = false; - sm.run(Event::k1); + sm.run(EventId::k1); EXPECT_FALSE(is_k1_event_run); EXPECT_TRUE(is_k2_event_run); } TEST(StateMachine, InnerAnyEvent) { - enum class State { kTerm, k1 }; - enum class Event { kAny, k1, k2 }; + enum class StateId { kTerm, k1 }; + enum class EventId { kAny, k1, k2 }; SM sm; bool is_k1_event_run = false; bool is_any_event_run = false; - sm.setInitState(State::k1); - sm.newState(State::k1, nullptr, nullptr); - sm.addEvent(State::k1, Event::k1, [&](SM::Event) { is_k1_event_run = true; return -1; }); - sm.addEvent(State::k1, Event::kAny, [&](SM::Event) { is_any_event_run = true; return -1; }); + sm.setInitState(StateId::k1); + sm.newState(StateId::k1, nullptr, nullptr); + sm.addEvent(StateId::k1, EventId::k1, [&](Event) { is_k1_event_run = true; return -1; }); + sm.addEvent(StateId::k1, EventId::kAny, [&](Event) { is_any_event_run = true; return -1; }); sm.start(); - sm.run(Event::k1); + sm.run(EventId::k1); EXPECT_TRUE(is_k1_event_run); EXPECT_FALSE(is_any_event_run); is_k1_event_run = false; - sm.run(Event::k2); + sm.run(EventId::k2); EXPECT_FALSE(is_k1_event_run); EXPECT_TRUE(is_any_event_run); } @@ -561,7 +561,7 @@ TEST(StateMachine, SwitchStateInEvent) { sm.setInitState(STATE_A); sm.addEvent(STATE_A, EVENT_ANY, - [](SM::Event e) -> SM::StateID { + [](Event e) -> SM::StateID { if (e.id == EVENT_1) return STATE_B; if (e.id == EVENT_2) @@ -570,7 +570,7 @@ TEST(StateMachine, SwitchStateInEvent) { } ); sm.addEvent(STATE_B, EVENT_1, - [](SM::Event e) -> SM::StateID { return STATE_A; } + [](Event e) -> SM::StateID { return STATE_A; } ); sm.start(); @@ -587,28 +587,28 @@ TEST(StateMachine, SwitchStateInEvent) { TEST(StateMachine, SetInitState) { - enum class State { kTerm, k1, kInit }; - enum class Event { kAny, k1 }; + enum class StateId { kTerm, k1, kInit }; + enum class EventId { kAny, k1 }; SM sm; - sm.setInitState(State::kInit); - sm.newState(State::kInit, nullptr, nullptr); - sm.newState(State::k1, nullptr, nullptr); + sm.setInitState(StateId::kInit); + sm.newState(StateId::kInit, nullptr, nullptr); + sm.newState(StateId::k1, nullptr, nullptr); EXPECT_TRUE(sm.start()); - EXPECT_EQ(sm.currentState(), State::kInit); + EXPECT_EQ(sm.currentState(), StateId::kInit); } TEST(StateMachine, SetInitState_Fail) { - enum class State { kTerm, k1, kInit }; - enum class Event { kAny, k1 }; + enum class StateId { kTerm, k1, kInit }; + enum class EventId { kAny, k1 }; SM sm; - sm.setInitState(State::kInit); - sm.newState(State::k1, nullptr, nullptr); + sm.setInitState(StateId::kInit); + sm.newState(StateId::k1, nullptr, nullptr); EXPECT_FALSE(sm.start()); } diff --git a/modules/mqtt/client.cpp b/modules/mqtt/client.cpp index 5f3063ea3af78d598fec447260be64a28dbf5d7c..746c9b105d2f4bde3f4d7cbe683ebb97247a6448 100644 --- a/modules/mqtt/client.cpp +++ b/modules/mqtt/client.cpp @@ -1,6 +1,7 @@ #include "client.h" #include +#include #include #include @@ -33,6 +34,33 @@ struct Client::Data { std::thread *sp_thread = nullptr; + std::shared_ptr alive_tag; //!< 对像存活标签 + /// Q1: 为什么需要定义它? + /// + /// 因为在下面使用了runInLoop()函数注册了延后执行的回调函数。在该函数中将this指针捕获了。 + /// 在它真正执行的时候如果 this 指针所指向的对象就已经被 delete 了,那么如果我们还继续 + /// 访问那么就一定会触发访问失效的内存地址,行为不可预知。 + /// + /// 怎么解决呢? + /// 方法一:在对象析构的时候,撤消之前通过 runInLoop() 注册的回调函数; + /// 方法二:在回调函数真正执行的时候,判定一下 this 所指的对象是否有效。 + /// + /// 这里,我们采用了方法二。 + /// 那怎么实现在后期能知晓 this 指针是否有效呢? + /// 通用的方案是:将 this 指针用 std::shared_ptr 进行管理。传入回调函数中的是它的 weak_ptr + /// 这样,在使用 this 指针的时候就能得知它是否有效了。 + /// + /// 这么做有一个缺点:这个对象必须继续于 enable_this_shared_ptr 类,而且实例化它的时候必须 + /// 使用 std::make_shared() 才有效。这样种会给使用者造成一定的负担。 + /// 除此之外,还有没有别的方法?于是,我想到了 alive_tag。 + /// 由于对象的成员的生命期与对象自身是一致的,判定一个对象是否有效,可以通过判定它的某个成 + /// 员生命期是否有效即可。内部附带一个任意类型的智能指针。于是,我在这里定义了一个 alive_tag + /// 的智能指针。它的类型可以是任意的,这里就选定了bool。 + /// 无所谓它的类型,反正我们也不会往里写内容。 + /// 在 runInLoop() 注册回调函数的时候,就通过 alive_tag 生成对应的 std::weak_ptr 并传入。 + /// 在回调函数执行之前,检查一下这个 std::weak_ptr 是否过期就可以得知 this 指针是否有效。 + /// 进而避免继续访问到无效 this 指向的内容。 + static int _instance_count; }; @@ -59,6 +87,8 @@ Client::Client(Loop *wp_loop) : d_->sp_sock_write_ev = wp_loop->newFdEvent(); d_->sp_sock_write_ev->setCallback(std::bind(&Client::onSocketWrite, this)); + + d_->alive_tag = std::make_shared(true); } Client::~Client() @@ -237,15 +267,19 @@ bool Client::start() d_->state = State::kConnecting; CHECK_DELETE_RESET_OBJ(d_->sp_thread); + std::weak_ptr alive_tag(d_->alive_tag); //! 原理见Q1 + //! 由于 mosquitto_connect() 是阻塞函数,为了避免阻塞其它事件,特交给子线程去做 d_->sp_thread = new thread( - [this] { + [this, alive_tag] { int ret = mosquitto_connect(d_->sp_mosq, d_->config.base.broker.domain.c_str(), d_->config.base.broker.port, d_->config.base.keepalive); d_->wp_loop->runInLoop( - [this, ret] { + [this, ret, alive_tag] { + if (alive_tag.expired()) //!< 判定this指针是否有效 + return; onMosquittoConnectDone(ret, true); } ); @@ -350,11 +384,14 @@ void Client::onTimerTick() if (d_->state == State::kConnecting) { if (d_->sp_thread == nullptr) { + std::weak_ptr alive_tag(d_->alive_tag); //! 原理见Q1 d_->sp_thread = new thread( - [this] { + [this, alive_tag] { int ret = mosquitto_reconnect(d_->sp_mosq); d_->wp_loop->runInLoop( - [this, ret] { + [this, ret, alive_tag] { + if (alive_tag.expired()) //!< 判定this指针是否有效 + return; onMosquittoConnectDone(ret, false); } ); diff --git a/modules/util/Makefile b/modules/util/Makefile index 6b05b111f0761f3e049c5c5cf7f8c9998063bd19..07b10af3c48b6ae34483c6ff26d1faa014c05d3e 100644 --- a/modules/util/Makefile +++ b/modules/util/Makefile @@ -11,7 +11,6 @@ HEAD_FILES = \ split_cmdline.h \ serializer.h \ time_counter.h \ - state_machine.h \ async_pipe.h \ backtrace.h \ timestamp.h \ @@ -25,7 +24,6 @@ CPP_SRC_FILES = \ split_cmdline.cpp \ serializer.cpp \ time_counter.cpp \ - state_machine.cpp \ async_pipe.cpp \ backtrace.cpp \ timestamp.cpp \ @@ -41,7 +39,6 @@ TEST_CPP_SRC_FILES = \ split_cmdline_test.cpp \ serializer_test.cpp \ time_counter_test.cpp \ - state_machine_test.cpp \ async_pipe_test.cpp \ json_test.cpp \