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 if (g_subscribed_params.find(mapped_key) != g_subscribed_params.end())
237 g_subscribed_params.erase(mapped_key);
240 g_params.erase(mapped_key);
245 params[1] = mapped_key;
260 if (mapped_key.empty()) mapped_key =
"/";
264 boost::mutex::scoped_lock lock(g_params_mutex);
266 if (g_subscribed_params.find(mapped_key) != g_subscribed_params.end())
268 M_Param::iterator it = g_params.find(mapped_key);
269 if (it != g_params.end())
271 if (it->second.valid())
273 ROS_DEBUG_NAMED(
"cached_parameters",
"Using cached parameter value for key [%s]", mapped_key.c_str());
280 ROS_DEBUG_NAMED(
"cached_parameters",
"Cached parameter is invalid for key [%s]", mapped_key.c_str());
288 if (g_subscribed_params.insert(mapped_key).second)
293 params[2] = mapped_key;
295 if (!
master::execute(
"subscribeParam", params, result, payload,
false))
297 ROS_DEBUG_NAMED(
"cached_parameters",
"Subscribe to parameter [%s]: call to the master failed", mapped_key.c_str());
298 g_subscribed_params.erase(mapped_key);
303 ROS_DEBUG_NAMED(
"cached_parameters",
"Subscribed to parameter [%s]", mapped_key.c_str());
311 params[1] = mapped_key;
320 boost::mutex::scoped_lock lock(g_params_mutex);
322 ROS_DEBUG_NAMED(
"cached_parameters",
"Caching parameter [%s] with value type [%d]", mapped_key.c_str(), v.
getType());
323 g_params[mapped_key] = v;
329 bool getImpl(
const std::string& key, std::string& s,
bool use_cache)
332 if (!
getImpl(key, v, use_cache))
340 bool getImpl(
const std::string& key,
double& d,
bool use_cache)
343 if (!
getImpl(key, v, use_cache))
364 bool getImpl(
const std::string& key,
float& f,
bool use_cache)
366 double d =
static_cast<double>(f);
367 bool result =
getImpl(key, d, use_cache);
369 f =
static_cast<float>(d);
373 bool getImpl(
const std::string& key,
int& i,
bool use_cache)
376 if (!
getImpl(key, v, use_cache))
385 if (fmod(d, 1.0) < 0.5)
408 bool getImpl(
const std::string& key,
bool& b,
bool use_cache)
411 if (!
getImpl(key, v, use_cache))
419 bool get(
const std::string& key, std::string&
s)
424 bool get(
const std::string& key,
double&
d)
429 bool get(
const std::string& key,
float&
f)
434 bool get(
const std::string& key,
int& i)
439 bool get(
const std::string& key,
bool& b)
481 return static_cast<T
>(xml_value);
489 template<>
bool xml_castable<std::string>(
int XmlType)
530 case XmlRpcValue::TypeDouble:
531 return static_cast<double>(xml_value);
532 case XmlRpcValue::TypeInt:
533 return static_cast<double>(
static_cast<int>(xml_value));
534 case XmlRpcValue::TypeBoolean:
535 return static_cast<double>(
static_cast<bool>(xml_value));
545 case XmlRpcValue::TypeDouble:
546 return static_cast<float>(
static_cast<double>(xml_value));
547 case XmlRpcValue::TypeInt:
548 return static_cast<float>(
static_cast<int>(xml_value));
549 case XmlRpcValue::TypeBoolean:
550 return static_cast<float>(
static_cast<bool>(xml_value));
560 case XmlRpcValue::TypeDouble:
561 return static_cast<int>(
static_cast<double>(xml_value));
562 case XmlRpcValue::TypeInt:
563 return static_cast<int>(xml_value);
564 case XmlRpcValue::TypeBoolean:
565 return static_cast<int>(
static_cast<bool>(xml_value));
575 case XmlRpcValue::TypeDouble:
576 return static_cast<bool>(
static_cast<double>(xml_value));
577 case XmlRpcValue::TypeInt:
578 return static_cast<bool>(
static_cast<int>(xml_value));
579 case XmlRpcValue::TypeBoolean:
580 return static_cast<bool>(xml_value);
587 bool getImpl(
const std::string& key, std::vector<T>& vec,
bool cached)
590 if(!
getImpl(key, xml_array, cached)) {
600 vec.resize(xml_array.
size());
603 for (
int i = 0; i < xml_array.
size(); i++) {
604 if(!xml_castable<T>(xml_array[i].getType())) {
614 bool get(
const std::string& key, std::vector<std::string>& vec)
616 return getImpl(key, vec,
false);
618 bool get(
const std::string& key, std::vector<double>& vec)
620 return getImpl(key, vec,
false);
622 bool get(
const std::string& key, std::vector<float>& vec)
624 return getImpl(key, vec,
false);
626 bool get(
const std::string& key, std::vector<int>& vec)
628 return getImpl(key, vec,
false);
630 bool get(
const std::string& key, std::vector<bool>& vec)
632 return getImpl(key, vec,
false);
635 bool getCached(
const std::string& key, std::vector<std::string>& vec)
637 return getImpl(key, vec,
true);
639 bool getCached(
const std::string& key, std::vector<double>& vec)
641 return getImpl(key, vec,
true);
643 bool getCached(
const std::string& key, std::vector<float>& vec)
645 return getImpl(key, vec,
true);
647 bool getCached(
const std::string& key, std::vector<int>& vec)
649 return getImpl(key, vec,
true);
651 bool getCached(
const std::string& key, std::vector<bool>& vec)
653 return getImpl(key, vec,
true);
657 bool getImpl(
const std::string& key, std::map<std::string, T>& map,
bool cached)
660 if(!
getImpl(key, xml_value, cached)) {
670 for (XmlRpc::XmlRpcValue::ValueStruct::const_iterator it = xml_value.
begin();
671 it != xml_value.
end();
675 if(!xml_castable<T>(it->second.getType())) {
679 map[it->first] =
xml_cast<T>(it->second);
685 bool get(
const std::string& key, std::map<std::string, std::string>& map)
687 return getImpl(key, map,
false);
689 bool get(
const std::string& key, std::map<std::string, double>& map)
691 return getImpl(key, map,
false);
693 bool get(
const std::string& key, std::map<std::string, float>& map)
695 return getImpl(key, map,
false);
697 bool get(
const std::string& key, std::map<std::string, int>& map)
699 return getImpl(key, map,
false);
701 bool get(
const std::string& key, std::map<std::string, bool>& map)
703 return getImpl(key, map,
false);
706 bool getCached(
const std::string& key, std::map<std::string, std::string>& map)
708 return getImpl(key, map,
true);
710 bool getCached(
const std::string& key, std::map<std::string, double>& map)
712 return getImpl(key, map,
true);
714 bool getCached(
const std::string& key, std::map<std::string, float>& map)
716 return getImpl(key, map,
true);
718 bool getCached(
const std::string& key, std::map<std::string, int>& map)
720 return getImpl(key, map,
true);
722 bool getCached(
const std::string& key, std::map<std::string, bool>& map)
724 return getImpl(key, map,
true);
731 if (!
master::execute(
"getParamNames", params, result, payload,
false)) {
739 if (result.
size() != 3) {
745 keys.resize(parameters.
size());
748 for (
int i = 0; i < parameters.
size(); ++i) {
752 keys[i] = std::string(parameters[i]);
757 bool search(
const std::string& key, std::string& result_out)
762 bool search(
const std::string& ns,
const std::string& key, std::string& result_out)
770 std::string remapped = key;
774 remapped = it->second;
777 params[1] = remapped;
786 result_out = (std::string)payload;
794 ROS_DEBUG_NAMED(
"cached_parameters",
"Received parameter update for key [%s]", clean_key.c_str());
796 boost::mutex::scoped_lock lock(g_params_mutex);
798 if (g_subscribed_params.find(clean_key) != g_subscribed_params.end())
800 g_params[clean_key] = v;
808 result[1] = std::string(
"");
826 boost::mutex::scoped_lock lock(g_params_mutex);
828 for(S_string::iterator itr = g_subscribed_params.begin();
829 itr != g_subscribed_params.end(); ++itr)
831 const std::string mapped_key(*itr);
838 M_string::const_iterator it = remappings.begin();
839 M_string::const_iterator end = remappings.end();
840 for (; it != end; ++it)
842 const std::string& name = it->first;
843 const std::string&
param = it->second;
850 if (name[0] ==
'_' && name[1] !=
'_')
852 std::string local_name =
"~" + name.substr(1);
854 bool success =
false;
858 int32_t i = boost::lexical_cast<int32_t>(
param);
862 catch (boost::bad_lexical_cast&)
874 double d = boost::lexical_cast<
double>(
param);
878 catch (boost::bad_lexical_cast&)
888 if (param ==
"true" || param ==
"True" || param ==
"TRUE")
892 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)
ROSCPP_DECL void unsubscribeCachedParam(const std::string &key)
Unsubscribe cached parameter from the master.
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)