rosparam_utils.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
38 
39 namespace jsk_topic_tools
40 {
42  {
43  switch(val.getType())
44  {
46  {
47  return (double)((int)val);
48  }
50  {
51  return (double)val;
52  }
53  default:
54  {
55  ROS_ERROR_STREAM("the value cannot be converted into double: " << val);
56  throw std::runtime_error("the value cannot be converted into double");
57  }
58  }
59  }
60 
62  const std::string& param_name,
63  std::vector<double>& result)
64  {
65  if (nh.hasParam(param_name)) {
67  nh.param(param_name, v, v);
69  result.resize(v.size());
70  for (size_t i = 0; i < v.size(); i++) {
71  result[i] = getXMLDoubleValue(v[i]);
72  }
73  return true;
74  }
75  else {
76  return false;
77  }
78  }
79  else {
80  return false;
81  }
82  }
83 
85  const std::string& param_name,
86  std::vector<std::vector<double> >& result)
87  {
88  if (nh.hasParam(param_name)) {
89  XmlRpc::XmlRpcValue v_toplevel;
90  nh.param(param_name, v_toplevel, v_toplevel);
91  if (v_toplevel.getType() == XmlRpc::XmlRpcValue::TypeArray) {
92  result.resize(v_toplevel.size());
93  for (size_t i = 0; i < v_toplevel.size(); i++) {
94  // ensure v[i] is an array
95  XmlRpc::XmlRpcValue nested_v = v_toplevel[i];
96  if (nested_v.getType() == XmlRpc::XmlRpcValue::TypeArray) {
97  std::vector<double> nested_std_vector(nested_v.size());
98  for (size_t j = 0; j < nested_v.size(); j++) {
99  nested_std_vector[j] = getXMLDoubleValue(nested_v[j]);
100  }
101  result[i] = nested_std_vector;
102  }
103  else {
104  return false;
105  }
106  }
107  return true;
108  }
109  else {
110  return false;
111  }
112  }
113  else {
114  return false;
115  }
116  }
117 
119  const std::string& param_name,
120  std::vector<std::vector<std::string> >& result)
121  {
122  if (nh.hasParam(param_name)) {
123  XmlRpc::XmlRpcValue v_toplevel;
124  nh.param(param_name, v_toplevel, v_toplevel);
125  if (v_toplevel.getType() == XmlRpc::XmlRpcValue::TypeArray) {
126  result.resize(v_toplevel.size());
127  for (size_t i = 0; i < v_toplevel.size(); i++) {
128  // ensure v[i] is an array
129  XmlRpc::XmlRpcValue nested_v = v_toplevel[i];
130  if (nested_v.getType() == XmlRpc::XmlRpcValue::TypeArray) {
131  std::vector<std::string> nested_std_vector(nested_v.size());
132  for (size_t j = 0; j < nested_v.size(); j++) {
133  if (nested_v[j].getType() == XmlRpc::XmlRpcValue::TypeString) {
134  nested_std_vector[j] = (std::string)nested_v[j];
135  }
136  else {
137  return false;
138  }
139  }
140  result[i] = nested_std_vector;
141  }
142  else {
143  return false;
144  }
145  }
146  return true;
147  }
148  else {
149  return false;
150  }
151  }
152  else {
153  return false;
154  }
155  }
156 
158  const std::string& param_name,
159  std::vector<std::string>& result)
160  {
161  if (nh.hasParam(param_name)) {
163  nh.param(param_name, v, v);
165  result.resize(v.size());
166  for (size_t i = 0; i < result.size(); i++) {
167  if (v[i].getType() == XmlRpc::XmlRpcValue::TypeString) {
168  result[i] = (std::string)v[i];
169  }
170  else {
171  throw std::runtime_error(
172  "the value cannot be converted into std::string");
173  }
174  }
175  return true;
176  }
177  else {
178  return false;
179  }
180  }
181  else {
182  return false;
183  }
184  }
185 
186 
187 }
int size() const
Type const & getType() const
double getXMLDoubleValue(XmlRpc::XmlRpcValue val)
bool readVectorParameter(ros::NodeHandle &nh, const std::string &param_name, std::vector< double > &result)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
bool hasParam(const std::string &key) const
#define ROS_ERROR_STREAM(args)


jsk_topic_tools
Author(s): Kei Okada , Yusuke Furuta
autogenerated on Tue Feb 6 2018 03:45:19