14#ifndef ZYPP_NG_BASE_STATEMACHINE_INCLUDED_H
15#define ZYPP_NG_BASE_STATEMACHINE_INCLUDED_H
18#include <zypp-core/zyppng/base/Base>
38 template <
class State,
class Transitions >
43 template<
typename StateMachine >
55 static constexpr auto stateId = State::stateId;
56 static constexpr bool isFinal = State::isFinal;
60 auto keepAlive =
_ptr;
61 return keepAlive->enter( );
66 auto keepAlive =
_ptr;
67 return keepAlive->exit( );
80 std::shared_ptr<State>
_ptr;
87 template <
template<
typename...>
typename Templ ,
typename NewType,
typename TupleType,
bool condition >
90 template <
template<
typename...>
typename Templ,
typename NewType,
typename ...Types >
92 using Type = Templ<Types..., NewType>;
95 template <
template<
typename...>
typename Templ,
typename NewType,
typename ...Types >
97 using Type = Templ<Types...>;
104 template <
typename Variant,
typename Type,
template<
typename,
typename>
typename Compare = std::is_same,
size_t I = 0 >
107 if constexpr ( I >= std::variant_size_v<Variant> ) {
111 if (
Compare< std::variant_alternative_t< I, Variant>, Type >::value )
122 template<
class State,
class TupleSoFar,
class Head,
class ...Transitions >
128 template<
class State,
class TupleSoFar,
class Head >
133 template<
class State,
class ...Transitions >
141 template <
typename VariantSoFar,
typename Head,
typename ...Transitions>
148 template <
typename VariantSoFar,
typename Head>
154 template <
typename Head,
typename ...Transitions>
165 template <
typename A,
typename B>
166 struct is_same_state :
public std::is_same< typename A::StateType, typename B::StateType> {};
172 template <
typename State,
typename ...Transitions>
181 template <
typename VariantSoFar,
typename StateVariant,
typename ...Transitions>
184 template <
typename VariantSoFar,
typename HeadState,
typename ...State,
typename ...Transitions>
191 template <
typename VariantSoFar,
typename HeadState,
typename ...Transitions >
197 template <
typename NoState,
typename StateVariant,
typename ...Transitions>
200 template <
typename NoState,
typename HeadState,
typename ...States,
typename ...Transitions>
243 template<
typename Statemachine >
247 if constexpr ( std::is_member_function_pointer_v<OpType> ) {
249 }
else if constexpr ( std::is_null_pointer_v<OpType> ) {
250 return std::make_shared<Target>(sm);
258 if constexpr ( std::is_same_v<bool, CondType> ) {
260 }
else if constexpr ( std::is_member_function_pointer_v<CondType> ) {
363 template <
typename Derived,
typename StateId,
typename ...Transitions >
372 using FState =
typename StateSetHelper::FirstState;
393 template <
typename Func>
395 return std::visit( [ func = std::forward<Func>(f) ] (
auto &s ) {
397 if constexpr ( std::is_same_v< T, _InitialState > ) {
398 throw std::exception();
400 auto lock = s.wrappedState();
401 return ( func( *lock ) );
411 return std::visit( [](
const auto &s ) -> std::optional<StateId> {
413 if constexpr ( std::is_same_v< T, _InitialState > ) {
436 return std::get<WrappedEventType>(
_state ).wrappedState();
444 const std::shared_ptr<T>
state ()
const {
446 return std::get<WrappedEventType>(
_state ).wrappedState();
453 template <
typename NewState >
456 std::visit( [
this, &nS](
auto &currState ) {
458 if constexpr ( std::is_same_v< T, WrappedSType > ) {
460 }
else if constexpr ( std::is_same_v< T, _InitialState > ) {
463 enterState ( currState, WrappedSType( std::move(nS) ) );
487 template <
typename OldState,
typename NewState>
491 std::forward<OldState>(os).exit();
496 template <
typename NewState>
499 if constexpr ( !NewState::isFinal ) {
503 _state.template emplace<NewState>( std::forward<NewState>(nS) );
506 if constexpr ( NewState::isFinal ) {
515 std::get< std::decay_t<NewState> >(
_state ).enter();
525 template <
typename State,
typename Transition>
528 return [ mytrans = &transition,
this]()
mutable {
529 auto stateLock = std::get< std::decay_t<State> >(
_state).wrappedState();
530 if ( mytrans->checkCondition( *stateLock ) ) {
531 auto &st = std::get< std::decay_t<State> >(
_state);
532 enterState( st , WrappedEventType( (*mytrans)(
static_cast<Derived &
>(*
this), *stateLock ) ) );
537 template< std::size_t I = 0,
typename State,
typename ...StateTrans>
539 if constexpr (I >=
sizeof...(StateTrans)) {
542 auto &transition = std::get<I>( transitions );
569 template <
typename StatemachineType,
bool isFin >
599 template <
typename StatemachineType, auto sId,
bool isFin >
static auto connectFunc(typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)
StatemachineType & stateMachine()
BasicState(BasicState &&) noexcept=default
static constexpr bool isFinal
BasicState(StatemachineType &sm)
const StatemachineType & stateMachine() const
static constexpr auto stateId
This defines the actual StateMachine.
std::optional< StateId > previousState() const
typename detail::make_statewithtransition_set< _InitialState, AllStates, Transitions... > StateSetHelper
const std::shared_ptr< T > state() const
SignalProxy< void()> sigFinished()
auto makeEventCallback(Transition &transition)
void enterState(NewState &&nS)
Statemachine< Derived, StateId, Transitions... > StatemachineType
std::optional< StateId > currentState() const
Signal< void()> _sigFinished
void connectAllTransitions(State &&nS, std::tuple< StateTrans... > &transitions)
void forceState(std::unique_ptr< NewState > &&nS)
SignalProxy< void(StateId)> sigStateChanged()
std::vector< sigc::connection > _currentStateConnections
Signal< void(StateId)> _sigStateChanged
typename detail::make_state_set< Transitions... >::Type AllStates
typename StateSetHelper::FirstState FState
auto visitState(Func &&f)
void enterState(OldState &os, NewState &&nS)
std::shared_ptr< T > state()
std::optional< StateId > _previousState
typename StateSetHelper::Type StateSet
typename decay< T >::type decay_t
std::enable_if< std::is_member_pointer< typenamestd::decay< Functor >::type >::value, typenamestd::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
constexpr bool VariantHasType()
Constexpr function that evaluates to true if a variant type Variant already contains the type Type.
SignalProxy< void()>(T::*)() EventSource
constexpr std::nullptr_t DefaultStateTransition(nullptr)
constexpr bool DefaultStateCondition(true)
General compare functor returning -1, 0, 1.
SignalProxy< void() > eventSource(Source *st)
bool checkCondition(Source ¤tState)
auto eventAccessor() const
std::shared_ptr< Target > operator()(Statemachine &sm, Source &oldState)
static constexpr auto stateId
StateWithTransitions & operator=(StateWithTransitions &&other)=default
StateWithTransitions(std::shared_ptr< State > &&s)
std::shared_ptr< State > wrappedState()
std::shared_ptr< State > _ptr
StateWithTransitions(const StateWithTransitions &other)=delete
const std::shared_ptr< State > wrappedState() const
StateWithTransitions(StateWithTransitions &&other)=default
StateWithTransitions(StateMachine &sm)
static constexpr bool isFinal
Templ< Types..., NewType > Type
this adds the type NewType to the collection if the condition is true
typename add_type_to_collection< std::tuple, Head, TupleSoFar, std::is_same_v< State, typename Head::SourceType > >::Type Type
collect all transitions that have the same SourceState as the first type argument
typename add_type_to_collection< std::tuple, Head, TupleSoFar, std::is_same_v< State, typename Head::SourceType > >::Type NewTuple
typename collect_transitions_helper< State, NewTuple, Transitions... >::Type Type
typename collect_transitions_helper< State, std::tuple<>, Transitions... >::Type Type
Evaluates to true if type A and type B wrap the same State type.
typename add_type_to_collection< std::variant, typename Head::TargetType, WithSource, !VariantHasType< WithSource, typename Head::TargetType >() >::Type Type
typename add_type_to_collection< std::variant, typename Head::SourceType, VariantSoFar, !VariantHasType< VariantSoFar, typename Head::SourceType >() >::Type WithSource
Iterates over the list of Transitions and collects them all in a std::variant<State1,...
typename make_state_set_helper< WithTarget, Transitions... >::Type Type
typename add_type_to_collection< std::variant, typename Head::SourceType, VariantSoFar, !VariantHasType< VariantSoFar, typename Head::SourceType >() >::Type WithSource
typename add_type_to_collection< std::variant, typename Head::TargetType, WithSource, !VariantHasType< WithSource, typename Head::TargetType >() >::Type WithTarget
typename make_state_set_helper< VariantSoFar, Transitions... >::Type Type
typename add_type_to_collection< std::variant, typename Head::TargetType, InitialVariant, !VariantHasType< InitialVariant, typename Head::TargetType >() >::Type VariantSoFar
std::variant< typename Head::SourceType > InitialVariant
typename make_statewithtransition< HeadState, Transitions... >::Type FirstState
typename make_statewithtransition_set_helper< std::variant< NoState, FirstState >, std::variant< States... >, Transitions... >::Type Type
typename make_statewithtransition_set_helper< NewVariant, std::variant< State... >, Transitions... >::Type Type
typename make_statewithtransition< HeadState, Transitions... >::Type FullStateType
typename add_type_to_collection< std::variant, FullStateType, VariantSoFar, !VariantHasType< VariantSoFar, FullStateType >()>::Type NewVariant
typename add_type_to_collection< std::variant, FullStateType, VariantSoFar, !VariantHasType< VariantSoFar, FullStateType >()>::Type Type
typename make_statewithtransition< HeadState, Transitions... >::Type FullStateType
Iterates over each State in the StateVariant argument, collects the corresponding Transitions and com...
Turns a State type into its StateWithTransitions counterpart.
StateWithTransitions< State, typename collect_transitions< State, Transitions... >::Type > Type