parameters.h
Go to the documentation of this file.
1 #ifndef SWRI_ROSCPP_PARAMETERS_H_
2 #define SWRI_ROSCPP_PARAMETERS_H_
3 
4 #include <algorithm>
5 #include <set>
6 #include <string>
7 #include <vector>
8 
9 #include <boost/bind.hpp>
10 
11 #include <ros/console.h>
12 #include <ros/node_handle.h>
13 
14 namespace swri
15 {
16 
19  static std::set<std::string> _used_params;
20 
21  static inline
22  bool getParam(const ros::NodeHandle &nh,
23  const std::string &name,
24  int &variable)
25  {
26  std::string resolved_name = nh.resolveName(name);
27  _used_params.insert(resolved_name);
28  if (!nh.getParam(name, variable))
29  {
30  ROS_ERROR("Required int parameter %s does not exist", name.c_str());
31  return false;
32  }
33  ROS_INFO("Read parameter %s = %d", name.c_str(), variable);
34  return true;
35  }
36 
37  static inline
38  bool getParam(const ros::NodeHandle &nh,
39  const std::string &name,
40  double &variable)
41  {
42  std::string resolved_name = nh.resolveName(name);
43  _used_params.insert(resolved_name);
44  if (!nh.getParam(name, variable))
45  {
46  ROS_ERROR("Required double parameter %s does not exist", name.c_str());
47  return false;
48  }
49  ROS_INFO("Read parameter %s = %lf", name.c_str(), variable);
50  return true;
51  }
52 
53  static inline
54  bool getParam(const ros::NodeHandle &nh,
55  const std::string &name,
56  float &variable)
57  {
58  double dbl_value;
59  if (!nh.getParam(name, dbl_value))
60  {
61  ROS_ERROR("Required double parameter %s does not exist", name.c_str());
62  return false;
63  }
64  variable = dbl_value;
65  ROS_INFO("Read parameter %s = %f", name.c_str(), variable);
66  return true;
67  }
68 
69  static inline
70  bool getParam(const ros::NodeHandle &nh,
71  const std::string &name,
72  std::string &variable)
73  {
74  std::string resolved_name = nh.resolveName(name);
75  _used_params.insert(resolved_name);
76  if (!nh.getParam(name, variable))
77  {
78  ROS_ERROR("Required string parameter %s does not exist", name.c_str());
79  return false;
80  }
81  ROS_INFO("Read parameter %s = %s", name.c_str(), variable.c_str());
82  return true;
83  }
84 
85  static inline
86  bool getParam(const ros::NodeHandle &nh,
87  const std::string &name,
88  bool &variable)
89  {
90  std::string resolved_name = nh.resolveName(name);
91  _used_params.insert(resolved_name);
92  if (!nh.getParam(name, variable))
93  {
94  ROS_ERROR("Required bool parameter %s does not exist", name.c_str());
95  return false;
96  }
97  ROS_INFO("Read parameter %s = %s", name.c_str(), variable ? "true" : "false");
98  return true;
99  }
100 
101  static inline
102  void param(const ros::NodeHandle &nh,
103  const std::string &name,
104  int &variable,
105  const int default_value)
106  {
107  std::string resolved_name = nh.resolveName(name);
108  _used_params.insert(resolved_name);
109  nh.param(name, variable, default_value);
110  ROS_INFO("Read parameter %s = %d", name.c_str(), variable);
111  }
112 
113  static inline
114  void param(const ros::NodeHandle &nh,
115  const std::string &name,
116  double &variable,
117  const double default_value)
118  {
119  std::string resolved_name = nh.resolveName(name);
120  _used_params.insert(resolved_name);
121  nh.param(name, variable, default_value);
122  ROS_INFO("Read parameter %s = %lf", name.c_str(), variable);
123  }
124 
125  static inline
126  void param(const ros::NodeHandle &nh,
127  const std::string &name,
128  float &variable,
129  const float default_value)
130  {
131  double dbl_value;
132  double dbl_default = default_value;
133  nh.param(name, dbl_value, dbl_default);
134  variable = dbl_value;
135  ROS_INFO("Read parameter %s = %f", name.c_str(), variable);
136  }
137 
138  static inline
139  void param(const ros::NodeHandle &nh,
140  const std::string &name,
141  std::string &variable,
142  const std::string default_value)
143  {
144  std::string resolved_name = nh.resolveName(name);
145  _used_params.insert(resolved_name);
146  nh.param(name, variable, default_value);
147  ROS_INFO("Read parameter %s = \"%s\"", name.c_str(), variable.c_str());
148  }
149 
150  static inline
151  void param(const ros::NodeHandle &nh,
152  const std::string &name,
153  bool &variable,
154  const bool default_value)
155  {
156  std::string resolved_name = nh.resolveName(name);
157  _used_params.insert(resolved_name);
158  nh.param(name, variable, default_value);
159  ROS_INFO("Read parameter %s = %s", name.c_str(), variable ? "true" : "false");
160  }
161 
172  inline int comparePrefix(std::string const& string, std::string const& prefix)
173  {
174  return string.compare(0, prefix.size(), prefix);
175  }
176 
186  inline bool isPrefixOf(std::string const& string, std::string const& prefix)
187  {
188  return comparePrefix(string, prefix) == 0;
189  }
190 
202  inline int prefixLessThan(std::string const& string, std::string const& prefix)
203  {
204  return comparePrefix(string, prefix) < 0;
205  }
206 
223  static inline
224  std::vector<std::string> getUnusedParamKeys(ros::NodeHandle const& n)
225  {
226  // Get a list of every parameter on the parameter server
227  std::string const& n_namespace = n.getNamespace();
228  std::vector<std::string> all_params;
229  n.getParamNames(all_params);
230 
231  // Sort the list of parameters to permit partitioning it by namespace
232  std::sort(all_params.begin(), all_params.end());
233 
234  // Partition the list of parameters to only the namespace of interest
235  // Create a functor that tells whether a parameter is in a namespace
236  boost::function<bool(std::string)> inNamespace = boost::bind(isPrefixOf, _1, n_namespace);
237  // Find the beginning of the namespace in the sorted list
238  std::vector<std::string>::const_iterator first_param_in_namespace = find_if(all_params.begin(), all_params.end(), inNamespace);
239  // Find the end of the namespace in the sorted list
240  std::vector<std::string>::const_iterator after_last_param_in_namespace = find_if(all_params.begin(), all_params.end(), std::not1(inNamespace));
241  // At this point, all parameters on the server that are in the namespace
242  // are in range [`first_param_in_namespace`,`after_last_param_in_namespace`)
243 
244  // Preallocate a vector to put unused params in.
245  std::vector<std::string> unused_params(all_params.size());
246 
247  // Fill unused_params with the set difference (params in namespace) - (used params)
248  std::vector<std::string>::iterator last_unused_param = std::set_difference(
249  first_param_in_namespace, after_last_param_in_namespace, // Set 1
250  _used_params.begin(), _used_params.end(), // Set 2
251  unused_params.begin(), // Destination iterator
252  prefixLessThan); // Prefix comparator so that all sub-keys of a
253  // used parameter are also considered used
254 
255  // Shrink the unused_params vector to fit contents
256  unused_params.resize(last_unused_param - unused_params.begin());
257 
258  return unused_params;
259  }
260 
266  static inline
268  {
269  std::vector<std::string> unused_params = getUnusedParamKeys(n);
270  for (std::vector<std::string>::const_iterator itr = unused_params.begin(); itr != unused_params.end(); ++itr)
271  {
272  ROS_WARN("Parameter %s was set, but not used by this node", itr->c_str());
273  }
274  }
275 } // namespace swri_param
276 #endif // SWRI_ROSCPP_PARAMETERS_H_
static std::set< std::string > _used_params
Definition: parameters.h:19
int prefixLessThan(std::string const &string, std::string const &prefix)
Definition: parameters.h:202
static std::vector< std::string > getUnusedParamKeys(ros::NodeHandle const &n)
Definition: parameters.h:224
int comparePrefix(std::string const &string, std::string const &prefix)
Definition: parameters.h:172
bool isPrefixOf(std::string const &string, std::string const &prefix)
Definition: parameters.h:186
std::string resolveName(const std::string &name, bool remap=true) const
#define ROS_WARN(...)
static void warnUnusedParams(ros::NodeHandle const &n)
Definition: parameters.h:267
static void param(const ros::NodeHandle &nh, const std::string &name, int &variable, const int default_value)
Definition: parameters.h:102
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
const std::string & getNamespace() const
bool getParamNames(std::vector< std::string > &keys) const
bool getParam(const std::string &key, std::string &s) const
static bool getParam(const ros::NodeHandle &nh, const std::string &name, int &variable)
Definition: parameters.h:22
#define ROS_ERROR(...)


swri_roscpp
Author(s):
autogenerated on Tue Apr 6 2021 02:50:35