QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SkippableAsyncState Class Reference

#include <SkippableAsyncState.h>

+ Inheritance diagram for SkippableAsyncState:
+ Collaboration diagram for SkippableAsyncState:

Public Types

using SkipPredicate = std::function< bool()>
 
using SetupFunction = std::function< void(SkippableAsyncState *state)>
 
using SkipAction = std::function< void()>
 
- Public Types inherited from QGCAbstractState
using EntryCallback = std::function< void()>
 
using ExitCallback = std::function< void()>
 
using EventHandler = std::function< bool(QEvent *)>
 

Signals

void skipped ()
 Emitted when skip predicate returns true and the state is skipped.
 
- Signals inherited from WaitStateBase
void completed ()
 
void timeout ()
 
void timedOut ()
 
- Signals inherited from QGCAbstractState
void advance ()
 
void error ()
 

Public Member Functions

 SkippableAsyncState (const QString &stateName, QState *parent, SkipPredicate skipPredicate, SetupFunction setupFunc, SkipAction skipAction=nullptr, int timeoutMsecs=0)
 
void complete ()
 Call this to signal that the async operation has completed successfully.
 
void fail ()
 Call this to signal that the async operation has failed.
 
template<typename Func >
void connectToCompletion (typename QtPrivate::FunctionPointer< Func >::Object *sender, Func signal)
 
template<typename Func , typename Slot >
void connectToCompletion (typename QtPrivate::FunctionPointer< Func >::Object *sender, Func signal, Slot slot)
 
void setSkipPredicate (SkipPredicate predicate)
 Set the skip predicate.
 
void setSetupFunction (SetupFunction setupFunc)
 Set the setup function.
 
void setSkipAction (SkipAction skipAction)
 Set the skip action.
 
- Public Member Functions inherited from WaitStateBase
 WaitStateBase (const QString &stateName, QState *parent, int timeoutMsecs=0)
 
void restartWait ()
 
- Public Member Functions inherited from QGCState
 QGCState (const QString &stateName, QState *parentState)
 
template<typename PointerToMemberFunction >
QSignalTransition * addThisTransition (PointerToMemberFunction signal, QAbstractState *target)
 Simpler version of QState::addTransition which assumes the sender is this.
 
void setLocalErrorState (QAbstractState *errorState)
 
QAbstractState * localErrorState () const
 Get the per-state error state (nullptr if using global)
 
void setProperty (QObject *object, const char *name, const QVariant &value)
 
void setEnabled (QObject *object, bool enabled)
 Convenience overload for setting enabled state on widgets/controls.
 
void setVisible (QObject *object, bool visible)
 Convenience overload for setting visible state on widgets/controls.
 
- Public Member Functions inherited from QGCAbstractState
 QGCAbstractState (const QString &stateName, QState *parent)
 
QGCStateMachinemachine () const
 
Vehiclevehicle () const
 
QString stateName () const
 
class StateContextcontext () const
 
void setOnEntry (EntryCallback callback)
 Set a callback to be invoked when the state is entered.
 
void setOnExit (ExitCallback callback)
 Set a callback to be invoked when the state is exited.
 
void setCallbacks (EntryCallback onEntry, ExitCallback onExit=nullptr)
 Set both entry and exit callbacks.
 
void setEventHandler (EventHandler handler)
 Set a custom event handler for this state.
 

Protected Member Functions

void connectWaitSignal () override
 Subclasses override to set up their signal connections.
 
void disconnectWaitSignal () override
 Subclasses override to tear down their signal connections.
 
void onWaitEntered () override
 Called when the state is entered - subclasses should call base implementation.
 
- Protected Member Functions inherited from WaitStateBase
virtual void onWaitExited ()
 Called when the state is exited - subclasses should call base implementation.
 
virtual void onWaitTimeout ()
 Called when the timeout expires - default emits timeout(), subclasses can override.
 
void waitComplete ()
 
void waitFailed ()
 
int timeoutMsecs () const
 
- Protected Member Functions inherited from QGCAbstractState
virtual void onEnter ()
 Override to perform actions on state entry.
 
virtual void onLeave ()
 Override to perform actions on state exit.
 
void onEntry (QEvent *event) override
 
void onExit (QEvent *event) override
 
bool event (QEvent *event) override
 

Detailed Description

Combines skip condition checking with async operation setup in a single state. Common pattern in initialization workflows where operations should be skipped under certain conditions.

On entry:

  1. Evaluate skipPredicate()
  2. If true: call skipAction() (if provided), emit skipped()
  3. If false: call setupFunc(this), wait for complete() or timeout

Example usage:

