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

#include <StateContext.h>

Public Member Functions

 StateContext ()=default
 
 ~StateContext ()=default
 
template<typename T >
void set (const QString &key, T &&value)
 
template<typename T >
std::optional< T > get (const QString &key) const
 
template<typename T >
getOr (const QString &key, const T &defaultValue) const
 
bool contains (const QString &key) const
 
template<typename T >
bool containsType (const QString &key) const
 
bool remove (const QString &key)
 
void clear ()
 Clear all stored values.
 
int count () const
 Get the number of stored values.
 
QStringList keys () const
 Get all keys in the context.
 
void setVariant (const QString &key, const QVariant &value)
 Store a QVariant value.
 
QVariant variant (const QString &key) const
 Retrieve a QVariant value.
 
bool containsVariant (const QString &key) const
 Check if a variant key exists.
 

Detailed Description

Type-safe context for passing data between states in a state machine.

StateContext provides a way for states to share data without tight coupling. Data is stored by key and can be retrieved with type checking.

Example usage:

// In producer state
context->set("userId", 42);
context->set("userName", QString("Alice"));
// In consumer state
auto userId = context->get<int>("userId");
if (userId) {
qDebug() << "User ID:" << *userId;
}

The context can also be accessed via QGCStateMachine::context().

Definition at line 30 of file StateContext.h.

Constructor & Destructor Documentation

◆ StateContext()

StateContext::StateContext ( )
default

◆ ~StateContext()

StateContext::~StateContext ( )
default

Member Function Documentation

◆ clear()

void StateContext::clear ( )
inline

Clear all stored values.

Definition at line 108 of file StateContext.h.

◆ contains()

bool StateContext::contains ( const QString &  key) const
inline

Check if a key exists in the context

Parameters
keyThe key to check
Returns
true if the key exists

Definition at line 81 of file StateContext.h.

◆ containsType()

template<typename T >
bool StateContext::containsType ( const QString &  key) const
inline

Check if a key exists and holds the specified type

Parameters
keyThe key to check
Returns
true if key exists and holds type T

Definition at line 90 of file StateContext.h.

◆ containsVariant()

bool StateContext::containsVariant ( const QString &  key) const
inline

Check if a variant key exists.

Definition at line 142 of file StateContext.h.

◆ count()

int StateContext::count ( ) const
inline

Get the number of stored values.

Definition at line 114 of file StateContext.h.

◆ get()

template<typename T >
std::optional< T > StateContext::get ( const QString &  key) const
inline

Retrieve a value by key with type checking

Parameters
keyThe key to look up
Returns
The value if found and type matches, std::nullopt otherwise

Definition at line 53 of file StateContext.h.

◆ getOr()

template<typename T >
T StateContext::getOr ( const QString &  key,
const T &  defaultValue 
) const
inline

Retrieve a value by key, returning a default if not found

Parameters
keyThe key to look up
defaultValueValue to return if key not found or type mismatch
Returns
The stored value or defaultValue

Definition at line 72 of file StateContext.h.

◆ keys()

QStringList StateContext::keys ( ) const
inline

Get all keys in the context.

Definition at line 120 of file StateContext.h.

◆ remove()

bool StateContext::remove ( const QString &  key)
inline

Remove a value by key

Parameters
keyThe key to remove
Returns
true if the key was found and removed

Definition at line 102 of file StateContext.h.

◆ set()

template<typename T >
void StateContext::set ( const QString &  key,
T &&  value 
)
inline

Store a value with the given key

Parameters
keyUnique identifier for the value
valueThe value to store

Definition at line 44 of file StateContext.h.

◆ setVariant()

void StateContext::setVariant ( const QString &  key,
const QVariant &  value 
)
inline

Store a QVariant value.

Definition at line 130 of file StateContext.h.

◆ variant()

QVariant StateContext::variant ( const QString &  key) const
inline

Retrieve a QVariant value.

Definition at line 136 of file StateContext.h.


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