36 #include <boost/thread/mutex.hpp> 37 #include <boost/lexical_cast.hpp> 48 typedef std::map<std::string, XmlRpc::XmlRpcValue>
M_Param;
56 while (ns_key !=
"" && ns_key !=
"/")
58 if (g_subscribed_params.find(ns_key) != g_subscribed_params.end())
61 g_params.erase(ns_key);
73 params[1] = mapped_key;
79 boost::mutex::scoped_lock lock(g_params_mutex);
85 if (g_subscribed_params.find(mapped_key) != g_subscribed_params.end())
87 g_params[mapped_key] = v;
94 void set(
const std::string& key,
const std::string&
s)
102 void set(
const std::string& key,
const char*
s)
106 std::string sxx = std::string(
s);
111 void set(
const std::string& key,
double d)
117 void set(
const std::string& key,
int i)
123 void set(
const std::string& key,
bool b)
130 void setImpl(
const std::string& key,
const std::vector<T>& vec)
138 for(
size_t i=0; i < vec.size(); i++) {
139 xml_vec[i] = vec.at(i);
145 void set(
const std::string& key,
const std::vector<std::string>& vec)
150 void set(
const std::string& key,
const std::vector<double>& vec)
155 void set(
const std::string& key,
const std::vector<float>& vec)
160 void set(
const std::string& key,
const std::vector<int>& vec)
165 void set(
const std::string& key,
const std::vector<bool>& vec)
171 void setImpl(
const std::string& key,
const std::map<std::string, T>& map)
179 for(
typename std::map<std::string, T>::const_iterator it = map.begin(); it != map.end(); ++it) {
180 xml_value[it->first] = it->second;
186 void set(
const std::string& key,
const std::map<std::string, std::string>& map)
191 void set(
const std::string& key,
const std::map<std::string, double>& map)
196 void set(
const std::string& key,
const std::map<std::string, float>& map)
201 void set(
const std::string& key,
const std::map<std::string, int>& map)
206 void set(
const std::string& key,
const std::map<std::string, bool>& map)
211 bool has(
const std::string& key)
228 bool del(
const std::string& key)
233 boost::mutex::scoped_lock lock(g_params_mutex);
235 g_subscribed_params.erase(mapped_key);
236 g_params.erase(mapped_key);
241 params[1] = mapped_key;
256 if (mapped_key.empty()) mapped_key =
"/";
260 boost::mutex::scoped_lock lock(g_params_mutex);
262 if (g_subscribed_params.find(mapped_key) != g_subscribed_params.end())
264 M_Param::iterator it = g_params.find(mapped_key);
265 if (it != g_params.end())
267 if (it->second.valid())
269 ROS_DEBUG_NAMED(
"cached_parameters",
"Using cached parameter value for key [%s]", mapped_key.c_str());
276 ROS_DEBUG_NAMED(
"cached_parameters",
"Cached parameter is invalid for key [%s]", mapped_key.c_str());
284 if (g_subscribed_params.insert(mapped_key).second)
289 params[2] = mapped_key;
291 if (!
master::execute(
"subscribeParam", params, result, payload,
false))
293 ROS_DEBUG_NAMED(
"cached_parameters",
"Subscribe to parameter [%s]: call to the master failed", mapped_key.c_str());
294 g_subscribed_params.erase(mapped_key);
299 ROS_DEBUG_NAMED(
"cached_parameters",
"Subscribed to parameter [%s]", mapped_key.c_str());
307 params[1] = mapped_key;
316 boost::mutex::scoped_lock lock(g_params_mutex);
318 ROS_DEBUG_NAMED(
"cached_parameters",
"Caching parameter [%s] with value type [%d]", mapped_key.c_str(), v.
getType());
319 g_params[mapped_key] = v;
325 bool getImpl(
const std::string& key, std::string& s,
bool use_cache)
328 if (!
getImpl(key, v, use_cache))
336 bool getImpl(
const std::string& key,
double& d,
bool use_cache)
339 if (!
getImpl(key, v, use_cache))
360 bool getImpl(
const std::string& key,
float& f,
bool use_cache)
362 double d =
static_cast<double>(f);
363 bool result =
getImpl(key, d, use_cache);
365 f =
static_cast<float>(d);
369 bool getImpl(
const std::string& key,
int& i,
bool use_cache)
372 if (!
getImpl(key, v, use_cache))
381 if (fmod(d, 1.0) < 0.5)
404 bool getImpl(
const std::string& key,
bool& b,
bool use_cache)
407 if (!
getImpl(key, v, use_cache))
415 bool get(
const std::string& key, std::string&
s)
420 bool get(
const std::string& key,
double&
d)
425 bool get(
const std::string& key,
float&
f)
430 bool get(
const std::string& key,
int& i)
435 bool get(
const std::string& key,
bool& b)
477 return static_cast<T
>(xml_value);
485 template<>
bool xml_castable<std::string>(
int XmlType)
526 case XmlRpcValue::TypeDouble:
527 return static_cast<double>(xml_value);
528 case XmlRpcValue::TypeInt:
529 return static_cast<double>(
static_cast<int>(xml_value));
530 case XmlRpcValue::TypeBoolean:
531 return static_cast<double>(
static_cast<bool>(xml_value));
541 case XmlRpcValue::TypeDouble:
542 return static_cast<float>(
static_cast<double>(xml_value));
543 case XmlRpcValue::TypeInt:
544 return static_cast<float>(
static_cast<int>(xml_value));
545 case XmlRpcValue::TypeBoolean:
546 return static_cast<float>(
static_cast<bool>(xml_value));
556 case XmlRpcValue::TypeDouble:
557 return static_cast<int>(
static_cast<double>(xml_value));
558 case XmlRpcValue::TypeInt:
559 return static_cast<int>(xml_value);
560 case XmlRpcValue::TypeBoolean:
561 return static_cast<int>(
static_cast<bool>(xml_value));
571 case XmlRpcValue::TypeDouble:
572 return static_cast<bool>(
static_cast<double>(xml_value));
573 case XmlRpcValue::TypeInt:
574 return static_cast<bool>(
static_cast<int>(xml_value));
575 case XmlRpcValue::TypeBoolean:
576 return static_cast<bool>(xml_value);
583 bool getImpl(
const std::string& key, std::vector<T>& vec,
bool cached)
586 if(!
getImpl(key, xml_array, cached)) {
596 vec.resize(xml_array.
size());
599 for (
int i = 0; i < xml_array.
size(); i++) {
600 if(!xml_castable<T>(xml_array[i].getType())) {
610 bool get(
const std::string& key, std::vector<std::string>& vec)
612 return getImpl(key, vec,
false);
614 bool get(
const std::string& key, std::vector<double>& vec)
616 return getImpl(key, vec,
false);
618 bool get(
const std::string& key, std::vector<float>& vec)
620 return getImpl(key, vec,
false);
622 bool get(
const std::string& key, std::vector<int>& vec)
624 return getImpl(key, vec,
false);
626 bool get(
const std::string& key, std::vector<bool>& vec)
628 return getImpl(key, vec,
false);
631 bool getCached(
const std::string& key, std::vector<std::string>& vec)
633 return getImpl(key, vec,
true);
635 bool getCached(
const std::string& key, std::vector<double>& vec)
637 return getImpl(key, vec,
true);
639 bool getCached(
const std::string& key, std::vector<float>& vec)
641 return getImpl(key, vec,
true);
643 bool getCached(
const std::string& key, std::vector<int>& vec)
645 return getImpl(key, vec,
true);
647 bool getCached(
const std::string& key, std::vector<bool>& vec)
649 return getImpl(key, vec,
true);
653 bool getImpl(
const std::string& key, std::map<std::string, T>& map,
bool cached)
656 if(!
getImpl(key, xml_value, cached)) {
666 for (XmlRpc::XmlRpcValue::ValueStruct::const_iterator it = xml_value.
begin();
667 it != xml_value.
end();
671 if(!xml_castable<T>(it->second.getType())) {
675 map[it->first] =
xml_cast<T>(it->second);
681 bool get(
const std::string& key, std::map<std::string, std::string>& map)
683 return getImpl(key, map,
false);
685 bool get(
const std::string& key, std::map<std::string, double>& map)
687 return getImpl(key, map,
false);
689 bool get(
const std::string& key, std::map<std::string, float>& map)
691 return getImpl(key, map,
false);
693 bool get(
const std::string& key, std::map<std::string, int>& map)
695 return getImpl(key, map,
false);
697 bool get(
const std::string& key, std::map<std::string, bool>& map)
699 return getImpl(key, map,
false);
702 bool getCached(
const std::string& key, std::map<std::string, std::string>& map)
704 return getImpl(key, map,
true);
706 bool getCached(
const std::string& key, std::map<std::string, double>& map)
708 return getImpl(key, map,
true);
710 bool getCached(
const std::string& key, std::map<std::string, float>& map)
712 return getImpl(key, map,
true);
714 bool getCached(
const std::string& key, std::map<std::string, int>& map)
716 return getImpl(key, map,
true);
718 bool getCached(
const std::string& key, std::map<std::string, bool>& map)
720 return getImpl(key, map,
true);
727 if (!
master::execute(
"getParamNames", params, result, payload,
false)) {
735 if (result.
size() != 3) {
741 keys.resize(parameters.
size());
744 for (
int i = 0; i < parameters.
size(); ++i) {
748 keys[i] = std::string(parameters[i]);
753 bool search(
const std::string& key, std::string& result_out)
758 bool search(
const std::string& ns,
const std::string& key, std::string& result_out)
766 std::string remapped = key;
770 remapped = it->second;
773 params[1] = remapped;
782 result_out = (std::string)payload;
790 ROS_DEBUG_NAMED(
"cached_parameters",
"Received parameter update for key [%s]", clean_key.c_str());
792 boost::mutex::scoped_lock lock(g_params_mutex);
794 if (g_subscribed_params.find(clean_key) != g_subscribed_params.end())
796 g_params[clean_key] = v;
804 result[1] = std::string(
"");
812 M_string::const_iterator it = remappings.begin();
813 M_string::const_iterator end = remappings.end();
814 for (; it != end; ++it)
816 const std::string& name = it->first;
817 const std::string&
param = it->second;
824 if (name[0] ==
'_' && name[1] !=
'_')
826 std::string local_name =
"~" + name.substr(1);
828 bool success =
false;
832 int32_t i = boost::lexical_cast<int32_t>(
param);
836 catch (boost::bad_lexical_cast&)
848 double d = boost::lexical_cast<
double>(
param);
852 catch (boost::bad_lexical_cast&)
862 if (param ==
"true" || param ==
"True" || param ==
"TRUE")
866 else if (param ==
"false" || param ==
"False" || param ==
"FALSE")
ROSCPP_DECL bool getParamNames(std::vector< std::string > &keys)
Get the list of all the parameters in the server.
std::map< std::string, XmlRpc::XmlRpcValue > M_Param
bool xml_castable(int XmlType)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
Assign value from parameter server, with default.
std::set< std::string > S_string
boost::mutex g_params_mutex
bool xml_castable< int >(int XmlType)
ROSCPP_DECL std::string parentNamespace(const std::string &name)
Get the parent namespace of a name.
S_string g_subscribed_params
T xml_cast(XmlRpc::XmlRpcValue xml_value)
ROSCPP_DECL const std::string & getName()
Returns the name of the current node.
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
Resolve a graph resource name into a fully qualified graph resource name.
ROSCPP_DECL std::string clean(const std::string &name)
Cleans a graph resource name: removes double slashes, trailing slash.
Type const & getType() const
void setImpl(const std::string &key, const std::vector< T > &vec)
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
std::map< std::string, std::string > M_string
#define ROS_DEBUG_NAMED(name,...)
bool getImpl(const std::string &key, XmlRpc::XmlRpcValue &v, bool use_cache)
ROSCPP_DECL void set(const std::string &key, const XmlRpc::XmlRpcValue &v)
Set an arbitrary XML/RPC value on the parameter server.
void init(const M_string &remappings)
void invalidateParentParams(const std::string &key)
ROSCPP_DECL bool execute(const std::string &method, const XmlRpc::XmlRpcValue &request, XmlRpc::XmlRpcValue &response, XmlRpc::XmlRpcValue &payload, bool wait_for_master)
Execute an XMLRPC call on the master.
ROSCPP_DECL const M_string & getUnresolvedRemappings()
ROSCPP_DECL bool has(const std::string &key)
Check whether a parameter exists on the parameter server.
ROSCPP_DECL bool getCached(const std::string &key, std::string &s)
Get a string value from the parameter server, with local caching.
ROSCPP_DECL bool del(const std::string &key)
Delete a parameter from the parameter server.
bool xml_castable< double >(int XmlType)
static const XMLRPCManagerPtr & instance()
bool xml_castable< float >(int XmlType)
ROSCPP_DECL bool search(const std::string &ns, const std::string &key, std::string &result)
Search up the tree for a parameter with a given key.
void paramUpdateCallback(XmlRpc::XmlRpcValue ¶ms, XmlRpc::XmlRpcValue &result)
bool xml_castable< bool >(int XmlType)