parameters.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
31 
32 #include <boost/algorithm/string.hpp>
33 
35 
36 #include <iostream>
37 
38 namespace hector_pose_estimation {
39 
40 namespace {
41  struct null_deleter {
42  void operator()(void const *) const {}
43  };
44 }
45 
47  erase(parameter->key);
48  push_back(parameter);
49  return *this;
50 }
51 
53  for(ParameterList::const_iterator it = other.begin(); it != other.end(); ++it) push_back(*it);
54  return *this;
55 }
56 
57 ParameterList& ParameterList::add(Parameter& alias, const std::string& key) {
58  if (!key.empty()) alias.key = key;
59  return add(ParameterPtr(&alias, null_deleter()));
60 }
61 
62 ParameterList& ParameterList::copy(const std::string& prefix, ParameterList const& parameters) {
63  for(ParameterList::const_iterator it = parameters.begin(); it != parameters.end(); ++it) {
64  ParameterPtr copy((*it)->clone());
65  if (!copy) continue;
66  if (!prefix.empty()) copy->key = prefix + copy->key;
67  push_back(copy);
68  }
69  return *this;
70 }
71 
73  copy(std::string(), parameters);
74  return *this;
75 }
76 
77 ParameterPtr const& ParameterList::get(const std::string& key) const {
78  for(const_iterator it = begin(); it != end(); ++it) {
79  if ((*it)->key == key) {
80  return *it;
81  }
82  }
83  throw std::runtime_error("parameter not found");
84 }
85 
86 ParameterList::iterator ParameterList::erase(const std::string& key) {
87  iterator it = begin();
88  for(; it != end(); ++it) {
89  if ((*it)->key == key) return erase(it);
90  }
91  return it;
92 }
93 
94 template <typename T>
96 {
97  bool operator()(const ParameterPtr& parameter, ros::NodeHandle& nh, bool set_all = false) {
98  try {
99  ParameterT<T> p(*parameter);
100  std::string param_key(boost::algorithm::to_lower_copy(parameter->key));
101  if (!nh.getParam(param_key, p.value())) {
102  if (set_all) {
103  nh.setParam(param_key, p.value());
104  ROS_DEBUG_STREAM("Registered parameter " << param_key << " with new value " << p.value());
105  }
106  } else {
107  ROS_DEBUG_STREAM("Found parameter " << param_key << " with value " << p.value());
108  }
109  return true;
110  } catch(std::bad_cast&) {
111  return false;
112  }
113  }
114 };
115 
116 template <>
118 {
119  bool operator()(const ParameterPtr& parameter, ros::NodeHandle& nh, bool set_all = false) {
120  try {
121  ParameterT<ColumnVector> p(*parameter);
122  std::string param_key(boost::algorithm::to_lower_copy(parameter->key));
123  XmlRpc::XmlRpcValue vector;
124  if (!nh.getParam(param_key, vector)) {
125  if (set_all) {
127  ROS_DEBUG_STREAM("Not registered vector parameter " << param_key << ". Using defaults.");
128  }
129  } else {
130  if (vector.getType() != XmlRpc::XmlRpcValue::TypeArray) {
131  ROS_WARN_STREAM("Found parameter " << param_key << ", but it's not an array!");
132  return false;
133  }
134  p.value().resize(vector.size());
135  for(int i = 0; i < vector.size(); ++i) p.value()[i] = vector[i];
136  ROS_DEBUG_STREAM("Found parameter " << param_key << " with value " << p.value());
137  }
138  return true;
139  } catch(std::bad_cast&) {
140  return false;
141  }
142  }
143 };
144 
145 template <typename T>
146 std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector) {
147  os << "[";
148  for(typename std::vector<T>::const_iterator it = vector.begin(); it != vector.end(); ++it) {
149  if (it != vector.begin()) os << ", ";
150  os << *it;
151  }
152  os << "]";
153  return os;
154 }
155 
156 template <typename T>
157 struct ParameterRegistryROS::Handler< std::vector<T> >
158 {
159  bool operator()(const ParameterPtr& parameter, ros::NodeHandle& nh, bool set_all = false) {
160  try {
161  ParameterT< std::vector<T> > p(*parameter);
162  std::string param_key(boost::algorithm::to_lower_copy(parameter->key));
163  XmlRpc::XmlRpcValue vector;
164  if (!nh.getParam(param_key, vector)) {
165  if (set_all) {
167  ROS_DEBUG_STREAM("Not registered vector parameter " << param_key << ". Using defaults.");
168  }
169  } else {
170  if (vector.getType() != XmlRpc::XmlRpcValue::TypeArray) {
171  ROS_WARN_STREAM("Found parameter " << param_key << ", but it's not an array!");
172  return false;
173  }
174  p.value().resize(vector.size());
175  for(int i = 0; i < vector.size(); ++i) p.value()[i] = vector[i];
176  ROS_DEBUG_STREAM("Found parameter " << param_key << " with value " << p.value());
177  }
178  return true;
179  } catch(std::bad_cast&) {
180  return false;
181  }
182  }
183 };
184 
186  : nh_(nh)
187  , set_all_(false)
188 {
189  nh_.getParam("set_all_parameters", set_all_);
190 }
191 
193  // call initialize recursively for ParameterList parameters
194  if (parameter->hasType<ParameterList>()) {
195  ParameterList with_prefix;
196  with_prefix.copy(parameter->key + "/", parameter->as<ParameterList>());
197  with_prefix.initialize(*this);
198  return;
199  }
200 
201  ROS_DEBUG_STREAM("Registering ROS parameter " << parameter->key);
202 
203  if (Handler<std::string>()(parameter, nh_, set_all_) ||
204  Handler<double>()(parameter, nh_, set_all_) ||
205  Handler<std::vector<double> >()(parameter, nh_, set_all_) ||
206  Handler<int>()(parameter, nh_, set_all_) ||
207  Handler<bool>()(parameter, nh_, set_all_) ||
208  Handler<ColumnVector>()(parameter, nh_, set_all_)
209  ) {
210  return;
211  }
212 
213  ROS_ERROR("Parameter %s has unknown type %s!", parameter->key.c_str(), parameter->type());
214 }
215 
217  for(const_iterator it = begin(); it != end(); ++it) {
218  const ParameterPtr& parameter = *it;
219  if (parameter->empty()) continue;
220  if (parameter->isAlias()) continue;
221 
222  func(*it);
223  }
224 }
225 
226 } // namespace hector_pose_estimation
bool operator()(const ParameterPtr &parameter, ros::NodeHandle &nh, bool set_all=false)
Definition: parameters.cpp:159
ParameterPtr const & get(const std::string &key) const
Definition: parameters.cpp:77
int size() const
Type const & getType() const
bool add(const actionlib::TwoIntsGoal &req, actionlib::TwoIntsResult &res)
bool operator()(const ParameterPtr &parameter, ros::NodeHandle &nh, bool set_all=false)
Definition: parameters.cpp:97
ParameterList & add(const std::string &key, T &value, const T &default_value)
Definition: parameters.h:148
ColumnVector_< Dynamic >::type ColumnVector
Definition: matrix.h:58
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
iterator erase(const std::string &key)
Definition: parameters.cpp:86
bool getParam(const std::string &key, std::string &s) const
bool operator()(const ParameterPtr &parameter, ros::NodeHandle &nh, bool set_all=false)
Definition: parameters.cpp:119
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
boost::function< void(ParameterPtr)> ParameterRegisterFunc
Definition: parameters.h:44
boost::shared_ptr< Parameter > ParameterPtr
Definition: parameters.h:41
ParameterList & copy(const std::string &prefix, ParameterList const &parameters)
Definition: parameters.cpp:62
void initialize(ParameterRegisterFunc func) const
Definition: parameters.cpp:216


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Thu Feb 18 2021 03:29:30