xmlrpc_helpers.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (Modified BSD License)
3  *
4  * Copyright (c) 2013, PAL Robotics, S.L.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PAL Robotics, S.L. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
37 #ifndef XMLRPCHELPERS_H
38 #define XMLRPCHELPERS_H
39 
40 #include <sstream>
41 #include <ros/ros.h>
42 
43 namespace xh
44 {
45 
47 {
48 public:
49  XmlrpcHelperException(const std::string& what)
50  : ros::Exception(what) {}
51 };
52 
55 
56 template <class T>
57 void fetchParam(ros::NodeHandle nh, const std::string& param_name, T& output)
58 {
60  if (!nh.getParamCached(param_name, val))
61  {
62  std::ostringstream err_msg;
63  err_msg << "could not load parameter '" << param_name << "'. (namespace: "
64  << nh.getNamespace() << ")";
65  throw XmlrpcHelperException(err_msg.str());
66  }
67 
68  output = static_cast<T>(val);
69 }
70 
71 void checkArrayItem(const Array& col, int index)
72 {
74  throw XmlrpcHelperException("not an array");
75  if(index >= col.size())
76  {
77  std::ostringstream err_msg;
78  err_msg << "index '" << index << "' is over array capacity";
79  throw XmlrpcHelperException(err_msg.str());
80  }
81 }
82 
83 void checkStructMember(const Struct& col, const std::string& member)
84 {
86  throw XmlrpcHelperException("not a struct");
87  if (!col.hasMember(member))
88  {
89  std::ostringstream err_msg;
90  err_msg << "could not find member '" << member << "'";
91  throw XmlrpcHelperException(err_msg.str());
92  }
93 }
94 
95 template <class T>
96 void getArrayItem(Array& col, int index, T& output) // XXX: XmlRpcValue::operator[] is not const
97 {
98  checkArrayItem(col, index);
99  output = static_cast<T>(col[index]);
100 }
101 
102 template <class T>
103 void getStructMember(Struct& col, const std::string& member, T& output)
104 {
105  checkStructMember(col, member);
106  output = static_cast<T>(col[member]);
107 }
108 
109 } // namespace xh
110 
111 #endif // XMLRPCHELPERS_H
XmlRpc::XmlRpcValue Array
XmlRpc::XmlRpcValue Struct
void getStructMember(Struct &col, const std::string &member, T &output)
void fetchParam(ros::NodeHandle nh, const std::string &param_name, T &output)
void checkArrayItem(const Array &col, int index)
Type const & getType() const
Exception(const std::string &what)
XmlrpcHelperException(const std::string &what)
void getArrayItem(Array &col, int index, T &output)
bool getParamCached(const std::string &key, std::string &s) const
const std::string & getNamespace() const
void checkStructMember(const Struct &col, const std::string &member)
bool hasMember(const std::string &name) const


twist_mux
Author(s): Enrique Fernandez , Siegfried-A. Gevatter Pujals
autogenerated on Wed Oct 26 2022 02:14:17