auto* missionState = new SkippableAsyncState(
"LoadMission",
parentState,
[this]() { return _shouldSkipForLinkType() || !_hasPrimaryLink(); },
[this](SkippableAsyncState* state) {
state->connectToCompletion(_missionManager, &MissionManager::newMissionItemsAvailable);
_missionManager->loadFromVehicle();
},
[this]() { qCDebug(Log) << "Skipping mission load"; }
);
// Both advance and skipped transition to same next state
missionState->addTransition(missionState, &QGCState::advance, nextState);
missionState->addTransition(missionState, &SkippableAsyncState::skipped, nextState);
void newMissionItemsAvailable(bool removeAllRequested)
void skipped()
Emitted when skip predicate returns true and the state is skipped.

Definition at line 32 of file SkippableAsyncState.h.

Member Typedef Documentation

◆ SetupFunction

using SkippableAsyncState::SetupFunction = std::function<void(SkippableAsyncState* state)>

Definition at line 39 of file SkippableAsyncState.h.

◆ SkipAction

using SkippableAsyncState::SkipAction = std::function<void()>

Definition at line 40 of file SkippableAsyncState.h.

◆ SkipPredicate

using SkippableAsyncState::SkipPredicate = std::function<bool()>

Definition at line 38 of file SkippableAsyncState.h.

Constructor & Destructor Documentation

◆ SkippableAsyncState()

SkippableAsyncState::SkippableAsyncState ( const QString &  stateName,
QState *  parent,
SkipPredicate  skipPredicate,
SetupFunction  setupFunc,
SkipAction  skipAction = nullptr,
int  timeoutMsecs = 0 
)
Parameters
stateNameName for this state (for logging)
parentParent state
skipPredicateIf true, skip the async operation and emit skipped()
setupFuncFunction called when state is entered and not skipped. Should start the async operation.
skipActionOptional function called when skipping (for cleanup or logging)
timeoutMsecsTimeout in milliseconds, 0 for no timeout

Definition at line 6 of file SkippableAsyncState.cc.

Member Function Documentation

◆ complete()

void SkippableAsyncState::complete ( )
inline

Call this to signal that the async operation has completed successfully.

Definition at line 57 of file SkippableAsyncState.h.

References WaitStateBase::waitComplete().

Referenced by connectToCompletion().

◆ connectToCompletion() [1/2]

template<typename Func >
void SkippableAsyncState::connectToCompletion ( typename QtPrivate::FunctionPointer< Func >::Object *  sender,
Func  signal 
)
inline

Connect to an external signal that will trigger completion

Parameters
senderThe QObject that will emit the completion signal
signalThe signal that indicates completion

Definition at line 66 of file SkippableAsyncState.h.

References complete().

◆ connectToCompletion() [2/2]

template<typename Func , typename Slot >
void SkippableAsyncState::connectToCompletion ( typename QtPrivate::FunctionPointer< Func >::Object *  sender,
Func  signal,
Slot  slot 
)
inline

Connect to an external signal that will trigger completion, with a slot Useful when you need to process the signal data before completing

Definition at line 76 of file SkippableAsyncState.h.

◆ connectWaitSignal()

void SkippableAsyncState::connectWaitSignal ( )
overrideprotectedvirtual

Subclasses override to set up their signal connections.

Implements WaitStateBase.

Definition at line 19 of file SkippableAsyncState.cc.

◆ disconnectWaitSignal()

void SkippableAsyncState::disconnectWaitSignal ( )
overrideprotectedvirtual

Subclasses override to tear down their signal connections.

Implements WaitStateBase.

Definition at line 24 of file SkippableAsyncState.cc.

◆ fail()

void SkippableAsyncState::fail ( )
inline

Call this to signal that the async operation has failed.

Definition at line 60 of file SkippableAsyncState.h.

References WaitStateBase::waitFailed().

◆ onWaitEntered()

void SkippableAsyncState::onWaitEntered ( )
overrideprotectedvirtual

Called when the state is entered - subclasses should call base implementation.

Reimplemented from WaitStateBase.

Definition at line 32 of file SkippableAsyncState.cc.

References skipped(), and QGCAbstractState::stateName().

◆ setSetupFunction()

void SkippableAsyncState::setSetupFunction ( SetupFunction  setupFunc)
inline

Set the setup function.

Definition at line 85 of file SkippableAsyncState.h.

◆ setSkipAction()

void SkippableAsyncState::setSkipAction ( SkipAction  skipAction)
inline

Set the skip action.

Definition at line 88 of file SkippableAsyncState.h.

◆ setSkipPredicate()

void SkippableAsyncState::setSkipPredicate ( SkipPredicate  predicate)
inline

Set the skip predicate.

Definition at line 82 of file SkippableAsyncState.h.

◆ skipped

void SkippableAsyncState::skipped ( )
signal

Emitted when skip predicate returns true and the state is skipped.

Referenced by onWaitEntered().


The documentation for this class was generated from the following files: