28 #include "ros/param.h"
29 #include "ros/master.h"
30 #include "ros/xmlrpc_manager.h"
31 #include "ros/this_node.h"
32 #include "ros/names.h"
34 #include <ros/console.h>
47 typedef std::map<std::string, XmlRpc::XmlRpcValue>
M_Param;
55 while (ns_key !=
"" && ns_key !=
"/")
72 params[1] = mapped_key;
93 void set(
const std::string& key,
const std::string& s)
101 void set(
const std::string& key,
const char* s)
105 std::string sxx = std::string(
s);
110 void set(
const std::string& key,
double d)
116 void set(
const std::string& key,
int i)
122 void set(
const std::string& key,
bool b)
129 void setImpl(
const std::string& key,
const std::vector<T>& vec)
137 for(
size_t i=0; i < vec.size(); i++) {
138 xml_vec[i] = vec.at(i);
144 void set(
const std::string& key,
const std::vector<std::string>& vec)
149 void set(
const std::string& key,
const std::vector<double>& vec)
154 void set(
const std::string& key,
const std::vector<float>& vec)
159 void set(
const std::string& key,
const std::vector<int>& vec)
164 void set(
const std::string& key,
const std::vector<bool>& vec)
170 void setImpl(
const std::string& key,
const std::map<std::string, T>& map)
178 for(
typename std::map<std::string, T>::const_iterator it = map.begin(); it != map.end(); ++it) {
179 xml_value[it->first] = it->second;
185 void set(
const std::string& key,
const std::map<std::string, std::string>& map)
190 void set(
const std::string& key,
const std::map<std::string, double>& map)
195 void set(
const std::string& key,
const std::map<std::string, float>& map)
200 void set(
const std::string& key,
const std::map<std::string, int>& map)
205 void set(
const std::string& key,
const std::map<std::string, bool>& map)
210 bool has(
const std::string& key)
227 bool del(
const std::string& key)
240 params[1] = mapped_key;
255 if (mapped_key.empty()) mapped_key =
"/";
263 M_Param::iterator it =
g_params.find(mapped_key);
266 if (it->second.valid())
268 ROS_DEBUG_NAMED(
"cached_parameters",
"Using cached parameter value for key [%s]", mapped_key.c_str());
275 ROS_DEBUG_NAMED(
"cached_parameters",
"Cached parameter is invalid for key [%s]", mapped_key.c_str());
288 params[2] = mapped_key;
292 ROS_DEBUG_NAMED(
"cached_parameters",
"Subscribe to parameter [%s]: call to the master failed", mapped_key.c_str());
298 ROS_DEBUG_NAMED(
"cached_parameters",
"Subscribed to parameter [%s]", mapped_key.c_str());
306 params[1] = mapped_key;
317 ROS_DEBUG_NAMED(
"cached_parameters",
"Caching parameter [%s] with value type [%d]", mapped_key.c_str(), v.
getType());
324 bool getImpl(
const std::string& key, std::string& s,
bool use_cache)
327 if (!
getImpl(key, v, use_cache))
335 bool getImpl(
const std::string& key,
double& d,
bool use_cache)
338 if (!
getImpl(key, v, use_cache))
359 bool getImpl(
const std::string& key,
float& f,
bool use_cache)
361 double d =
static_cast<double>(
f);
362 bool result =
getImpl(key,
d, use_cache);
364 f =
static_cast<float>(
d);
368 bool getImpl(
const std::string& key,
int& i,
bool use_cache)
371 if (!
getImpl(key, v, use_cache))
380 if (fmod(
d, 1.0) < 0.5)
403 bool getImpl(
const std::string& key,
bool& b,
bool use_cache)
406 if (!
getImpl(key, v, use_cache))
414 bool get(
const std::string& key, std::string& s)
419 bool get(
const std::string& key,
double& d)
424 bool get(
const std::string& key,
float& f)
429 bool get(
const std::string& key,
int& i)
434 bool get(
const std::string& key,
bool& b)
476 return static_cast<T
>(xml_value);
484 template<>
bool xml_castable<std::string>(
int XmlType)
525 case XmlRpcValue::TypeDouble:
526 return static_cast<double>(xml_value);
527 case XmlRpcValue::TypeInt:
528 return static_cast<double>(
static_cast<int>(xml_value));
529 case XmlRpcValue::TypeBoolean:
530 return static_cast<double>(
static_cast<bool>(xml_value));
540 case XmlRpcValue::TypeDouble:
541 return static_cast<float>(
static_cast<double>(xml_value));
542 case XmlRpcValue::TypeInt:
543 return static_cast<float>(
static_cast<int>(xml_value));
544 case XmlRpcValue::TypeBoolean:
545 return static_cast<float>(
static_cast<bool>(xml_value));
555 case XmlRpcValue::TypeDouble:
556 return static_cast<int>(
static_cast<double>(xml_value));
557 case XmlRpcValue::TypeInt:
558 return static_cast<int>(xml_value);
559 case XmlRpcValue::TypeBoolean:
560 return static_cast<int>(
static_cast<bool>(xml_value));
570 case XmlRpcValue::TypeDouble:
571 return static_cast<bool>(
static_cast<double>(xml_value));
572 case XmlRpcValue::TypeInt:
573 return static_cast<bool>(
static_cast<int>(xml_value));
574 case XmlRpcValue::TypeBoolean:
575 return static_cast<bool>(xml_value);
582 bool getImpl(
const std::string& key, std::vector<T>& vec,
bool cached)
585 if(!
getImpl(key, xml_array, cached)) {
595 vec.resize(xml_array.
size());
598 for (
int i = 0; i < xml_array.
size(); i++) {
599 if(!xml_castable<T>(xml_array[i].getType())) {
603 vec[i] = xml_cast<T>(xml_array[i]);
609 bool get(
const std::string& key, std::vector<std::string>& vec)
611 return getImpl(key, vec,
false);
613 bool get(
const std::string& key, std::vector<double>& vec)
615 return getImpl(key, vec,
false);
617 bool get(
const std::string& key, std::vector<float>& vec)
619 return getImpl(key, vec,
false);
621 bool get(
const std::string& key, std::vector<int>& vec)
623 return getImpl(key, vec,
false);
625 bool get(
const std::string& key, std::vector<bool>& vec)
627 return getImpl(key, vec,
false);
630 bool getCached(
const std::string& key, std::vector<std::string>& vec)
632 return getImpl(key, vec,
true);
634 bool getCached(
const std::string& key, std::vector<double>& vec)
636 return getImpl(key, vec,
true);
638 bool getCached(
const std::string& key, std::vector<float>& vec)
640 return getImpl(key, vec,
true);
642 bool getCached(
const std::string& key, std::vector<int>& vec)
644 return getImpl(key, vec,
true);
646 bool getCached(
const std::string& key, std::vector<bool>& vec)
648 return getImpl(key, vec,
true);
652 bool getImpl(
const std::string& key, std::map<std::string, T>& map,
bool cached)
655 if(!
getImpl(key, xml_value, cached)) {
665 for (XmlRpc::XmlRpcValue::ValueStruct::const_iterator it = xml_value.
begin();
666 it != xml_value.
end();
670 if(!xml_castable<T>(it->second.getType())) {
674 map[it->first] = xml_cast<T>(it->second);
680 bool get(
const std::string& key, std::map<std::string, std::string>& map)
682 return getImpl(key, map,
false);
684 bool get(
const std::string& key, std::map<std::string, double>& map)
686 return getImpl(key, map,
false);
688 bool get(
const std::string& key, std::map<std::string, float>& map)
690 return getImpl(key, map,
false);
692 bool get(
const std::string& key, std::map<std::string, int>& map)
694 return getImpl(key, map,
false);
696 bool get(
const std::string& key, std::map<std::string, bool>& map)
698 return getImpl(key, map,
false);
701 bool getCached(
const std::string& key, std::map<std::string, std::string>& map)
703 return getImpl(key, map,
true);
705 bool getCached(
const std::string& key, std::map<std::string, double>& map)
707 return getImpl(key, map,
true);
709 bool getCached(
const std::string& key, std::map<std::string, float>& map)
711 return getImpl(key, map,
true);
713 bool getCached(
const std::string& key, std::map<std::string, int>& map)
715 return getImpl(key, map,
true);
717 bool getCached(
const std::string& key, std::map<std::string, bool>& map)
719 return getImpl(key, map,
true);
734 if (result.
size() != 3) {
740 keys.resize(parameters.
size());
743 for (
int i = 0; i < parameters.
size(); ++i) {
747 keys[i] = std::string(parameters[i]);
752 bool search(
const std::string& key, std::string& result_out)
757 bool search(
const std::string& ns,
const std::string& key, std::string& result_out)
765 std::string remapped = key;
769 remapped = it->second;
772 params[1] = remapped;
781 result_out = (std::string)
payload;
789 ROS_DEBUG_NAMED(
"cached_parameters",
"Received parameter update for key [%s]", clean_key.c_str());
803 result[1] = std::string(
"");
811 M_string::const_iterator it = remappings.begin();
812 M_string::const_iterator end = remappings.end();
813 for (; it != end; ++it)
815 const std::string&
name = it->first;
816 const std::string&
param = it->second;
823 if (
name[0] ==
'_' &&
name[1] !=
'_')
825 std::string local_name =
"~" +
name.substr(1);
857 if(sscanf(
param.c_str(),
"%d", &i) == 1)
861 else if(sscanf(
param.c_str(),
"%lf", &
d) == 1)