36 #include <boost/thread/recursive_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::recursive_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::recursive_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::recursive_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())
285 if (g_subscribed_params.insert(mapped_key).second)
290 params[2] = mapped_key;
292 if (!
master::execute(
"subscribeParam", params, result, payload,
false))
294 g_subscribed_params.erase(mapped_key);
303 params[1] = mapped_key;
312 boost::recursive_mutex::scoped_lock lock(g_params_mutex);
313 g_params[mapped_key] = v;
319 bool getImpl(
const std::string& key, std::string& s,
bool use_cache)
322 if (!
getImpl(key, v, use_cache))
330 bool getImpl(
const std::string& key,
double& d,
bool use_cache)
333 if (!
getImpl(key, v, use_cache))
354 bool getImpl(
const std::string& key,
float& f,
bool use_cache)
356 double d =
static_cast<double>(f);
357 bool result =
getImpl(key, d, use_cache);
359 f =
static_cast<float>(d);
363 bool getImpl(
const std::string& key,
int& i,
bool use_cache)
366 if (!
getImpl(key, v, use_cache))
375 if (fmod(d, 1.0) < 0.5)
398 bool getImpl(
const std::string& key,
bool& b,
bool use_cache)
401 if (!
getImpl(key, v, use_cache))
409 bool get(
const std::string& key, std::string&
s)
414 bool get(
const std::string& key,
double&
d)
419 bool get(
const std::string& key,
float&
f)
424 bool get(
const std::string& key,
int& i)
429 bool get(
const std::string& key,
bool& b)
471 return static_cast<T
>(xml_value);
479 template<>
bool xml_castable<std::string>(
int XmlType)
520 case XmlRpcValue::TypeDouble:
521 return static_cast<double>(xml_value);
522 case XmlRpcValue::TypeInt:
523 return static_cast<double>(
static_cast<int>(xml_value));
524 case XmlRpcValue::TypeBoolean:
525 return static_cast<double>(
static_cast<bool>(xml_value));
535 case XmlRpcValue::TypeDouble:
536 return static_cast<float>(
static_cast<double>(xml_value));
537 case XmlRpcValue::TypeInt:
538 return static_cast<float>(
static_cast<int>(xml_value));
539 case XmlRpcValue::TypeBoolean:
540 return static_cast<float>(
static_cast<bool>(xml_value));
550 case XmlRpcValue::TypeDouble:
551 return static_cast<int>(
static_cast<double>(xml_value));
552 case XmlRpcValue::TypeInt:
553 return static_cast<int>(xml_value);
554 case XmlRpcValue::TypeBoolean:
555 return static_cast<int>(
static_cast<bool>(xml_value));
565 case XmlRpcValue::TypeDouble:
566 return static_cast<bool>(
static_cast<double>(xml_value));
567 case XmlRpcValue::TypeInt:
568 return static_cast<bool>(
static_cast<int>(xml_value));
569 case XmlRpcValue::TypeBoolean:
570 return static_cast<bool>(xml_value);
577 bool getImpl(
const std::string& key, std::vector<T>& vec,
bool cached)
580 if(!
getImpl(key, xml_array, cached)) {
590 vec.resize(xml_array.
size());
593 for (
int i = 0; i < xml_array.
size(); i++) {
594 if(!xml_castable<T>(xml_array[i].getType())) {
604 bool get(
const std::string& key, std::vector<std::string>& vec)
606 return getImpl(key, vec,
false);
608 bool get(
const std::string& key, std::vector<double>& vec)
610 return getImpl(key, vec,
false);
612 bool get(
const std::string& key, std::vector<float>& vec)
614 return getImpl(key, vec,
false);
616 bool get(
const std::string& key, std::vector<int>& vec)
618 return getImpl(key, vec,
false);
620 bool get(
const std::string& key, std::vector<bool>& vec)
622 return getImpl(key, vec,
false);
625 bool getCached(
const std::string& key, std::vector<std::string>& vec)
627 return getImpl(key, vec,
true);
629 bool getCached(
const std::string& key, std::vector<double>& vec)
631 return getImpl(key, vec,
true);
633 bool getCached(
const std::string& key, std::vector<float>& vec)
635 return getImpl(key, vec,
true);
637 bool getCached(
const std::string& key, std::vector<int>& vec)
639 return getImpl(key, vec,
true);
641 bool getCached(
const std::string& key, std::vector<bool>& vec)
643 return getImpl(key, vec,
true);
647 bool getImpl(
const std::string& key, std::map<std::string, T>& map,
bool cached)
650 if(!
getImpl(key, xml_value, cached)) {
660 for (XmlRpc::XmlRpcValue::ValueStruct::const_iterator it = xml_value.
begin();
661 it != xml_value.
end();
665 if(!xml_castable<T>(it->second.getType())) {
669 map[it->first] =
xml_cast<T>(it->second);
675 bool get(
const std::string& key, std::map<std::string, std::string>& map)
677 return getImpl(key, map,
false);
679 bool get(
const std::string& key, std::map<std::string, double>& map)
681 return getImpl(key, map,
false);
683 bool get(
const std::string& key, std::map<std::string, float>& map)
685 return getImpl(key, map,
false);
687 bool get(
const std::string& key, std::map<std::string, int>& map)
689 return getImpl(key, map,
false);
691 bool get(
const std::string& key, std::map<std::string, bool>& map)
693 return getImpl(key, map,
false);
696 bool getCached(
const std::string& key, std::map<std::string, std::string>& map)
698 return getImpl(key, map,
true);
700 bool getCached(
const std::string& key, std::map<std::string, double>& map)
702 return getImpl(key, map,
true);
704 bool getCached(
const std::string& key, std::map<std::string, float>& map)
706 return getImpl(key, map,
true);
708 bool getCached(
const std::string& key, std::map<std::string, int>& map)
710 return getImpl(key, map,
true);
712 bool getCached(
const std::string& key, std::map<std::string, bool>& map)
714 return getImpl(key, map,
true);
721 if (!
master::execute(
"getParamNames", params, result, payload,
false)) {
729 if (result.
size() != 3) {
735 keys.resize(parameters.
size());
738 for (
int i = 0; i < parameters.
size(); ++i) {
742 keys[i] = std::string(parameters[i]);
747 bool search(
const std::string& key, std::string& result_out)
752 bool search(
const std::string& ns,
const std::string& key, std::string& result_out)
760 std::string remapped = key;
764 remapped = it->second;
767 params[1] = remapped;
776 result_out = (std::string)payload;
784 ROS_DEBUG_NAMED(
"cached_parameters",
"Received parameter update for key [%s]", clean_key.c_str());
786 boost::recursive_mutex::scoped_lock lock(g_params_mutex);
788 if (g_subscribed_params.find(clean_key) != g_subscribed_params.end())
790 g_params[clean_key] = v;
798 result[1] = std::string(
"");
816 boost::recursive_mutex::scoped_lock lock(g_params_mutex);
818 for(S_string::iterator itr = g_subscribed_params.begin();
819 itr != g_subscribed_params.end(); ++itr)
821 const std::string mapped_key(*itr);
828 M_string::const_iterator it = remappings.begin();
829 M_string::const_iterator end = remappings.end();
830 for (; it != end; ++it)
832 const std::string& name = it->first;
833 const std::string&
param = it->second;
840 if (name[0] ==
'_' && name[1] !=
'_')
842 std::string local_name =
"~" + name.substr(1);
844 bool success =
false;
848 int32_t i = boost::lexical_cast<int32_t>(
param);
852 catch (boost::bad_lexical_cast&)
864 double d = boost::lexical_cast<
double>(
param);
868 catch (boost::bad_lexical_cast&)
878 if (param ==
"true" || param ==
"True" || param ==
"TRUE")
882 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
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.
boost::recursive_mutex g_params_mutex
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.
Type const & getType() const
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)