From 8df846c2f45adb68be988c781ebc77e8f207497b Mon Sep 17 00:00:00 2001 From: Cecille Freeman Date: Wed, 23 Nov 2022 20:44:16 -0500 Subject: [PATCH] Fix warning on TestStateMachine This warning was bugging me, so I fixed it. That being said, this warning does point to a strange circular dependency between the transitions and the state machine where the state machine owns transitions, but transitions can post events to the state machine. It was abstracted out somewhat by the virtual Context interface, but that still leaves us with this circular dependency. A better solution would be to fix this circular dependency. One solution would be to have the transitions table either return a state or an event, and have the state machine dispatch to itself rather than having the transitions hold a reference back to the state machine. Then the transitions would only need to know about states and events. --- src/lib/support/tests/TestStateMachine.cpp | 29 ++++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/lib/support/tests/TestStateMachine.cpp b/src/lib/support/tests/TestStateMachine.cpp index da29ce8dbae121..cef020e019ff74 100644 --- a/src/lib/support/tests/TestStateMachine.cpp +++ b/src/lib/support/tests/TestStateMachine.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -64,28 +65,31 @@ struct BaseState void LogTransition(const char * previous) { mMock.LogTransition(previous); } const char * GetName() { return mName; } - chip::StateMachine::Context & mCtx; + Context * mCtx; const char * mName; MockState & mMock; }; struct State1 : public BaseState { - State1(Context & ctx, MockState & mock) : BaseState{ ctx, "State1", mock } {} + State1(Context * ctx, MockState & mock) : BaseState{ ctx, "State1", mock } {} }; struct State2 : public BaseState { - State2(Context & ctx, MockState & mock) : BaseState{ ctx, "State2", mock } {} + State2(Context * ctx, MockState & mock) : BaseState{ ctx, "State2", mock } {} }; struct State3 : public BaseState { - State3(Context & ctx, MockState & mock) : BaseState{ ctx, "State3", mock } {} + State3(Context * ctx, MockState & mock) : BaseState{ ctx, "State3", mock } {} void Enter() { BaseState::Enter(); - this->mCtx.Dispatch(Event::Create()); + if (this->mCtx) + { + this->mCtx->Dispatch(Event::Create()); + } } }; @@ -95,12 +99,12 @@ using State = chip::StateMachine::VariantState; struct StateFactory { - Context & mCtx; + Context * mCtx; MockState ms1{ 0, 0, 0, nullptr }; MockState ms2{ 0, 0, 0, nullptr }; MockState ms3{ 0, 0, 0, nullptr }; - StateFactory(Context & ctx) : mCtx(ctx) {} + StateFactory(Context * ctx) : mCtx(ctx) {} auto CreateState1() { return State::Create(mCtx, ms1); } auto CreateState2() { return State::Create(mCtx, ms2); } @@ -109,9 +113,9 @@ struct StateFactory struct Transitions { - Context & mCtx; + Context * mCtx; StateFactory mFactory; - Transitions(Context & ctx) : mCtx(ctx), mFactory(ctx) {} + Transitions(Context * ctx) : mCtx(ctx), mFactory(ctx) {} using OptState = chip::StateMachine::Optional; State GetInitState() { return mFactory.CreateState1(); } @@ -128,7 +132,10 @@ struct Transitions if (state.Is() && event.Is()) { // legal - Dispatches event without transition - mCtx.Dispatch(Event::Create()); + if (mCtx) + { + mCtx->Dispatch(Event::Create()); + } return {}; } if (state.Is() && event.Is()) @@ -155,7 +162,7 @@ class SimpleStateMachine Transitions mTransitions; chip::StateMachine::StateMachine mStateMachine; - SimpleStateMachine() : mTransitions(mStateMachine), mStateMachine(mTransitions) {} + SimpleStateMachine() : mTransitions(&mStateMachine), mStateMachine(mTransitions) {} ~SimpleStateMachine() {} };