27#ifndef EASYNAV__TYPES__NAVSTATE_HPP_
28#define EASYNAV__TYPES__NAVSTATE_HPP_
31#include <unordered_map>
85 void set(
const std::string & key,
const T & value)
87 std::lock_guard<std::mutex> lock(mutex_);
88 auto it = values_.find(key);
90 if (it == values_.end()) {
91 values_[key] = std::make_shared<T>(value);
92 types_[key] =
typeid(T).hash_code();
94 if (types_[key] !=
typeid(T).hash_code()) {
95 throw std::runtime_error(
"Type mismatch in set for key: " + key);
98 auto ptr = std::static_pointer_cast<T>(it->second);
114 const T &
get(
const std::string & key)
const
116 std::lock_guard<std::mutex> lock(mutex_);
117 auto it = values_.find(key);
119 if (it == values_.end()) {
120 throw std::runtime_error(
"Key not found in get: " + key);
123 if (types_.at(key) !=
typeid(T).hash_code()) {
124 throw std::runtime_error(
"Type mismatch in get for key: " + key);
127 auto ptr = std::static_pointer_cast<T>(it->second);
134 bool has(
const std::string & key)
const
136 return values_.find(key) != values_.end();
142 using AnyPrinter = std::function<std::string(std::shared_ptr<void>)>;
154 auto wrapper = [printer](std::shared_ptr<void> base_ptr) -> std::string {
155 auto typed_ptr = std::static_pointer_cast<T>(base_ptr);
156 return printer(*typed_ptr);
158 type_printers_[
typeid(T).hash_code()] = wrapper;
169 std::stringstream ss;
170 for (
const auto & kv : values_) {
171 ss << kv.first <<
" = ";
172 auto ptr = kv.second;
174 auto type_it = types_.find(kv.first);
175 if (type_it != types_.end()) {
176 auto printer_it = type_printers_.find(type_it->second);
177 if (printer_it != type_printers_.end()) {
178 ss <<
"[" << ptr.get() <<
"] : " << printer_it->second(ptr);
180 ss <<
"[" << ptr.get() <<
"] : " << type_it->second <<
"]";
183 ss <<
"[" << ptr.get() <<
"] : unknown]";
199 int size = backtrace(array, 50);
200 char **strings = backtrace_symbols(array, size);
201 std::cerr <<
"\nStack trace:\n";
202 for (
int i = 0; i < size; ++i) {
203 std::cerr << strings[i] << std::endl;
205 std::cerr << std::endl;
222 mutable std::mutex mutex_;
225 mutable std::unordered_map<std::string, std::shared_ptr<void>> values_;
228 mutable std::unordered_map<std::string, size_t> types_;
231 static inline std::unordered_map<size_t, AnyPrinter> type_printers_;
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
std::function< std::string(std::shared_ptr< void >)> AnyPrinter
Type alias for a generic printer function.
Definition NavState.hpp:142
static void print_stacktrace()
Prints the current C++ stack trace to standard error.
Definition NavState.hpp:196
static void register_basic_printers()
Registers default string printers for basic types: int, float, double, std::string,...
Definition NavState.hpp:211
void set(const std::string &key, const T &value)
Stores a value of type T associated with the given key.
Definition NavState.hpp:85
static void register_printer(std::function< std::string(const T &)> printer)
Registers a printer for a given type.
Definition NavState.hpp:152
NavState()
Constructs an empty NavState and registers basic type printers.
Definition NavState.hpp:65
virtual ~NavState()=default
Destructor.
bool has(const std::string &key) const
Checks whether a key exists in the NavState.
Definition NavState.hpp:134
std::string debug_string() const
Dumps all keys and their values to a formatted string.
Definition NavState.hpp:167
Definition RTTFBuffer.hpp:30