xmlrpc_helpers.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (Modified BSD License)
00003  *
00004  *  Copyright (c) 2013, PAL Robotics, S.L.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of PAL Robotics, S.L. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00037 #ifndef XMLRPCHELPERS_H
00038 #define XMLRPCHELPERS_H
00039 
00040 #include <sstream>
00041 #include <ros/ros.h>
00042 
00043 namespace xh
00044 {
00045 
00046 class XmlrpcHelperException : public ros::Exception
00047 {
00048 public:
00049   XmlrpcHelperException(const std::string& what)
00050     : ros::Exception(what) {}
00051 };
00052 
00053 typedef XmlRpc::XmlRpcValue Struct;
00054 typedef XmlRpc::XmlRpcValue Array;
00055 
00056 template <class T>
00057 void fetchParam(ros::NodeHandle nh, const std::string& param_name, T& output)
00058 {
00059   XmlRpc::XmlRpcValue val;
00060   if (!nh.getParamCached(param_name, val))
00061   {
00062     std::ostringstream err_msg;
00063     err_msg << "could not load parameter '" << param_name << "'. (namespace: "
00064       << nh.getNamespace() << ")";
00065     throw XmlrpcHelperException(err_msg.str());
00066   }
00067 
00068   output = static_cast<T>(val);
00069 }
00070 
00071 void checkArrayItem(const Array& col, int index)
00072 {
00073   if (col.getType() != XmlRpc::XmlRpcValue::TypeArray)
00074     throw XmlrpcHelperException("not an array");
00075   if(index >= col.size())
00076   {
00077     std::ostringstream err_msg;
00078     err_msg << "index '" << index << "' is over array capacity";
00079     throw XmlrpcHelperException(err_msg.str());
00080   }
00081 }
00082 
00083 void checkStructMember(const Struct& col, const std::string& member)
00084 {
00085   if (col.getType() != XmlRpc::XmlRpcValue::TypeStruct)
00086     throw XmlrpcHelperException("not a struct");
00087   if (!col.hasMember(member))
00088   {
00089     std::ostringstream err_msg;
00090     err_msg << "could not find member '" << member << "'";
00091     throw XmlrpcHelperException(err_msg.str());
00092   }
00093 }
00094 
00095 template <class T>
00096 void getArrayItem(Array& col, int index, T& output) // XXX: XmlRpcValue::operator[] is not const
00097 {
00098   checkArrayItem(col, index);
00099   output = static_cast<T>(col[index]);
00100 }
00101 
00102 template <class T>
00103 void getStructMember(Struct& col, const std::string& member, T& output)
00104 {
00105   checkStructMember(col, member);
00106   output = static_cast<T>(col[member]);
00107 }
00108 
00109 } // namespace xh
00110 
00111 #endif // XMLRPCHELPERS_H


twist_mux
Author(s): Enrique Fernandez , Siegfried-A. Gevatter Pujals
autogenerated on Fri Aug 28 2015 13:25:02