From 6cd9719eba5f43a6f9d6044abce7393fa629cbbb Mon Sep 17 00:00:00 2001 From: Hevake Date: Tue, 6 Dec 2022 07:40:11 +0800 Subject: [PATCH 1/7] feat:rename action to flow --- config.mk | 2 +- modules/{action => flow}/Makefile | 5 +- modules/{action => flow}/action.cpp | 2 +- modules/{action => flow}/action.h | 2 +- modules/{action => flow}/action_executor.cpp | 2 +- modules/{action => flow}/action_executor.h | 2 +- .../{action => flow}/action_executor_test.cpp | 2 +- .../{action => flow}/actions/event_action.cpp | 2 +- .../{action => flow}/actions/event_action.h | 2 +- .../actions/fallback_action.cpp | 2 +- .../actions/fallback_action.h | 2 +- .../actions/fallback_action_test.cpp | 2 +- .../actions/if_else_action.cpp | 2 +- .../{action => flow}/actions/if_else_action.h | 2 +- .../actions/if_else_action_test.cpp | 2 +- .../actions/invert_action.cpp | 2 +- .../{action => flow}/actions/invert_action.h | 2 +- .../actions/invert_action_test.cpp | 2 +- .../{action => flow}/actions/loop_action.cpp | 2 +- .../{action => flow}/actions/loop_action.h | 2 +- .../actions/loop_action_test.cpp | 2 +- .../actions/loop_if_action.cpp | 2 +- .../{action => flow}/actions/loop_if_action.h | 2 +- .../actions/loop_if_action_test.cpp | 2 +- .../actions/nondelay_action.cpp | 2 +- .../actions/nondelay_action.h | 2 +- .../actions/nondelay_action_test.cpp | 2 +- .../actions/parallel_action.cpp | 2 +- .../actions/parallel_action.h | 2 +- .../actions/parallel_action_test.cpp | 2 +- .../actions/sequence_action.cpp | 2 +- .../actions/sequence_action.h | 2 +- .../actions/sequence_action_test.cpp | 2 +- .../{action => flow}/actions/sleep_action.cpp | 2 +- .../{action => flow}/actions/sleep_action.h | 2 +- .../actions/sleep_action_test.cpp | 2 +- modules/{action => flow}/event.h | 2 +- modules/{action => flow}/event_publisher.h | 2 +- .../{action => flow}/event_publisher_impl.cpp | 2 +- .../{action => flow}/event_publisher_impl.h | 2 +- .../event_publisher_impl_test.cpp | 0 modules/{action => flow}/event_subscriber.h | 2 +- modules/{util => flow}/state_machine.cpp | 2 +- modules/{util => flow}/state_machine.h | 16 +--- modules/{util => flow}/state_machine_test.cpp | 90 +++++++++---------- modules/util/Makefile | 3 - 46 files changed, 92 insertions(+), 104 deletions(-) rename modules/{action => flow}/Makefile (94%) rename modules/{action => flow}/action.cpp (99%) rename modules/{action => flow}/action.h (99%) rename modules/{action => flow}/action_executor.cpp (99%) rename modules/{action => flow}/action_executor.h (99%) rename modules/{action => flow}/action_executor_test.cpp (99%) rename modules/{action => flow}/actions/event_action.cpp (96%) rename modules/{action => flow}/actions/event_action.h (97%) rename modules/{action => flow}/actions/fallback_action.cpp (98%) rename modules/{action => flow}/actions/fallback_action.h (98%) rename modules/{action => flow}/actions/fallback_action_test.cpp (99%) rename modules/{action => flow}/actions/if_else_action.cpp (99%) rename modules/{action => flow}/actions/if_else_action.h (98%) rename modules/{action => flow}/actions/if_else_action_test.cpp (99%) rename modules/{action => flow}/actions/invert_action.cpp (97%) rename modules/{action => flow}/actions/invert_action.h (97%) rename modules/{action => flow}/actions/invert_action_test.cpp (97%) rename modules/{action => flow}/actions/loop_action.cpp (98%) rename modules/{action => flow}/actions/loop_action.h (97%) rename modules/{action => flow}/actions/loop_action_test.cpp (98%) rename modules/{action => flow}/actions/loop_if_action.cpp (98%) rename modules/{action => flow}/actions/loop_if_action.h (98%) rename modules/{action => flow}/actions/loop_if_action_test.cpp (98%) rename modules/{action => flow}/actions/nondelay_action.cpp (94%) rename modules/{action => flow}/actions/nondelay_action.h (96%) rename modules/{action => flow}/actions/nondelay_action_test.cpp (98%) rename modules/{action => flow}/actions/parallel_action.cpp (98%) rename modules/{action => flow}/actions/parallel_action.h (97%) rename modules/{action => flow}/actions/parallel_action_test.cpp (99%) rename modules/{action => flow}/actions/sequence_action.cpp (98%) rename modules/{action => flow}/actions/sequence_action.h (98%) rename modules/{action => flow}/actions/sequence_action_test.cpp (99%) rename modules/{action => flow}/actions/sleep_action.cpp (98%) rename modules/{action => flow}/actions/sleep_action.h (98%) rename modules/{action => flow}/actions/sleep_action_test.cpp (99%) rename modules/{action => flow}/event.h (95%) rename modules/{action => flow}/event_publisher.h (96%) rename modules/{action => flow}/event_publisher_impl.cpp (98%) rename modules/{action => flow}/event_publisher_impl.h (96%) rename modules/{action => flow}/event_publisher_impl_test.cpp (100%) rename modules/{action => flow}/event_subscriber.h (94%) rename modules/{util => flow}/state_machine.cpp (99%) rename modules/{util => flow}/state_machine.h (94%) rename modules/{util => flow}/state_machine_test.cpp (82%) diff --git a/config.mk b/config.mk index f3418b3..56b6844 100644 --- a/config.mk +++ b/config.mk @@ -9,5 +9,5 @@ MODULES += coroutine MODULES += mqtt MODULES += terminal MODULES += main -MODULES += action +MODULES += flow MODULES += timer diff --git a/modules/action/Makefile b/modules/flow/Makefile similarity index 94% rename from modules/action/Makefile rename to modules/flow/Makefile index 78fb12a..230c8ce 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 \ 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 a9d7344..fc465f8 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 99% rename from modules/action/action.h rename to modules/flow/action.h index 6fa8de6..b656762 100644 --- a/modules/action/action.h +++ b/modules/flow/action.h @@ -9,7 +9,7 @@ #include namespace tbox { -namespace action { +namespace flow { //! 动作类 class Action { 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 4eb7de2..2ac2133 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 99% rename from modules/action/action_executor.h rename to modules/flow/action_executor.h index 5f86a81..13bac2e 100644 --- a/modules/action/action_executor.h +++ b/modules/flow/action_executor.h @@ -6,7 +6,7 @@ #include namespace tbox { -namespace action { +namespace flow { class Action; 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 6577d90..82b42a9 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 5b38a61..70d8f38 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 97% rename from modules/action/actions/event_action.h rename to modules/flow/actions/event_action.h index c1b0493..c458956 100644 --- a/modules/action/actions/event_action.h +++ b/modules/flow/actions/event_action.h @@ -6,7 +6,7 @@ #include "../event_publisher.h" namespace tbox { -namespace action { +namespace flow { class EventAction : public Action, public EventSubscriber { 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 d46e545..437ef17 100644 --- a/modules/action/actions/fallback_action.cpp +++ b/modules/flow/actions/fallback_action.cpp @@ -5,7 +5,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 98% rename from modules/action/actions/fallback_action.h rename to modules/flow/actions/fallback_action.h index 9e43759..b2ec090 100644 --- a/modules/action/actions/fallback_action.h +++ b/modules/flow/actions/fallback_action.h @@ -4,7 +4,7 @@ #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool FallbackAction(acton_vec) { 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 7a0762f..2264c92 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 b05a192..18f2620 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 98% rename from modules/action/actions/if_else_action.h rename to modules/flow/actions/if_else_action.h index 6f6fec0..a0e4d37 100644 --- a/modules/action/actions/if_else_action.h +++ b/modules/flow/actions/if_else_action.h @@ -4,7 +4,7 @@ #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool IfElseAction(cond_action, if_action, else_acton) { 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 dd7fcb6..bbfd7bf 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 fb1ed35..190c598 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 97% rename from modules/action/actions/invert_action.h rename to modules/flow/actions/invert_action.h index cc88969..57a9d95 100644 --- a/modules/action/actions/invert_action.h +++ b/modules/flow/actions/invert_action.h @@ -4,7 +4,7 @@ #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool InvertAction(child) { 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 53ca326..222fc0e 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 26d08e6..f95d421 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 97% rename from modules/action/actions/loop_action.h rename to modules/flow/actions/loop_action.h index f979a4d..4022990 100644 --- a/modules/action/actions/loop_action.h +++ b/modules/flow/actions/loop_action.h @@ -4,7 +4,7 @@ #include "../action.h" namespace tbox { -namespace action { +namespace flow { class LoopAction : public Action { public: 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 ccfc27b..dccc867 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 91883c4..7e1cacc 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 98% rename from modules/action/actions/loop_if_action.h rename to modules/flow/actions/loop_if_action.h index 195af57..a34dd18 100644 --- a/modules/action/actions/loop_if_action.h +++ b/modules/flow/actions/loop_if_action.h @@ -4,7 +4,7 @@ #include "../action.h" namespace tbox { -namespace action { +namespace flow { class LoopIfAction : public Action { public: 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 638ab8f..1893a3d 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 a80e56a..99dcec9 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 96% rename from modules/action/actions/nondelay_action.h rename to modules/flow/actions/nondelay_action.h index bdfd91a..0f4d6da 100644 --- a/modules/action/actions/nondelay_action.h +++ b/modules/flow/actions/nondelay_action.h @@ -4,7 +4,7 @@ #include "../action.h" namespace tbox { -namespace action { +namespace flow { class NondelayAction : public Action { public: 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 60443d2..8c8efa8 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 a9b4e20..200899f 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 97% rename from modules/action/actions/parallel_action.h rename to modules/flow/actions/parallel_action.h index 12355e0..6114d1e 100644 --- a/modules/action/actions/parallel_action.h +++ b/modules/flow/actions/parallel_action.h @@ -7,7 +7,7 @@ #include namespace tbox { -namespace action { +namespace flow { class ParallelAction : public Action { public: 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 e6eb56e..c338dbf 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 3fe0f4b..eb4d723 100644 --- a/modules/action/actions/sequence_action.cpp +++ b/modules/flow/actions/sequence_action.cpp @@ -5,7 +5,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 98% rename from modules/action/actions/sequence_action.h rename to modules/flow/actions/sequence_action.h index 537a1b8..a120b2d 100644 --- a/modules/action/actions/sequence_action.h +++ b/modules/flow/actions/sequence_action.h @@ -4,7 +4,7 @@ #include "../action.h" namespace tbox { -namespace action { +namespace flow { /** * bool SequenceAction(acton_vec) { 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 d58ea01..028ed70 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 ac61ce8..6a7ea67 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 98% rename from modules/action/actions/sleep_action.h rename to modules/flow/actions/sleep_action.h index c8b7a68..b55c7d3 100644 --- a/modules/action/actions/sleep_action.h +++ b/modules/flow/actions/sleep_action.h @@ -5,7 +5,7 @@ #include namespace tbox { -namespace action { +namespace flow { class SleepAction : public Action { public: 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 150c164..d4ddbf8 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 95% rename from modules/action/event.h rename to modules/flow/event.h index 8518ebf..743807f 100644 --- a/modules/action/event.h +++ b/modules/flow/event.h @@ -2,7 +2,7 @@ #define TBOX_ACTION_EVENT_H_20221022 namespace tbox { -namespace action { +namespace flow { struct Event { using ID = int; diff --git a/modules/action/event_publisher.h b/modules/flow/event_publisher.h similarity index 96% rename from modules/action/event_publisher.h rename to modules/flow/event_publisher.h index 89ba9ea..9bd671a 100644 --- a/modules/action/event_publisher.h +++ b/modules/flow/event_publisher.h @@ -4,7 +4,7 @@ #include "event.h" namespace tbox { -namespace action { +namespace flow { class EventSubscriber; 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 b17e1c7..169d1ce 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 96% rename from modules/action/event_publisher_impl.h rename to modules/flow/event_publisher_impl.h index c1aab21..c0bb219 100644 --- a/modules/action/event_publisher_impl.h +++ b/modules/flow/event_publisher_impl.h @@ -5,7 +5,7 @@ #include "event_publisher.h" namespace tbox { -namespace action { +namespace flow { class EventPublisherImpl : public EventPublisher { public: 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 94% rename from modules/action/event_subscriber.h rename to modules/flow/event_subscriber.h index 78ecdf5..6694f73 100644 --- a/modules/action/event_subscriber.h +++ b/modules/flow/event_subscriber.h @@ -4,7 +4,7 @@ #include "event.h" namespace tbox { -namespace action { +namespace flow { class EventSubscriber { public: 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 3eb41d1..56b405d 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 94% rename from modules/util/state_machine.h rename to modules/flow/state_machine.h index 383a91d..ae1c537 100644 --- a/modules/util/state_machine.h +++ b/modules/flow/state_machine.h @@ -2,9 +2,10 @@ #define TBOX_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表示条件不成立 diff --git a/modules/util/state_machine_test.cpp b/modules/flow/state_machine_test.cpp similarity index 82% rename from modules/util/state_machine_test.cpp rename to modules/flow/state_machine_test.cpp index e106e9c..86180c0 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); @@ -204,7 +204,7 @@ TEST(StateMachine, EnumClass) int enter_counter = 0; int exit_counter = 0; - sm.newState(State::k1, [&](SM::Event){ ++enter_counter; }, [&](SM::Event){ ++exit_counter; }); + sm.newState(State::k1, [&](Event){ ++enter_counter; }, [&](Event){ ++exit_counter; }); sm.newState(State::k2, nullptr, nullptr); sm.addRoute(State::k1, Event::k1, State::k2, nullptr, nullptr); @@ -346,7 +346,7 @@ TEST(StateMachine, StateChangedCallback) Event event; sm.setStateChangedCallback( - [&] (SM::StateID f, SM::StateID t, SM::Event e) { + [&] (SM::StateID f, SM::StateID t, Event e) { from = static_cast(f); to = static_cast(t); event = static_cast(e.id); @@ -374,23 +374,23 @@ TEST(StateMachine, InitStateHasSubMachine) 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(State::kInit, [&](Event){ EXPECT_EQ(step, 0); ++step; }, [&](Event){ EXPECT_EQ(step, 6); ++step; }); + sm.newState(State::kTerm, [&](Event){ EXPECT_EQ(step, 8); ++step; }, [&](Event){ EXPECT_EQ(step, 9); ++step; }); + sm.addRoute(State::kInit, Event::k1, State::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(State::kInit, [&](Event){ EXPECT_EQ(step, 1); ++step; }, [&](Event){ EXPECT_EQ(step, 2); ++step; }); + sub_sm.newState(State::kTerm, [&](Event){ EXPECT_EQ(step, 4); ++step; }, [&](Event){ EXPECT_EQ(step, 5); ++step; }); + sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](Event){ EXPECT_EQ(step, 3); ++step; }); sm.setSubStateMachine(State::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; } ); @@ -410,26 +410,26 @@ TEST(StateMachine, SubSMActionOrder) 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(State::kInit, [&](Event){ EXPECT_EQ(step, 0); ++step; }, [&](Event){ EXPECT_EQ(step, 1); ++step; }); + sm.newState(State::k1, [&](Event){ EXPECT_EQ(step, 3); ++step; }, [&](Event){ EXPECT_EQ(step, 9); ++step; }); + sm.newState(State::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(State::kInit, Event::k1, State::k1, nullptr, [&](Event){ EXPECT_EQ(step, 2); ++step; }); + sm.addRoute(State::k1, Event::k1, State::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(State::kInit, [&](Event){ EXPECT_EQ(step, 4); ++step; }, [&](Event){ EXPECT_EQ(step, 5); ++step; }); + sub_sm.newState(State::kTerm, [&](Event){ EXPECT_EQ(step, 7); ++step; }, [&](Event){ EXPECT_EQ(step, 8); ++step; }); + sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](Event){ EXPECT_EQ(step, 6); ++step; }); sm.setSubStateMachine(State::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; } ); @@ -449,24 +449,24 @@ TEST(StateMachine, EventExtra) int extra_data = 100; int count = 0; sm.newState(State::kInit, - [&](SM::Event e){ + [&](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){ + [&](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); @@ -474,13 +474,13 @@ TEST(StateMachine, EventExtra) ); sm.addRoute(State::kInit, 10, State::kTerm, - [&](SM::Event e){ + [&](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,7 +496,7 @@ TEST(StateMachine, EventExtra) ); sm.start(); - sm.run(SM::Event(10, &extra_data)); + sm.run(Event(10, &extra_data)); sm.stop(); EXPECT_EQ(count, 7); } @@ -513,9 +513,9 @@ TEST(StateMachine, InnerEvent) { 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.addEvent(State::k1, Event::k1, [&](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.addEvent(State::k2, Event::k1, [&](Event) { is_k2_event_run = true; return -1; }); sm.start(); sm.run(Event::k1); @@ -540,8 +540,8 @@ TEST(StateMachine, InnerAnyEvent) { 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.addEvent(State::k1, Event::k1, [&](Event) { is_k1_event_run = true; return -1; }); + sm.addEvent(State::k1, Event::kAny, [&](Event) { is_any_event_run = true; return -1; }); sm.start(); sm.run(Event::k1); @@ -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(); diff --git a/modules/util/Makefile b/modules/util/Makefile index 6b05b11..07b10af 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 \ -- Gitee From be0a7e363a0386fd32b6d198920f9e35cc917dae Mon Sep 17 00:00:00 2001 From: Hevake Date: Tue, 6 Dec 2022 07:44:08 +0800 Subject: [PATCH 2/7] fix: solve compile error in state_machine_test.cpp --- modules/flow/state_machine_test.cpp | 296 ++++++++++++++-------------- 1 file changed, 148 insertions(+), 148 deletions(-) diff --git a/modules/flow/state_machine_test.cpp b/modules/flow/state_machine_test.cpp index 86180c0..f8e877f 100644 --- a/modules/flow/state_machine_test.cpp +++ b/modules/flow/state_machine_test.cpp @@ -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, [&](Event){ ++enter_counter; }, [&](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,58 +331,58 @@ 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, Event e) { - from = static_cast(f); - to = static_cast(t); - event = static_cast(e.id); + 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, [&](Event){ EXPECT_EQ(step, 0); ++step; }, [&](Event){ EXPECT_EQ(step, 6); ++step; }); - sm.newState(State::kTerm, [&](Event){ EXPECT_EQ(step, 8); ++step; }, [&](Event){ EXPECT_EQ(step, 9); ++step; }); - sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](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, [&](Event){ EXPECT_EQ(step, 1); ++step; }, [&](Event){ EXPECT_EQ(step, 2); ++step; }); - sub_sm.newState(State::kTerm, [&](Event){ EXPECT_EQ(step, 4); ++step; }, [&](Event){ EXPECT_EQ(step, 5); ++step; }); - sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](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, EventID e) { @@ -396,32 +396,32 @@ TEST(StateMachine, InitStateHasSubMachine) ); #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, [&](Event){ EXPECT_EQ(step, 0); ++step; }, [&](Event){ EXPECT_EQ(step, 1); ++step; }); - sm.newState(State::k1, [&](Event){ EXPECT_EQ(step, 3); ++step; }, [&](Event){ EXPECT_EQ(step, 9); ++step; }); - sm.newState(State::kTerm, [&](Event){ EXPECT_EQ(step, 11); ++step; }, [&](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, [&](Event){ EXPECT_EQ(step, 2); ++step; }); - sm.addRoute(State::k1, Event::k1, State::kTerm, nullptr, [&](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, [&](Event){ EXPECT_EQ(step, 4); ++step; }, [&](Event){ EXPECT_EQ(step, 5); ++step; }); - sub_sm.newState(State::kTerm, [&](Event){ EXPECT_EQ(step, 7); ++step; }, [&](Event){ EXPECT_EQ(step, 8); ++step; }); - sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](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, EventID e) { @@ -435,20 +435,20 @@ TEST(StateMachine, SubSMActionOrder) ); #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.newState(StateId::kInit, [&](Event e){ ++count; EXPECT_EQ(e.id, 0); @@ -460,7 +460,7 @@ TEST(StateMachine, EventExtra) EXPECT_EQ(e.extra, &extra_data); } ); - sm.newState(State::kTerm, + sm.newState(StateId::kTerm, [&](Event e){ ++count; EXPECT_EQ(e.id, 10); @@ -473,7 +473,7 @@ TEST(StateMachine, EventExtra) } ); - sm.addRoute(State::kInit, 10, State::kTerm, + sm.addRoute(StateId::kInit, 10, StateId::kTerm, [&](Event e){ ++count; EXPECT_EQ(e.id, 10); @@ -502,54 +502,54 @@ TEST(StateMachine, EventExtra) } 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, [&](Event) { is_k1_event_run = true; return -1; }); - sm.addRoute(State::k1, Event::k1, State::k2, nullptr, nullptr); - sm.addEvent(State::k2, Event::k1, [&](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, [&](Event) { is_k1_event_run = true; return -1; }); - sm.addEvent(State::k1, Event::kAny, [&](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); } @@ -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()); } -- Gitee From 8fa7eea123fef26a4e77e241e343aa1b9c6500bc Mon Sep 17 00:00:00 2001 From: Hevake Date: Tue, 6 Dec 2022 07:49:47 +0800 Subject: [PATCH 3/7] tidy(flow) --- modules/flow/action.h | 6 +++--- modules/flow/action_executor.h | 6 +++--- modules/flow/actions/event_action.h | 6 +++--- modules/flow/actions/fallback_action.h | 6 +++--- modules/flow/actions/if_else_action.h | 6 +++--- modules/flow/actions/invert_action.h | 6 +++--- modules/flow/actions/loop_action.h | 6 +++--- modules/flow/actions/loop_if_action.h | 6 +++--- modules/flow/actions/nondelay_action.h | 6 +++--- modules/flow/actions/parallel_action.h | 6 +++--- modules/flow/actions/sequence_action.h | 6 +++--- modules/flow/actions/sleep_action.h | 6 +++--- modules/flow/event.h | 6 +++--- modules/flow/event_publisher.h | 6 +++--- modules/flow/event_publisher_impl.h | 6 +++--- modules/flow/event_subscriber.h | 6 +++--- modules/flow/state_machine.h | 6 +++--- 17 files changed, 51 insertions(+), 51 deletions(-) diff --git a/modules/flow/action.h b/modules/flow/action.h index b656762..8e1bce9 100644 --- a/modules/flow/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 @@ -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/flow/action_executor.h b/modules/flow/action_executor.h index 13bac2e..e5cf196 100644 --- a/modules/flow/action_executor.h +++ b/modules/flow/action_executor.h @@ -1,5 +1,5 @@ -#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 @@ -83,4 +83,4 @@ class ActionExecutor { } } -#endif //TBOX_ACTION_EXECUTOR_H_20221112 +#endif //TBOX_FLOW_EXECUTOR_H_20221112 diff --git a/modules/flow/actions/event_action.h b/modules/flow/actions/event_action.h index c458956..07f0f40 100644 --- a/modules/flow/actions/event_action.h +++ b/modules/flow/actions/event_action.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/actions/fallback_action.h b/modules/flow/actions/fallback_action.h index b2ec090..331b1aa 100644 --- a/modules/flow/actions/fallback_action.h +++ b/modules/flow/actions/fallback_action.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/actions/if_else_action.h b/modules/flow/actions/if_else_action.h index a0e4d37..4000765 100644 --- a/modules/flow/actions/if_else_action.h +++ b/modules/flow/actions/if_else_action.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/actions/invert_action.h b/modules/flow/actions/invert_action.h index 57a9d95..d15ff0b 100644 --- a/modules/flow/actions/invert_action.h +++ b/modules/flow/actions/invert_action.h @@ -1,5 +1,5 @@ -#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" @@ -34,4 +34,4 @@ class InvertAction : public Action { } } -#endif //TBOX_ACTION_INVERT_H_20221022 +#endif //TBOX_FLOW_INVERT_H_20221022 diff --git a/modules/flow/actions/loop_action.h b/modules/flow/actions/loop_action.h index 4022990..58594b4 100644 --- a/modules/flow/actions/loop_action.h +++ b/modules/flow/actions/loop_action.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/actions/loop_if_action.h b/modules/flow/actions/loop_if_action.h index a34dd18..7b89e73 100644 --- a/modules/flow/actions/loop_if_action.h +++ b/modules/flow/actions/loop_if_action.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/actions/nondelay_action.h b/modules/flow/actions/nondelay_action.h index 0f4d6da..2d04423 100644 --- a/modules/flow/actions/nondelay_action.h +++ b/modules/flow/actions/nondelay_action.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/actions/parallel_action.h b/modules/flow/actions/parallel_action.h index 6114d1e..aa8542c 100644 --- a/modules/flow/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" @@ -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/flow/actions/sequence_action.h b/modules/flow/actions/sequence_action.h index a120b2d..1774f49 100644 --- a/modules/flow/actions/sequence_action.h +++ b/modules/flow/actions/sequence_action.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/actions/sleep_action.h b/modules/flow/actions/sleep_action.h index b55c7d3..edf1f64 100644 --- a/modules/flow/actions/sleep_action.h +++ b/modules/flow/actions/sleep_action.h @@ -1,5 +1,5 @@ -#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 @@ -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/flow/event.h b/modules/flow/event.h index 743807f..c33df6a 100644 --- a/modules/flow/event.h +++ b/modules/flow/event.h @@ -1,5 +1,5 @@ -#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 flow { @@ -22,4 +22,4 @@ struct Event { } } -#endif //TBOX_ACTION_EVENT_H_20221022 +#endif //TBOX_FLOW_EVENT_H_20221022 diff --git a/modules/flow/event_publisher.h b/modules/flow/event_publisher.h index 9bd671a..db13124 100644 --- a/modules/flow/event_publisher.h +++ b/modules/flow/event_publisher.h @@ -1,5 +1,5 @@ -#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" @@ -21,4 +21,4 @@ class EventPublisher { } } -#endif //TBOX_ACTION_EVENT_PUBLISHER_H_20221001 +#endif //TBOX_FLOW_EVENT_PUBLISHER_H_20221001 diff --git a/modules/flow/event_publisher_impl.h b/modules/flow/event_publisher_impl.h index c0bb219..a212519 100644 --- a/modules/flow/event_publisher_impl.h +++ b/modules/flow/event_publisher_impl.h @@ -1,5 +1,5 @@ -#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" @@ -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/flow/event_subscriber.h b/modules/flow/event_subscriber.h index 6694f73..ad74f7a 100644 --- a/modules/flow/event_subscriber.h +++ b/modules/flow/event_subscriber.h @@ -1,5 +1,5 @@ -#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" @@ -17,4 +17,4 @@ class EventSubscriber { } } -#endif //TBOX_ACTION_EVENT_SUBSCRIBER_H_20221001 +#endif //TBOX_FLOW_EVENT_SUBSCRIBER_H_20221001 diff --git a/modules/flow/state_machine.h b/modules/flow/state_machine.h index ae1c537..ce1cf3e 100644 --- a/modules/flow/state_machine.h +++ b/modules/flow/state_machine.h @@ -1,5 +1,5 @@ -#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" @@ -150,4 +150,4 @@ class StateMachine { } } -#endif //TBOX_STATE_MACHINE_H_20220320 +#endif //TBOX_FLOW_STATE_MACHINE_H_20220320 -- Gitee From f5ec6fdd688707b1e5c3e70be0c3258b73a05f0a Mon Sep 17 00:00:00 2001 From: Hevake Date: Sun, 11 Dec 2022 00:02:38 +0800 Subject: [PATCH 4/7] =?UTF-8?q?fix(mqtt):=20=E8=A7=A3=E5=86=B3MQTT?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E8=BF=87=E7=A8=8B=E4=B8=ADstop()=E5=B9=B6del?= =?UTF-8?q?ete=E5=AF=B9=E8=B1=A1=E4=BA=A7=E7=94=9F=E7=9A=84=E9=9D=9E?= =?UTF-8?q?=E6=B3=95=E5=9C=B0=E5=9D=80=E8=AE=BF=E9=97=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/mqtt/client.cpp | 45 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/modules/mqtt/client.cpp b/modules/mqtt/client.cpp index 5f3063e..746c9b1 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); } ); -- Gitee From 97b214816d8edd7db728f0fbc6186f3042bec038 Mon Sep 17 00:00:00 2001 From: Hevake Date: Sun, 11 Dec 2022 00:12:47 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=90=88=E5=B9=B6feature-Flow=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E6=9B=B4=E6=96=B0README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e69e534..8cfd0a1 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,动作模块,解决异步模式下的顺序性动作执行困难问题; +- 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`。 -- Gitee From 5bd9ae08e26c7d223725403887b7b9e59c2e2131 Mon Sep 17 00:00:00 2001 From: Hevake Date: Tue, 13 Dec 2022 06:51:21 +0800 Subject: [PATCH 6/7] =?UTF-8?q?fix:=E8=A7=A3=E5=86=B3=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/flow/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/flow/Makefile b/modules/flow/Makefile index 230c8ce..1f4db05 100644 --- a/modules/flow/Makefile +++ b/modules/flow/Makefile @@ -54,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 -- Gitee From 99388e1e3658aec5fb94fe1dce54ba0ead1a79d4 Mon Sep 17 00:00:00 2001 From: Hevake Date: Tue, 13 Dec 2022 07:18:21 +0800 Subject: [PATCH 7/7] =?UTF-8?q?feat:=E5=B0=86timer=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E4=B8=BAalarm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- config.mk | 2 +- examples/{timer => alarm}/Makefile | 0 .../cron_timer => alarm/cron_alarm}/Makefile | 4 +-- .../cron_timer => alarm/cron_alarm}/main.cpp | 4 +-- .../oneshot_alarm}/Makefile | 4 +-- .../oneshot_alarm}/main.cpp | 4 +-- .../weekly_alarm}/Makefile | 4 +-- .../weekly_alarm}/main.cpp | 4 +-- .../{timer => alarm}/3rd-party/ccronexpr.cpp | 0 .../{timer => alarm}/3rd-party/ccronexpr.h | 0 modules/{timer => alarm}/Makefile | 24 ++++++++--------- modules/{timer/timer.cpp => alarm/alarm.cpp} | 26 +++++++++---------- modules/{timer/timer.h => alarm/alarm.h} | 14 +++++----- .../cron_timer.cpp => alarm/cron_alarm.cpp} | 17 ++++++------ .../cron_timer.h => alarm/cron_alarm.h} | 22 ++++++++-------- .../oneshot_alarm.cpp} | 13 +++++----- .../oneshot_timer.h => alarm/oneshot_alarm.h} | 20 +++++++------- .../weekly_alarm.cpp} | 11 ++++---- .../weekly_timer.h => alarm/weekly_alarm.h} | 20 +++++++------- .../workday_alarm.cpp} | 15 +++++------ .../workday_timer.h => alarm/workday_alarm.h} | 16 ++++++------ modules/{timer => alarm}/workday_calendar.cpp | 26 +++++++++---------- modules/{timer => alarm}/workday_calendar.h | 18 ++++++------- .../workday_calendar_test.cpp | 2 +- 25 files changed, 134 insertions(+), 138 deletions(-) rename examples/{timer => alarm}/Makefile (100%) rename examples/{timer/cron_timer => alarm/cron_alarm}/Makefile (76%) rename examples/{timer/cron_timer => alarm/cron_alarm}/main.cpp (94%) rename examples/{timer/oneshot_timer => alarm/oneshot_alarm}/Makefile (75%) rename examples/{timer/oneshot_timer => alarm/oneshot_alarm}/main.cpp (94%) rename examples/{timer/weekly_timer => alarm/weekly_alarm}/Makefile (75%) rename examples/{timer/weekly_timer => alarm/weekly_alarm}/main.cpp (94%) rename modules/{timer => alarm}/3rd-party/ccronexpr.cpp (100%) rename modules/{timer => alarm}/3rd-party/ccronexpr.h (100%) rename modules/{timer => alarm}/Makefile (57%) rename modules/{timer/timer.cpp => alarm/alarm.cpp} (89%) rename modules/{timer/timer.h => alarm/alarm.h} (91%) rename modules/{timer/cron_timer.cpp => alarm/cron_alarm.cpp} (72%) rename modules/{timer/cron_timer.h => alarm/cron_alarm.h} (73%) rename modules/{timer/oneshot_timer.cpp => alarm/oneshot_alarm.cpp} (73%) rename modules/{timer/oneshot_timer.h => alarm/oneshot_alarm.h} (69%) rename modules/{timer/weekly_timer.cpp => alarm/weekly_alarm.cpp} (82%) rename modules/{timer/weekly_timer.h => alarm/weekly_alarm.h} (76%) rename modules/{timer/workday_timer.cpp => alarm/workday_alarm.cpp} (79%) rename modules/{timer/workday_timer.h => alarm/workday_alarm.h} (78%) rename modules/{timer => alarm}/workday_calendar.cpp (55%) rename modules/{timer => alarm}/workday_calendar.h (79%) rename modules/{timer => alarm}/workday_calendar_test.cpp (98%) diff --git a/README.md b/README.md index 8cfd0a1..b9acb16 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ cpp-tbox,全称: C++ Treasure Box,C++开发百宝箱,是基于事件的服 - mqtt,MQTT客户端库; - terminal, 终端,类似shell的命令终端,可实现运行时与程序进行命令交互; - main,应用程序框架,实现了完备的程序启动流程与框架,让开发者只需关心业务代码; -- timer,定时器模块,实现了4种常用的定时器:CRON定时器、单次定时器、星期定时器、工作日定时器; +- alarm,闹钟模块,实现了4种常用的闹钟:CRON闹钟、单次闹钟、星期循环闹钟、工作日闹钟; - flow,动作模块,解决异步模式下的顺序性动作执行困难问题; #### 外部库依赖 diff --git a/config.mk b/config.mk index 56b6844..1ac189a 100644 --- a/config.mk +++ b/config.mk @@ -10,4 +10,4 @@ MODULES += mqtt MODULES += terminal MODULES += main MODULES += flow -MODULES += timer +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 9de1724..5bb4268 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 129e297..42f699c 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 8a119c9..32104f1 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 f9b9323..6f9a007 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 caed9d7..a8100ef 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 109c7e7..f79623c 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 836fade..10042a9 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 406bd7e..11eb816 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 aeef0e7..8ee916f 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 3631663..fc6ae3f 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 2652487..9c1e90b 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 3e1fb37..adab44a 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 010cc38..6b8d736 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 bb76a41..e884ce6 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 86cd8ca..6de34ac 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 4fddf91..3f043a8 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 6d9db32..709e38a 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 565d83c..9912773 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 f9cc033..5f7432d 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 8747111..35f420d 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) { -- Gitee