Created
August 28, 2013 05:06
-
-
Save b-adkins/6362327 to your computer and use it in GitHub Desktop.
Proposed method templates to get vectors and maps from the ROS param server, to be added to <ros/param.h>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file | |
* | |
* Proposed method templates to get vectors and maps from the ROS param server, to be added to <ros/param.h>. | |
* | |
* @date Aug 2, 2013 | |
* @author Bea Adkins | |
*/ | |
#ifndef ROSCPP_PARAM_PATCH_H_ | |
#define ROSCPP_PARAM_PATCH_H_ | |
#include "ros/param.h" | |
namespace rospatch | |
{ | |
namespace param | |
{ | |
template<typename T> bool isXmlRpcValueType(XmlRpc::XmlRpcValue& t); | |
template<> bool isXmlRpcValueType<std::string>(XmlRpc::XmlRpcValue& v) | |
{ | |
return v.getType() == XmlRpc::XmlRpcValue::TypeString; | |
} | |
template<> bool isXmlRpcValueType<int>(XmlRpc::XmlRpcValue& v) | |
{ | |
return v.getType() == XmlRpc::XmlRpcValue::TypeInt; | |
} | |
template<> bool isXmlRpcValueType<double>(XmlRpc::XmlRpcValue& v) | |
{ | |
return v.getType() == XmlRpc::XmlRpcValue::TypeDouble; | |
} | |
template<> bool isXmlRpcValueType<bool>(XmlRpc::XmlRpcValue& v) | |
{ | |
return v.getType() == XmlRpc::XmlRpcValue::TypeBoolean; | |
} | |
// Hack to make patch functions copy-pasteable to roscpp/include/param.h | |
bool getImpl(const std::string &key, XmlRpc::XmlRpcValue &v, bool use_cache) | |
{ | |
if(use_cache) | |
return ros::param::getCached(key, v); | |
else | |
return ros::param::get(key, v); | |
} | |
template<typename T> bool getImpl(const std::string& key, std::vector<T>& l, bool use_cache) | |
{ | |
using namespace ros; | |
XmlRpc::XmlRpcValue v; | |
if(!getImpl(key, v, use_cache)) | |
return false; | |
if(v.getType() != XmlRpc::XmlRpcValue::TypeArray) | |
return false; | |
//l.empty(); // Do we want to clear the list or append? | |
for(int32_t i = 0; i < v.size(); i++) | |
{ | |
if(!isXmlRpcValueType<T>(v[i])) | |
return false; | |
l.push_back(static_cast<T>(v[i])); | |
} | |
return true; | |
} | |
template<typename T> bool getImpl(const std::string& key, std::map<std::string, T>& d, bool use_cache) | |
{ | |
using namespace ros; | |
XmlRpc::XmlRpcValue v; | |
if(!getImpl(key, v, use_cache)) | |
return false; | |
if(v.getType() != XmlRpc::XmlRpcValue::TypeStruct) | |
return false; | |
// d.empty(); // Do we want to clear the map or append? | |
for(XmlRpc::XmlRpcValue::iterator itr = v.begin(); | |
itr != v.end(); | |
itr++) | |
{ | |
if(!isXmlRpcValueType<T>(itr->second)) | |
return false; | |
const std::string& map_key = itr->first; | |
const T& map_value = static_cast<T>(itr->second); | |
d[map_key] = map_value; | |
} | |
return true; | |
} | |
template<typename T> bool get(const std::string& key, std::vector<std::string, T>& l) | |
{ | |
return getImpl(key, l, false); | |
} | |
template<typename T> bool get(const std::string& key, std::map<std::string, T>& d) | |
{ | |
return getImpl(key, d, false); | |
} | |
template<typename T> bool getCached(const std::string& key, std::vector<std::string, T>& l) | |
{ | |
return getImpl(key, l, true); | |
} | |
template<typename T> bool getCached(const std::string& key, std::map<std::string, T>& d) | |
{ | |
return getImpl(key, d, true); | |
} | |
} /* namespace param */ | |
} /* namespace rospatch */ | |
#endif /* ROSCPP_PARAM_PATCH_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment