11 swarmio::data::Variant
result;
14 case XmlRpc::XmlRpcValue::Type::TypeInt:
15 result.set_int_value((
int&)value);
18 case XmlRpc::XmlRpcValue::Type::TypeDouble:
19 result.set_double_value((
double&)value);
22 case XmlRpc::XmlRpcValue::Type::TypeBoolean:
23 result.set_bool_value((
bool&)value);
26 case XmlRpc::XmlRpcValue::Type::TypeString:
27 result.set_string_value((std::string&)value);
30 case XmlRpc::XmlRpcValue::Type::TypeStruct:
31 for (
auto& pair : value)
37 case XmlRpc::XmlRpcValue::Type::TypeDateTime:
38 case XmlRpc::XmlRpcValue::Type::TypeInvalid:
39 case XmlRpc::XmlRpcValue::Type::TypeBase64:
40 throw Exception(
"Unsupported XmlRpc data type");
43 if (value.getType() == XmlRpc::XmlRpcValue::Type::TypeArray)
45 if (value.size() == 0)
47 result.mutable_map_array();
51 swarmio::data::Variant::ValueCase arrayType = swarmio::data::Variant::ValueCase::VALUE_NOT_SET;
52 for (
int i = 0; i < value.size(); ++i)
55 if (arrayType == swarmio::data::Variant::ValueCase::VALUE_NOT_SET)
57 arrayType = current.value_case();
59 else if (arrayType != current.value_case())
61 throw Exception(
"XmlRpc heterogeneous arrays are not supported");
63 switch (current.value_case())
65 case swarmio::data::Variant::ValueCase::kBoolValue:
66 result.mutable_bool_array()->mutable_elements()->Add(current.bool_value());
69 case swarmio::data::Variant::ValueCase::kIntValue:
70 result.mutable_int_array()->mutable_elements()->Add(current.int_value());
73 case swarmio::data::Variant::ValueCase::kDoubleValue:
74 result.mutable_double_array()->mutable_elements()->Add(current.double_value());
77 case swarmio::data::Variant::ValueCase::kStringValue:
78 *result.mutable_string_array()->mutable_elements()->Add() = current.string_value();
81 case swarmio::data::Variant::ValueCase::kMapValue:
82 *result.mutable_map_array()->mutable_elements()->Add() = current.map_value();
86 throw Exception(
"Unsupported XmlRpc array element type");
93 throw Exception(
"Unknown XmlRpc data type");
106 switch (value.value_case())
108 case swarmio::data::Variant::ValueCase::kBoolArray:
109 for (
bool e : value.bool_array().elements())
115 case swarmio::data::Variant::ValueCase::kIntArray:
116 for (int64_t e : value.int_array().elements())
118 if (e < std::numeric_limits<int>::min() ||
119 e > std::numeric_limits<int>::max())
121 throw Exception(
"Integer overflow in XmlRpc data type");
123 result[i++] = (int)e;
127 case swarmio::data::Variant::ValueCase::kUintArray:
128 for (uint64_t e : value.uint_array().elements())
130 if (e > std::numeric_limits<int>::max())
132 throw Exception(
"Integer overflow in XmlRpc data type");
134 result[i++] = (int)e;
138 case swarmio::data::Variant::ValueCase::kDoubleArray:
139 for (
double e : value.double_array().elements())
145 case swarmio::data::Variant::ValueCase::kStringValue:
146 for (
const std::string& e : value.string_array().elements())
152 case swarmio::data::Variant::ValueCase::kMapArray:
153 for (
const auto& e : value.map_array().elements())
156 for (
const auto& pair : e.pairs())
160 result[i++] = subresult;
165 throw Exception(
"Unknown Variant data type");
171 switch (value.value_case())
173 case swarmio::data::Variant::ValueCase::kBoolValue:
174 return value.bool_value();
176 case swarmio::data::Variant::ValueCase::kIntValue:
177 if (value.int_value() < std::numeric_limits<int>::min() ||
178 value.int_value() > std::numeric_limits<int>::max())
180 throw Exception(
"Integer overflow in XmlRpc data type");
182 return (
int)value.int_value();
184 case swarmio::data::Variant::ValueCase::kUintValue:
185 if (value.uint_value() > std::numeric_limits<int>::max())
187 throw Exception(
"Integer overflow in XmlRpc data type");
189 return (
int)value.uint_value();
191 case swarmio::data::Variant::ValueCase::kDoubleValue:
192 return value.double_value();
194 case swarmio::data::Variant::ValueCase::kStringValue:
195 return value.string_value();
198 if (value.value_case() == swarmio::data::Variant::ValueCase::kMapValue)
201 for (
const auto& pair : value.map_value().pairs())
209 throw Exception(
"Unknown Variant data type");
219 if (nodeHandle.
getParam(parameter, value))
225 throw Exception(
"Parameter does not exist on parameter server");
238 throw Exception(
"Trying to get value of unknown parameter");
251 throw Exception(
"Trying to set value of unknown parameter");
263 throw Exception(
"Trying to get field descriptor of unknown parameter");
std::string _parameter
Parameter name.
swarmio::data::Variant _value
Current value.
void RegisterTarget(const std::string &path, Target *target)
Register a new Target with the specified path.
The Key-Value Service is responsible for getting and setting named values.
ros::NodeHandle _nodeHandle
Node handle.
void UnregisterTarget(const std::string &path)
Unregister a Target.
virtual swarmio::data::discovery::Field GetFieldDescriptor(const std::string &path) const override
Get the data type of the target.
static XmlRpc::XmlRpcValue XmlRpcValueFromVariant(const swarmio::data::Variant &value)
Convert a variant to an XmlRpc value.
Exception base class for swarmros exceptions.
Type const & getType() const
bool _isWritable
True if the property can be set.
virtual swarmio::data::Variant Get(const std::string &path) override
Get the current value of the target parameter.
virtual void Set(const std::string &path, const swarmio::data::Variant &value) override
Set the current value of the target.
ParameterTarget(ros::NodeHandle &nodeHandle, swarmio::services::keyvalue::Service &keyvalueService, const std::string &name, const std::string ¶meter, bool isWritable)
Construct a new ParameterTarget object.
static size_t GetCount(const Variant &value)
Get the item count of the variant array, or 1 if it is not an array.
virtual ~ParameterTarget()
Destructor.
virtual bool CanWrite(const std::string &name) const noexceptoverride
Determines whether the value can be written.
static bool IsArray(const Variant &value)
Check whether the variant contains an array type.
static discovery::Field GetFieldDescriptor(const Variant &value, bool includeArraySizes=false)
Get the schema for an existing map.
bool getParam(const std::string &key, std::string &s) const
swarmio::services::keyvalue::Service & _keyvalueService
Key-value service.
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
virtual bool CanRead(const std::string &name) const noexceptoverride
Determines whether the value can be read.
static swarmio::data::Variant VariantFromXmlRpcValue(XmlRpc::XmlRpcValue &value)
Convert an XmlRpc value to a variant.
std::string _path
Discoverable keyvalue name.