From 64a3544bd859b462bf5a760b3c21db1d553c922b Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Mon, 16 Jun 2025 18:00:53 +0300 Subject: [PATCH] ... --- cxx/mcc_finite_state_machine.h | 10 +- cxx/mcc_mount_coord.h | 23 +---- cxx/mcc_mount_events_states.h | 170 ++++++++++++++++++++++----------- cxx/tests/coord.cpp | 8 +- 4 files changed, 132 insertions(+), 79 deletions(-) diff --git a/cxx/mcc_finite_state_machine.h b/cxx/mcc_finite_state_machine.h index 9a2cd9c..ebc5a97 100644 --- a/cxx/mcc_finite_state_machine.h +++ b/cxx/mcc_finite_state_machine.h @@ -490,12 +490,18 @@ public: } template - auto dispatchEvent(EvT&& event = EvT()) - requires std::default_initializable + auto dispatchEvent(EvT&& event) { return dispatchEvent(event); } + template + auto dispatchEvent() + requires std::default_initializable + { + return dispatchEvent(EvT{}); + } + std::string_view currentStateID() const { return _currentStateID; diff --git a/cxx/mcc_mount_coord.h b/cxx/mcc_mount_coord.h index 943f91f..672a0d1 100644 --- a/cxx/mcc_mount_coord.h +++ b/cxx/mcc_mount_coord.h @@ -271,7 +271,7 @@ public: // binary arithmetic operations template T1, std::convertible_to T2> -auto operator+(const T1& v1, const T2& v2) +static auto operator+(const T1& v1, const T2& v2) { static_assert(std::convertible_to || std::convertible_to, "INCOMPATIBLE TYPES!"); @@ -281,7 +281,7 @@ auto operator+(const T1& v1, const T2& v2) } template T1, std::convertible_to T2> -auto operator-(const T1& v1, const T2& v2) +static auto operator-(const T1& v1, const T2& v2) { static_assert(std::convertible_to || std::convertible_to, "INCOMPATIBLE TYPES!"); @@ -292,7 +292,7 @@ auto operator-(const T1& v1, const T2& v2) template T1, std::convertible_to T2> -auto operator*(const T1& v1, const T2& v2) +static auto operator*(const T1& v1, const T2& v2) { if constexpr (std::is_arithmetic_v) { return v2 *= v1; @@ -304,7 +304,7 @@ auto operator*(const T1& v1, const T2& v2) } template T1, typename T2> -auto operator/(const T1& v1, const T2& v2) +static auto operator/(const T1& v1, const T2& v2) requires std::is_arithmetic_v { return v1 /= v2; @@ -343,34 +343,19 @@ class MccAngleAZ : public MccAngle using MccAngle::MccAngle; }; -class MccAngleALT; // just forward declaration - class MccAngleZD : public MccAngle { public: using MccAngle::MccAngle; - - MccAngleZD(const MccAngleALT&); }; class MccAngleALT : public MccAngle { public: using MccAngle::MccAngle; - - MccAngleALT(const MccAngleZD& zd) - { - _angleInRads = std::numbers::pi / 2.0 - (double)zd; - } }; -MccAngleZD::MccAngleZD(const MccAngleALT& alt) -{ - _angleInRads = std::numbers::pi / 2.0 - (double)alt; -} - - class MccAngleX : public MccAngle // some co-longitude coordinate { using MccAngle::MccAngle; diff --git a/cxx/mcc_mount_events_states.h b/cxx/mcc_mount_events_states.h index 7b5cec7..e63636e 100644 --- a/cxx/mcc_mount_events_states.h +++ b/cxx/mcc_mount_events_states.h @@ -175,37 +175,53 @@ struct MccMountEventShutdown : public MccMountEventBase { template struct MccMountStateBase { + // template > EvT> + // void exit(this auto&& self, EvT& event) + // { + // using self_t = std::remove_cvref_t; + + // std::forward(self).exitImpl(event); + + // event.mount().logDebug("Exit from '{}' state due to '{}' event ...", self_t::ID, EvT::ID); + // } + + // template > EvT> + // void enter(this auto&& self, EvT& event) + // { + // using self_t = std::remove_cvref_t; + + // event.mount().logDebug("Enter to '{}' state due to '{}' event ...", self_t::ID, EvT::ID); + + // std::forward(self).enterImpl(event); + // } + +protected: + // template > EvT> + // void exitImpl(EvT& event) + // { + // event.mount().logWarn("Call an empty MccMountStateBase::exitImpl method!!! Event type is '{}'", EvT::ID); + // } + + // template > EvT> + // void enterImpl(EvT& event) + // { + // event.mount().logWarn("Call an empty MccMountStateBase::enterImpl method!!! Event type is '{}'", EvT::ID); + // } + + // MccMountStateBase() = default; + template > EvT> - void exit(this auto&& self, EvT& event) + void exitLog(this auto&& self, EvT& event) { using self_t = std::remove_cvref_t; - - std::forward(self).exitImpl(event); - event.mount().logDebug("Exit from '{}' state due to '{}' event ...", self_t::ID, EvT::ID); } template > EvT> - void enter(this auto&& self, EvT& event) + void enterLog(this auto&& self, EvT& event) { using self_t = std::remove_cvref_t; - event.mount().logDebug("Enter to '{}' state due to '{}' event ...", self_t::ID, EvT::ID); - - std::forward(self).enterImpl(event); - } - -protected: - template > EvT> - void exitImpl(EvT& event) - { - event.mount().logWarn("Call an empty MccMountStateBase::exitImpl method!!! Event type is '{}'", EvT::ID); - } - - template > EvT> - void enterImpl(EvT& event) - { - event.mount().logWarn("Call an empty MccMountStateBase::enterImpl method!!! Event type is '{}'", EvT::ID); } }; @@ -242,25 +258,30 @@ struct MccMountStateStop : MccMountStateBase { using transition_t = fsm::fsm_transition_table_t, MccMountStateIDLE>>; -protected: - void exitImpl(MccMountEventIDLE& event) + // protected: + void exit(MccMountEventIDLE& event) { // normal exit from the state + this->exitLog(event); } - // normal bihavior (transit to IDLE state after stopping) - void enterImpl(MccMountEventStop& event) - { - auto mount = event.mount(); - mount.stopMount(); + // normal behavior (transit to IDLE state after stopping) + // void enter(MccMountEventStop& event) + // { + // this->enterLog(event); - // switch to IDLE state - mount.template dispatchEvent>({mount}); - } + // auto mount = event.mount(); + // mount.stopMount(); + + // // switch to IDLE state + // mount.template dispatchEvent>({mount}); + // } template > EvT> - void enterImpl(EvT& event) + void enter(EvT& event) { + this->enterLog(event); + auto mount = event.mount(); mount.stopMount(); @@ -268,7 +289,7 @@ protected: if constexpr (std::same_as>) { mount.logInfo("Stop reason: {}", event.reason()); - // normal bihavior (transit to IDLE state after stopping) + // normal behavior (transit to IDLE state after stopping) mount.template dispatchEvent>({mount}); } else { mount.logInfo("Stop reason: special state"); @@ -285,15 +306,18 @@ struct MccMountStateInit : MccMountStateBase { using transition_t = fsm::fsm_transition_table_t, MccMountStateIDLE>>; -protected: - void exitImpl(MccMountEventIDLE& event) + // protected: + void exit(MccMountEventIDLE& event) { // normal exit from the state + this->exitLog(event); } template > EvT> - void enterImpl(EvT& event) + void enter(EvT& event) { + this->enterLog(event); + auto mount = event.mount(); mount.initMount(); @@ -318,33 +342,50 @@ struct MccMountStateError : MccMountStateBase { std::pair, MccMountStateError>, std::pair, MccMountStateShutdown>>; -protected: + // protected: template > EvT> - void exitImpl(EvT& event) + void exit(EvT& event) { + this->exitLog(event); + event.mount().logWarn( "The mount is in the error state!!! One must correct it before transit to any states! Event type is '{}'", EvT::ID); } - void exitImpl(MccMountEventIDLE& event) + void exit(MccMountEventIDLE& event) { - event.mount().logWarning("Suppose the error was corrected!"); + this->exitLog(event); + + event.mount().logWarn("Suppose the error was corrected!"); } - void exitImpl(MccMountEventInit& event) + void exit(MccMountEventInit& event) { // normal exit from the state + this->exitLog(event); } - template > EvT> - void enterImpl(EvT& event) + void enter(MccMountEventError& event) { + this->enterLog(event); + auto err = event.eventData(); event.mount().logError("The mount is in the error state: code = {}, category = {}, message = '{}'", err.value(), err.category().name(), err.message()); } + // template > EvT> + // void enter(EvT& event) + // { + // this->enterLog(event); + + // auto err = event.eventData(); + + // event.mount().logError("The mount is in the error state: code = {}, category = {}, message = '{}'", + // err.value(), + // err.category().name(), err.message()); + // } }; @@ -361,6 +402,18 @@ struct MccMountStateIDLE : MccMountStateBase { std::pair, MccMountStateGuiding>, std::pair, MccMountStateIDLE>, std::pair, MccMountStateShutdown>>; + + template > EvT> + void exit(EvT& event) + { + this->exitLog(event); + } + + template > EvT> + void enter(EvT& event) + { + this->enterLog(event); + } }; @@ -373,15 +426,18 @@ struct MccMountStateShutdown : MccMountStateBase { // only initialization using transition_t = fsm::fsm_transition_table_t, MccMountStateInit>>; -protected: - void exitImpl(MccMountEventInit& event) + // protected: + void exit(MccMountEventInit& event) { // normal exit from the state + this->exitLog(event); } template > EvT> - void enterImpl(EvT& event) + void enter(EvT& event) { + this->enterLog(event); + event.mount().shutdownMount(); } }; @@ -402,16 +458,19 @@ struct MccMountStateSlew : MccMountStateBase { std::pair, MccMountStateStop>, std::pair, MccMountStateStop>>; -protected: + // protected: template > EvT> - void exitImpl(EvT& event) + void exit(EvT& event) { + this->exitLog(event); } template > EvT> - void enterImpl(EvT& event) + void enter(EvT& event) { - event.mount().slewMount(/* params here ...*/); + this->enterLog(event); + + event.mount().slewMount(event.eventData()); } }; @@ -432,16 +491,19 @@ struct MccMountStateGuiding : MccMountStateBase { std::pair, MccMountStateStop>, std::pair, MccMountStateStop>>; -protected: + // protected: template > EvT> - void exitImpl(EvT& event) + void exit(EvT& event) { + this->exitLog(event); } template > EvT> - void enterImpl(EvT& event) + void enter(EvT& event) { - event.mount().slewMount(/* params here ...*/); + this->enterLog(event); + + event.mount().startGuiding(); } }; diff --git a/cxx/tests/coord.cpp b/cxx/tests/coord.cpp index cb75bd1..8e4f04a 100644 --- a/cxx/tests/coord.cpp +++ b/cxx/tests/coord.cpp @@ -16,11 +16,11 @@ int tests_coord(int argc, char* argv[]) std::cout << "ang_degs = " << ang.degrees() << "\n"; std::cout << "ang_rads = " << (float)ang << "\n"; - MccAngleALT alt(30.0_degs); - MccAngleZD zd(alt); + // MccAngleALT alt(30.0_degs); + // MccAngleZD zd(alt); - std::cout << "alt = " << alt.degrees() << "\n"; - std::cout << "zd (from alt) = " << zd.degrees() << "\n"; + // std::cout << "alt = " << alt.degrees() << "\n"; + // std::cout << "zd (from alt) = " << zd.degrees() << "\n"; std::cout << "\n";