ParameterTarget.cpp
Go to the documentation of this file.
2 #include <swarmros/Exception.h>
3 #include <swarmio/data/Helper.h>
4 #include <limits>
5 
6 using namespace swarmros;
7 using namespace swarmros::bridge;
8 
10 {
11  swarmio::data::Variant result;
12  switch (value.getType())
13  {
14  case XmlRpc::XmlRpcValue::Type::TypeInt:
15  result.set_int_value((int&)value);
16  break;
17 
18  case XmlRpc::XmlRpcValue::Type::TypeDouble:
19  result.set_double_value((double&)value);
20  break;
21 
22  case XmlRpc::XmlRpcValue::Type::TypeBoolean:
23  result.set_bool_value((bool&)value);
24  break;
25 
26  case XmlRpc::XmlRpcValue::Type::TypeString:
27  result.set_string_value((std::string&)value);
28  break;
29 
30  case XmlRpc::XmlRpcValue::Type::TypeStruct:
31  for (auto& pair : value)
32  {
33  (*result.mutable_map_value()->mutable_pairs())[pair.first] = VariantFromXmlRpcValue(pair.second);
34  }
35  break;
36 
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");
41 
42  default:
43  if (value.getType() == XmlRpc::XmlRpcValue::Type::TypeArray)
44  {
45  if (value.size() == 0)
46  {
47  result.mutable_map_array();
48  }
49  else
50  {
51  swarmio::data::Variant::ValueCase arrayType = swarmio::data::Variant::ValueCase::VALUE_NOT_SET;
52  for (int i = 0; i < value.size(); ++i)
53  {
54  auto current = VariantFromXmlRpcValue(value[i]);
55  if (arrayType == swarmio::data::Variant::ValueCase::VALUE_NOT_SET)
56  {
57  arrayType = current.value_case();
58  }
59  else if (arrayType != current.value_case())
60  {
61  throw Exception("XmlRpc heterogeneous arrays are not supported");
62  }
63  switch (current.value_case())
64  {
65  case swarmio::data::Variant::ValueCase::kBoolValue:
66  result.mutable_bool_array()->mutable_elements()->Add(current.bool_value());
67  break;
68 
69  case swarmio::data::Variant::ValueCase::kIntValue:
70  result.mutable_int_array()->mutable_elements()->Add(current.int_value());
71  break;
72 
73  case swarmio::data::Variant::ValueCase::kDoubleValue:
74  result.mutable_double_array()->mutable_elements()->Add(current.double_value());
75  break;
76 
77  case swarmio::data::Variant::ValueCase::kStringValue:
78  *result.mutable_string_array()->mutable_elements()->Add() = current.string_value();
79  break;
80 
81  case swarmio::data::Variant::ValueCase::kMapValue:
82  *result.mutable_map_array()->mutable_elements()->Add() = current.map_value();
83  break;
84 
85  default:
86  throw Exception("Unsupported XmlRpc array element type");
87  }
88  }
89  }
90  }
91  else
92  {
93  throw Exception("Unknown XmlRpc data type");
94  }
95  }
96  return result;
97 }
98 
100 {
102  {
104  int i = 0;
106  switch (value.value_case())
107  {
108  case swarmio::data::Variant::ValueCase::kBoolArray:
109  for (bool e : value.bool_array().elements())
110  {
111  result[i++] = e;
112  }
113  break;
114 
115  case swarmio::data::Variant::ValueCase::kIntArray:
116  for (int64_t e : value.int_array().elements())
117  {
118  if (e < std::numeric_limits<int>::min() ||
119  e > std::numeric_limits<int>::max())
120  {
121  throw Exception("Integer overflow in XmlRpc data type");
122  }
123  result[i++] = (int)e;
124  }
125  break;
126 
127  case swarmio::data::Variant::ValueCase::kUintArray:
128  for (uint64_t e : value.uint_array().elements())
129  {
130  if (e > std::numeric_limits<int>::max())
131  {
132  throw Exception("Integer overflow in XmlRpc data type");
133  }
134  result[i++] = (int)e;
135  }
136  break;
137 
138  case swarmio::data::Variant::ValueCase::kDoubleArray:
139  for (double e : value.double_array().elements())
140  {
141  result[i++] = e;
142  }
143  break;
144 
145  case swarmio::data::Variant::ValueCase::kStringValue:
146  for (const std::string& e : value.string_array().elements())
147  {
148  result[i++] = e;
149  }
150  break;
151 
152  case swarmio::data::Variant::ValueCase::kMapArray:
153  for (const auto& e : value.map_array().elements())
154  {
155  XmlRpc::XmlRpcValue subresult;
156  for (const auto& pair : e.pairs())
157  {
158  subresult[pair.first] = XmlRpcValueFromVariant(pair.second);
159  }
160  result[i++] = subresult;
161  }
162  break;
163 
164  default:
165  throw Exception("Unknown Variant data type");
166  }
167  return result;
168  }
169  else
170  {
171  switch (value.value_case())
172  {
173  case swarmio::data::Variant::ValueCase::kBoolValue:
174  return value.bool_value();
175 
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())
179  {
180  throw Exception("Integer overflow in XmlRpc data type");
181  }
182  return (int)value.int_value();
183 
184  case swarmio::data::Variant::ValueCase::kUintValue:
185  if (value.uint_value() > std::numeric_limits<int>::max())
186  {
187  throw Exception("Integer overflow in XmlRpc data type");
188  }
189  return (int)value.uint_value();
190 
191  case swarmio::data::Variant::ValueCase::kDoubleValue:
192  return value.double_value();
193 
194  case swarmio::data::Variant::ValueCase::kStringValue:
195  return value.string_value();
196 
197  default:
198  if (value.value_case() == swarmio::data::Variant::ValueCase::kMapValue)
199  {
201  for (const auto& pair : value.map_value().pairs())
202  {
203  result[pair.first] = XmlRpcValueFromVariant(pair.second);
204  }
205  return result;
206  }
207  else
208  {
209  throw Exception("Unknown Variant data type");
210  }
211  }
212  }
213 }
214 
215 ParameterTarget::ParameterTarget(ros::NodeHandle& nodeHandle, swarmio::services::keyvalue::Service& keyvalueService, const std::string& name, const std::string& parameter, bool isWritable)
216  : _nodeHandle(nodeHandle), _keyvalueService(keyvalueService), _path(name), _parameter(parameter), _isWritable(isWritable)
217 {
218  XmlRpc::XmlRpcValue value;
219  if (nodeHandle.getParam(parameter, value))
220  {
222  }
223  else
224  {
225  throw Exception("Parameter does not exist on parameter server");
226  }
227  keyvalueService.RegisterTarget(name, this);
228 }
229 
230 swarmio::data::Variant ParameterTarget::Get(const std::string& path)
231 {
232  if (path == _path)
233  {
234  return _value;
235  }
236  else
237  {
238  throw Exception("Trying to get value of unknown parameter");
239  }
240 }
241 
242 void ParameterTarget::Set(const std::string& path, const swarmio::data::Variant& value)
243 {
244  if (path == _path)
245  {
247  _value = value;
248  }
249  else
250  {
251  throw Exception("Trying to set value of unknown parameter");
252  }
253 }
254 
255 swarmio::data::discovery::Field ParameterTarget::GetFieldDescriptor(const std::string& path) const
256 {
257  if (path == _path)
258  {
260  }
261  else
262  {
263  throw Exception("Trying to get field descriptor of unknown parameter");
264  }
265 }
266 
267 bool ParameterTarget::CanWrite(const std::string& name) const noexcept
268 {
269  return _isWritable;
270 }
271 
272 bool ParameterTarget::CanRead(const std::string& name) const noexcept
273 {
274  return true;
275 }
276 
278 {
280 }
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 &parameter, bool isWritable)
Construct a new ParameterTarget object.
void setSize(int size)
static size_t GetCount(const Variant &value)
Get the item count of the variant array, or 1 if it is not an array.
Definition: Helper.cpp:26
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.
Definition: Helper.cpp:10
static discovery::Field GetFieldDescriptor(const Variant &value, bool includeArraySizes=false)
Get the schema for an existing map.
Definition: Helper.cpp:91
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.


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48