Easy Navigation
Loading...
Searching...
No Matches
NavState Class Reference

A generic, type-safe, lock-free blackboard to hold runtime state. More...

#include <NavState.hpp>

Public Types

using AnyPrinter = std::function<std::string(std::shared_ptr<void>)>
 Type alias for a generic printer function.
 

Public Member Functions

std::string debug_string () const
 Dumps all keys and their values to a formatted string.
 
template<typename T>
const T & get (const std::string &key) const
 Retrieves a const reference to the value of type T associated with the given key.
 
bool has (const std::string &key) const
 Checks whether a key exists in the NavState.
 
 NavState ()
 Constructs an empty NavState and registers basic type printers.
 
template<typename T>
void set (const std::string &key, const T &value)
 Stores a value of type T associated with the given key.
 
virtual ~NavState ()=default
 Destructor.
 

Static Public Member Functions

static void print_stacktrace ()
 Prints the current C++ stack trace to standard error.
 
static void register_basic_printers ()
 Registers default string printers for basic types: int, float, double, std::string, bool, char.
 
template<typename T>
static void register_printer (std::function< std::string(const T &)> printer)
 Registers a printer for a given type.
 

Detailed Description

A generic, type-safe, lock-free blackboard to hold runtime state.

NavState provides:

  • Type-erased storage using std::shared_ptr<void>.
  • Runtime type verification and safe casting via typeid.
  • Support for raw, shared, and copy-based insertion.
  • Debug utilities including stack trace and introspection.

Example usage:

NavState state;
state.set("goal_reached", false);
bool reached = state.get<bool>("goal_reached");
const T & get(const std::string &key) const
Retrieves a const reference to the value of type T associated with the given key.
Definition NavState.hpp:114
void set(const std::string &key, const T &value)
Stores a value of type T associated with the given key.
Definition NavState.hpp:85
NavState()
Constructs an empty NavState and registers basic type printers.
Definition NavState.hpp:65

Member Typedef Documentation

◆ AnyPrinter

using AnyPrinter = std::function<std::string(std::shared_ptr<void>)>

Type alias for a generic printer function.

Used to print debug output for stored values.

Constructor & Destructor Documentation

◆ NavState()

NavState ( )

Constructs an empty NavState and registers basic type printers.

◆ ~NavState()

virtual ~NavState ( )
virtualdefault

Destructor.

Member Function Documentation

◆ debug_string()

std::string debug_string ( ) const

Dumps all keys and their values to a formatted string.

If a printer is registered for a given type, it is used; otherwise, the raw pointer address and hash are shown.

Returns
String representation of current state.

◆ get()

template<typename T>
const T & get ( const std::string & key) const

Retrieves a const reference to the value of type T associated with the given key.

The reference points to the value managed internally through a shared_ptr<T>. This avoids unnecessary copies, but care must be taken not to hold the reference beyond the lifetime of the NavState instance.

Template Parameters
TThe expected type of the stored value.
Parameters
keyThe key of the value to retrieve.
Returns
const T& A const reference to the stored value.
Exceptions
std::runtime_errorif the key is not found or there is a type mismatch.

◆ has()

bool has ( const std::string & key) const

Checks whether a key exists in the NavState.

Parameters
keyLookup key.
Returns
True if the key is registered.

◆ print_stacktrace()

static void print_stacktrace ( )
static

Prints the current C++ stack trace to standard error.

Used to assist debugging in exception contexts.

◆ register_basic_printers()

static void register_basic_printers ( )
static

Registers default string printers for basic types: int, float, double, std::string, bool, char.

◆ register_printer()

template<typename T>
static void register_printer ( std::function< std::string(const T &)> printer)
static

Registers a printer for a given type.

The function will be used to convert values of this type into strings for use in debug_string().

Template Parameters
TType to register.
Parameters
printerFunction that converts a const reference to string.

◆ set()

template<typename T>
void set ( const std::string & key,
const T & value )

Stores a value of type T associated with the given key.

If the key does not exist, a new shared_ptr<T> is created and stored. If the key already exists, the stored value is updated in-place.

The value is internally managed through a shared_ptr<T>.

Template Parameters
TThe type of the value to store. Must be copy-assignable.
Parameters
keyThe key associated with the value.
valueThe value to store.
Exceptions
std::runtime_errorif there is a type mismatch with an existing key.

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