RUSH
Reusable Utilities in Single Headers is a collection of header-only utilities for C++
Loading...
Searching...
No Matches
ros-parameter-manager.hpp
Go to the documentation of this file.
1
8#ifndef RUSH_ROS_PARAMETER_MANAGER_HPP
9#define RUSH_ROS_PARAMETER_MANAGER_HPP
10
11#include <algorithm>
12#include <chrono>
13#include <iosfwd>
14#include <iterator>
15#include <map>
16#include <ros/param.h>
17#include <ros/this_node.h>
18#include <set>
19#include <stdexcept>
20#include <string>
21#include <thread>
22#include <utility>
23#include <vector>
24#include <xmlrpcpp/XmlRpcValue.h>
25
26namespace rush::ros {
27
32public:
33 ParamValue() = default;
34 explicit ParamValue(const XmlRpc::XmlRpcValue &v) : value_{v} {};
35
41 template <typename T>
42 T as() {
43 return static_cast<T>(value_);
44 }
45
51 template <typename T>
52 void as(T &x) {
53 x = static_cast<T>(value_);
54 }
55
61 template <typename T>
62 std::vector<T> as_vec() {
63 std::vector<T> ret;
64 as_vec(ret);
65 return ret;
66 }
67
73 template <typename T>
74 void as_vec(std::vector<T> &x) {
75 if(!x.empty()) {
76 x.clear();
77 }
78 int i = 0;
79 while(i < value_.size()) {
80 x.push_back(static_cast<T>(value_[i++]));
81 }
82 }
83
90 friend std::ostream &operator<<(std::ostream &os, const ParamValue &obj) {
91 os << obj.value_;
92 return os;
93 }
94
95private:
96 XmlRpc::XmlRpcValue value_;
97};
98
102class ParamMapper : public std::map<std::string, ParamValue> {
103public:
104 ParamMapper() = default;
105
110 explicit ParamMapper(const std::string &ns) {
111 load(ns);
112 }
113
120 ParamValue &operator[](const std::string &key) {
121 if(std::map<std::string, ParamValue>::count(key) < 1) {
122 throw std::runtime_error("Key " + key + " not found");
123 }
124 return std::map<std::string, ParamValue>::operator[](key);
125 }
126
131 void load(std::string ns = "") {
132 std::this_thread::sleep_for(std::chrono::milliseconds(1));
133 ns_.insert(ns);
134
135 std::vector<std::string> names;
136 XmlRpc::XmlRpcValue v;
137 if(ns.empty() || ns.back() != '/') {
138 ns += '/';
139 }
140 if(ns.front() != '/') {
141 ns = ::ros::this_node::getNamespace() + ns;
142 }
143
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());
146
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));
152 } else {
153 std::map<std::string, ParamValue>::operator[](key) = ParamValue(v);
154 }
155 }
156 }
157
161 void reload() {
162 std::map<std::string, ParamValue>::clear();
163 for(const std::string &ns : ns_) {
164 load(ns);
165 }
166 }
167
172 [[nodiscard]] std::vector<std::string> getKeys() const {
173 if(this->empty()) {
174 return {};
175 }
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; });
179 return keys;
180 }
181
182private:
183 std::set<std::string> ns_;
184};
185
186} // namespace rush::ros
187
188#endif // RUSH_ROS_PARAMETER_MANAGER_HPP
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