QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LoopState< T > Class Template Reference

#include <LoopState.h>

+ Inheritance diagram for LoopState< T >:
+ Collaboration diagram for LoopState< T >:

Public Types

using ItemAction = std::function< void(const T &item, int index)>
 
using ItemPredicate = std::function< bool(const T &item, int index)>
 
- Public Types inherited from QGCAbstractState
using EntryCallback = std::function< void()>
 
using ExitCallback = std::function< void()>
 
using EventHandler = std::function< bool(QEvent *)>
 

Public Member Functions

 LoopState (const QString &stateName, QState *parent, const QList< T > &items, ItemAction action)
 
void setFilter (ItemPredicate filter)
 Set a predicate to filter items (only items where predicate returns true are processed)
 
int currentIndex () const
 Get the current index being processed.
 
currentItem () const
 Get the current item being processed.
 
int totalItems () const
 Get the total number of items.
 
int processedCount () const
 Get the number of items processed so far.
 
- 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 onEnter () override
 Override to perform actions on state entry.
 
- Protected Member Functions inherited from QGCAbstractState
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
 

Additional Inherited Members

- Signals inherited from QGCAbstractState
void advance ()
 
void error ()
 

Detailed Description

template<typename T>
class LoopState< T >

A state that iterates over a collection, executing an action for each item.

The state calls the iterator function for each item, emitting itemProcessed() after each. When all items are processed, it emits advance().

Example usage:

QStringList files = {"a.txt", "b.txt", "c.txt"};
auto* loopState = new LoopState<QString>("ProcessFiles", &machine, files,
[](const QString& file, int index) {
qDebug() << "Processing file" << index << ":" << file;
});
loopState->addTransition(loopState, &LoopState<QString>::advance, nextState);
QGCStateMachine * machine() const
Note
Template class — cannot use Q_OBJECT. Signals (advance, error) are inherited from QGCState. LoopState-specific signals are not possible.

Definition at line 26 of file LoopState.h.

Member Typedef Documentation

◆ ItemAction

template<typename T >
using LoopState< T >::ItemAction = std::function<void(const T& item, int index)>

Definition at line 29 of file LoopState.h.

◆ ItemPredicate

template<typename T >
using LoopState< T >::ItemPredicate = std::function<bool(const T& item, int index)>

Definition at line 30 of file LoopState.h.

Constructor & Destructor Documentation

◆ LoopState()

template<typename T >
LoopState< T >::LoopState ( const QString &  stateName,
QState *  parent,
const QList< T > &  items,
ItemAction  action 
)
inline

Create a LoopState

Parameters
stateNameName for logging
parentParent state
itemsCollection to iterate over
actionAction to execute for each item

Definition at line 37 of file LoopState.h.

Member Function Documentation

◆ currentIndex()

template<typename T >
int LoopState< T >::currentIndex ( ) const
inline

Get the current index being processed.

Definition at line 49 of file LoopState.h.

◆ currentItem()

template<typename T >
T LoopState< T >::currentItem ( ) const
inline

Get the current item being processed.

Definition at line 52 of file LoopState.h.

◆ onEnter()

template<typename T >
void LoopState< T >::onEnter ( )
inlineoverrideprotectedvirtual

Override to perform actions on state entry.

Reimplemented from QGCAbstractState.

Definition at line 62 of file LoopState.h.

◆ processedCount()

template<typename T >
int LoopState< T >::processedCount ( ) const
inline

Get the number of items processed so far.

Definition at line 59 of file LoopState.h.

◆ setFilter()

template<typename T >
void LoopState< T >::setFilter ( ItemPredicate  filter)
inline

Set a predicate to filter items (only items where predicate returns true are processed)

Definition at line 46 of file LoopState.h.

◆ totalItems()

template<typename T >
int LoopState< T >::totalItems ( ) const
inline

Get the total number of items.

Definition at line 56 of file LoopState.h.


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