Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
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) 
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 } 
00110 
00111 #endif // XMLRPCHELPERS_H