8#ifndef RUSH_ROS_PARAMETER_MANAGER_HPP
9#define RUSH_ROS_PARAMETER_MANAGER_HPP
17#include <ros/this_node.h>
24#include <xmlrpcpp/XmlRpcValue.h>
34 explicit ParamValue(
const XmlRpc::XmlRpcValue &v) : value_{v} {};
43 return static_cast<T
>(value_);
53 x =
static_cast<T
>(value_);
79 while(i < value_.size()) {
80 x.push_back(
static_cast<T
>(value_[i++]));
96 XmlRpc::XmlRpcValue value_;
121 if(std::map<std::string, ParamValue>::count(key) < 1) {
122 throw std::runtime_error(
"Key " + key +
" not found");
124 return std::map<std::string, ParamValue>::operator[](key);
131 void load(std::string ns =
"") {
132 std::this_thread::sleep_for(std::chrono::milliseconds(1));
135 std::vector<std::string> names;
136 XmlRpc::XmlRpcValue v;
137 if(ns.empty() || ns.back() !=
'/') {
140 if(ns.front() !=
'/') {
141 ns = ::ros::this_node::getNamespace() + ns;
144 ::ros::param::getParamNames(names);
145 names.erase(std::remove_if(names.begin(), names.end(), [&ns](
const std::string &s) { return static_cast<bool>(s.compare(0, ns.size(), ns)); }), names.end());
147 for(
const std::string &n : names) {
148 ::ros::param::get(n, v);
149 const std::string key = n.substr(ns.size());
150 if(std::map<std::string, ParamValue>::count(key) < 1) {
151 std::map<std::string, ParamValue>::emplace(std::make_pair(key, v));
153 std::map<std::string, ParamValue>::operator[](key) =
ParamValue(v);
162 std::map<std::string, ParamValue>::clear();
163 for(
const std::string &ns : ns_) {
172 [[nodiscard]] std::vector<std::string>
getKeys()
const {
176 std::vector<std::string> keys;
177 keys.reserve(this->size());
178 std::transform(this->begin(), this->end(), std::back_inserter(keys), [](
const auto &pair) {
return pair.first; });
183 std::set<std::string> ns_;
This class represents a map of parameter names to ParamValue objects.
Definition ros-parameter-manager.hpp:102
ParamMapper(const std::string &ns)
Constructor that loads parameters from a specified namespace.
Definition ros-parameter-manager.hpp:110
std::vector< std::string > getKeys() const
Get a vector of parameter names in the map.
Definition ros-parameter-manager.hpp:172
void reload()
Reload parameters from all stored namespaces.
Definition ros-parameter-manager.hpp:161
ParamValue & operator[](const std::string &key)
Overloaded subscript operator to access a parameter by name.
Definition ros-parameter-manager.hpp:120
void load(std::string ns="")
Load parameters from a specified namespace.
Definition ros-parameter-manager.hpp:131
This class wraps an XmlRpcValue for convenience.
Definition ros-parameter-manager.hpp:31
std::vector< T > as_vec()
Convert the stored value to a vector of a specific type.
Definition ros-parameter-manager.hpp:62
friend std::ostream & operator<<(std::ostream &os, const ParamValue &obj)
Overloaded stream insertion operator to print the stored value.
Definition ros-parameter-manager.hpp:90
void as_vec(std::vector< T > &x)
Convert the stored value to a vector of a specific type and store it in the given vector.
Definition ros-parameter-manager.hpp:74
void as(T &x)
Convert the stored value to a specific type and store it in the given variable.
Definition ros-parameter-manager.hpp:52
T as()
Convert the stored value to a specific type.
Definition ros-parameter-manager.hpp:42