This commit is contained in:
Timur A. Fatkhullin 2025-05-20 23:32:25 +03:00
parent f03e38466e
commit 4244f76041
2 changed files with 83 additions and 7 deletions

View File

@ -252,26 +252,96 @@ public:
class MccMountState
{
public:
MccMountState(MccMount& mount) : _mount(mount) {}
static constexpr std::string_view emptyIdentString{"EMPTY-STRING-MOUNT-STATE-IDENTIFICATOR"};
virtual ~MccMountState() = default;
public:
MccMountState(MccMount& mount, traits::mcc_input_char_range auto const& ident)
: _mount(mount), _ident({ident.begin(), ident.end()})
{
_mount.logTrace("Create MccMountState instance: {}", (void*)this);
if (!_ident.size()) {
_mount.logWarn("An empty input mount state identificator! Use of default value!");
_ident = emptyIdentString;
}
}
MccMountState(MccMount& mount, const char* ident) : MccMountState(mount, std::string_view(ident)) {}
MccMountState(MccMountState&&) = default; // movable
MccMountState& operator=(MccMountState&&) = default;
MccMountState(const MccMountState&) = delete; // non-copyable
MccMountState& operator=(const MccMountState&) = delete;
virtual ~MccMountState()
{
_mount.logTrace("Delete MccMountState instance: {}", (void)this);
};
std::string_view ident() const
{
return _ident;
}
void enter(this std::derived_from<MccMountState> auto&& self)
{
std::forward<decltype(self)>(self).enter();
std::forward<decltype(self)>(self).enterImpl();
}
void exit(std::derived_from<MccMountState> auto next_state)
requires std::movable<decltype(next_state)>
void stop(this std::derived_from<MccMountState> auto&& self)
{
_mount.setMountState(std::move(next_state));
std::forward<decltype(self)>(self).stopImpl();
}
void exit(this std::derived_from<MccMountState> auto&& self, std::derived_from<MccMountState> auto next_state)
{
std::forward<decltype(self)>(self).exitImpl(std::move(next_state));
}
protected:
MccMount& _mount;
std::string_view _ident;
void enterImpl()
{
_mount.logWarn("call no-op MccMountState::enterImpl()");
}
void stopImpl()
{
_mount.logWarn("call no-op MccMountState::stopImpl()");
}
void exitImpl(std::derived_from<MccMountState> auto next_state)
{
_mount.setMountState(std::move(next_state));
}
};
// default IDLE state
class MccMountStateIDLE : public MccMountState
{
public:
MccMountStateIDLE(MccMount& mount) : MccMountState(mount, "DEFAULT IDLE STATE") {}
private:
void enterImpl()
{
this->_mount.logInfo("Enter to defalut IDLE state");
}
void stopImpl()
{
this->_mount.logWarn("Ignore stop for default IDLE state!");
}
};
/* Constructors and destructor */
MccMount(traits::mcc_input_char_range auto const& logger_mark = "[MOUNT]",

View File

@ -188,4 +188,10 @@ protected:
std::function<void()> _stopFunc;
};
template <MccMountType MOUNT_TYPE, std::derived_from<MccMountConfig> CONFIG_TYPE = MccMountConfig>
class MccMountStateSlew1 : public MccMount<MOUNT_TYPE, CONFIG_TYPE>::MccMountState
{
};
} // namespace mcc