YARP
Yet Another Robot Platform
SensorMeasurements.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 // Autogenerated by Thrift Compiler (0.12.0-yarped)
10 //
11 // This is an automatically generated file.
12 // It could get re-generated if the ALLOW_IDL_GENERATION flag is on.
13 
14 #include <SensorMeasurements.h>
15 
16 // Default constructor
18  WirePortable(),
19  measurements()
20 {
21 }
22 
23 // Constructor with field values
24 SensorMeasurements::SensorMeasurements(const std::vector<SensorMeasurement>& measurements) :
25  WirePortable(),
26  measurements(measurements)
27 {
28 }
29 
30 // Read structure on a Wire
32 {
33  if (!read_measurements(reader)) {
34  return false;
35  }
36  return !reader.isError();
37 }
38 
39 // Read structure on a Connection
41 {
42  yarp::os::idl::WireReader reader(connection);
43  if (!reader.readListHeader(1)) {
44  return false;
45  }
46  return read(reader);
47 }
48 
49 // Write structure on a Wire
51 {
52  if (!write_measurements(writer)) {
53  return false;
54  }
55  return !writer.isError();
56 }
57 
58 // Write structure on a Connection
60 {
61  yarp::os::idl::WireWriter writer(connection);
62  if (!writer.writeListHeader(1)) {
63  return false;
64  }
65  return write(writer);
66 }
67 
68 // Convert to a printable string
69 std::string SensorMeasurements::toString() const
70 {
72  b.read(*this);
73  return b.toString();
74 }
75 
76 // Editor: default constructor
78 {
79  group = 0;
80  obj_owned = true;
81  obj = new SensorMeasurements;
82  dirty_flags(false);
83  yarp().setOwner(*this);
84 }
85 
86 // Editor: constructor with base class
88 {
89  group = 0;
90  obj_owned = false;
91  edit(obj, false);
92  yarp().setOwner(*this);
93 }
94 
95 // Editor: destructor
97 {
98  if (obj_owned) {
99  delete obj;
100  }
101 }
102 
103 // Editor: edit
105 {
106  if (obj_owned) {
107  delete this->obj;
108  }
109  this->obj = &obj;
110  obj_owned = false;
111  dirty_flags(dirty);
112  return true;
113 }
114 
115 // Editor: validity check
117 {
118  return obj != nullptr;
119 }
120 
121 // Editor: state
123 {
124  return *obj;
125 }
126 
127 // Editor: grouping begin
129 {
130  group++;
131 }
132 
133 // Editor: grouping end
135 {
136  group--;
137  if (group == 0 && is_dirty) {
138  communicate();
139  }
140 }
141 // Editor: measurements setter
142 void SensorMeasurements::Editor::set_measurements(const std::vector<SensorMeasurement>& measurements)
143 {
144  will_set_measurements();
145  obj->measurements = measurements;
146  mark_dirty_measurements();
147  communicate();
148  did_set_measurements();
149 }
150 
151 // Editor: measurements setter (list)
153 {
154  will_set_measurements();
155  obj->measurements[index] = elem;
156  mark_dirty_measurements();
157  communicate();
158  did_set_measurements();
159 }
160 
161 // Editor: measurements getter
162 const std::vector<SensorMeasurement>& SensorMeasurements::Editor::get_measurements() const
163 {
164  return obj->measurements;
165 }
166 
167 // Editor: measurements will_set
169 {
170  return true;
171 }
172 
173 // Editor: measurements did_set
175 {
176  return true;
177 }
178 
179 // Editor: clean
181 {
182  dirty_flags(false);
183 }
184 
185 // Editor: read
187 {
188  if (!isValid()) {
189  return false;
190  }
191  yarp::os::idl::WireReader reader(connection);
192  reader.expectAccept();
193  if (!reader.readListHeader()) {
194  return false;
195  }
196  int len = reader.getLength();
197  if (len == 0) {
198  yarp::os::idl::WireWriter writer(reader);
199  if (writer.isNull()) {
200  return true;
201  }
202  if (!writer.writeListHeader(1)) {
203  return false;
204  }
205  writer.writeString("send: 'help' or 'patch (param1 val1) (param2 val2)'");
206  return true;
207  }
208  std::string tag;
209  if (!reader.readString(tag)) {
210  return false;
211  }
212  if (tag == "help") {
213  yarp::os::idl::WireWriter writer(reader);
214  if (writer.isNull()) {
215  return true;
216  }
217  if (!writer.writeListHeader(2)) {
218  return false;
219  }
220  if (!writer.writeTag("many", 1, 0)) {
221  return false;
222  }
223  if (reader.getLength() > 0) {
224  std::string field;
225  if (!reader.readString(field)) {
226  return false;
227  }
228  if (field == "measurements") {
229  if (!writer.writeListHeader(1)) {
230  return false;
231  }
232  if (!writer.writeString("std::vector<SensorMeasurement> measurements")) {
233  return false;
234  }
235  }
236  }
237  if (!writer.writeListHeader(2)) {
238  return false;
239  }
240  writer.writeString("*** Available fields:");
241  writer.writeString("measurements");
242  return true;
243  }
244  bool nested = true;
245  bool have_act = false;
246  if (tag != "patch") {
247  if (((len - 1) % 2) != 0) {
248  return false;
249  }
250  len = 1 + ((len - 1) / 2);
251  nested = false;
252  have_act = true;
253  }
254  for (int i = 1; i < len; ++i) {
255  if (nested && !reader.readListHeader(3)) {
256  return false;
257  }
258  std::string act;
259  std::string key;
260  if (have_act) {
261  act = tag;
262  } else if (!reader.readString(act)) {
263  return false;
264  }
265  if (!reader.readString(key)) {
266  return false;
267  }
268  if (key == "measurements") {
269  will_set_measurements();
270  if (!obj->nested_read_measurements(reader)) {
271  return false;
272  }
273  did_set_measurements();
274  } else {
275  // would be useful to have a fallback here
276  }
277  }
278  reader.accept();
279  yarp::os::idl::WireWriter writer(reader);
280  if (writer.isNull()) {
281  return true;
282  }
283  writer.writeListHeader(1);
284  writer.writeVocab(yarp::os::createVocab('o', 'k'));
285  return true;
286 }
287 
288 // Editor: write
290 {
291  if (!isValid()) {
292  return false;
293  }
294  yarp::os::idl::WireWriter writer(connection);
295  if (!writer.writeListHeader(dirty_count + 1)) {
296  return false;
297  }
298  if (!writer.writeString("patch")) {
299  return false;
300  }
301  if (is_dirty_measurements) {
302  if (!writer.writeListHeader(3)) {
303  return false;
304  }
305  if (!writer.writeString("set")) {
306  return false;
307  }
308  if (!writer.writeString("measurements")) {
309  return false;
310  }
311  if (!obj->nested_write_measurements(writer)) {
312  return false;
313  }
314  }
315  return !writer.isError();
316 }
317 
318 // Editor: send if possible
319 void SensorMeasurements::Editor::communicate()
320 {
321  if (group != 0) {
322  return;
323  }
324  if (yarp().canWrite()) {
325  yarp().write(*this);
326  clean();
327  }
328 }
329 
330 // Editor: mark dirty overall
331 void SensorMeasurements::Editor::mark_dirty()
332 {
333  is_dirty = true;
334 }
335 
336 // Editor: measurements mark_dirty
337 void SensorMeasurements::Editor::mark_dirty_measurements()
338 {
339  if (is_dirty_measurements) {
340  return;
341  }
342  dirty_count++;
343  is_dirty_measurements = true;
344  mark_dirty();
345 }
346 
347 // Editor: dirty_flags
348 void SensorMeasurements::Editor::dirty_flags(bool flag)
349 {
350  is_dirty = flag;
351  is_dirty_measurements = flag;
352  dirty_count = flag ? 1 : 0;
353 }
354 
355 // read measurements field
356 bool SensorMeasurements::read_measurements(yarp::os::idl::WireReader& reader)
357 {
358  measurements.clear();
359  uint32_t _size0;
360  yarp::os::idl::WireState _etype3;
361  reader.readListBegin(_etype3, _size0);
362  measurements.resize(_size0);
363  for (size_t _i4 = 0; _i4 < _size0; ++_i4) {
364  if (!reader.readNested(measurements[_i4])) {
365  reader.fail();
366  return false;
367  }
368  }
369  reader.readListEnd();
370  return true;
371 }
372 
373 // write measurements field
374 bool SensorMeasurements::write_measurements(const yarp::os::idl::WireWriter& writer) const
375 {
376  if (!writer.writeListBegin(BOTTLE_TAG_LIST, static_cast<uint32_t>(measurements.size()))) {
377  return false;
378  }
379  for (const auto& _item5 : measurements) {
380  if (!writer.writeNested(_item5)) {
381  return false;
382  }
383  }
384  if (!writer.writeListEnd()) {
385  return false;
386  }
387  return true;
388 }
389 
390 // read (nested) measurements field
391 bool SensorMeasurements::nested_read_measurements(yarp::os::idl::WireReader& reader)
392 {
393  measurements.clear();
394  uint32_t _size6;
395  yarp::os::idl::WireState _etype9;
396  reader.readListBegin(_etype9, _size6);
397  measurements.resize(_size6);
398  for (size_t _i10 = 0; _i10 < _size6; ++_i10) {
399  if (!reader.readNested(measurements[_i10])) {
400  reader.fail();
401  return false;
402  }
403  }
404  reader.readListEnd();
405  return true;
406 }
407 
408 // write (nested) measurements field
409 bool SensorMeasurements::nested_write_measurements(const yarp::os::idl::WireWriter& writer) const
410 {
411  if (!writer.writeListBegin(BOTTLE_TAG_LIST, static_cast<uint32_t>(measurements.size()))) {
412  return false;
413  }
414  for (const auto& _item11 : measurements) {
415  if (!writer.writeNested(_item11)) {
416  return false;
417  }
418  }
419  if (!writer.writeListEnd()) {
420  return false;
421  }
422  return true;
423 }
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
SensorMeasurements::measurements
std::vector< SensorMeasurement > measurements
Definition: SensorMeasurements.h:26
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
yarp::os::idl::WireReader::readString
bool readString(std::string &str, bool *is_vocab=nullptr)
Definition: WireReader.cpp:339
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
BOTTLE_TAG_LIST
#define BOTTLE_TAG_LIST
Definition: Bottle.h:30
yarp::os::Time::isValid
bool isValid()
Check if time is valid (non-zero).
Definition: Time.cpp:317
SensorMeasurements::Editor::clean
void clean()
Definition: SensorMeasurements.cpp:180
yarp::os::idl::WireWriter::writeString
bool writeString(const std::string &tag) const
Definition: WireWriter.cpp:166
SensorMeasurements::Editor::start_editing
void start_editing()
Definition: SensorMeasurements.cpp:128
yarp::os::idl::WireWriter::isNull
bool isNull() const
Definition: WireWriter.cpp:50
yarp::os::idl::WireWriter::isError
bool isError() const
Definition: WireWriter.cpp:136
SensorMeasurements::Editor::set_measurements
void set_measurements(const std::vector< SensorMeasurement > &measurements)
Definition: SensorMeasurements.cpp:142
yarp::os::idl::WireWriter::writeListHeader
bool writeListHeader(int len) const
Definition: WireWriter.cpp:183
yarp::os::idl::WireWriter::writeNested
bool writeNested(const WirePortable &obj) const
Definition: WireWriter.cpp:65
yarp::os::idl::WireWriter
IDL-friendly connection writer.
Definition: WireWriter.h:33
SensorMeasurements::toString
std::string toString() const
Definition: SensorMeasurements.cpp:69
SensorMeasurements
Definition: SensorMeasurements.h:23
SensorMeasurement
Definition: SensorMeasurement.h:23
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::os::idl::WireReader::expectAccept
void expectAccept()
Definition: WireReader.cpp:43
SensorMeasurements::Editor::~Editor
~Editor() override
Definition: SensorMeasurements.cpp:96
SensorMeasurements::Editor::edit
bool edit(SensorMeasurements &obj, bool dirty=true)
Definition: SensorMeasurements.cpp:104
yarp::os::idl::WireReader::readListEnd
void readListEnd()
Definition: WireReader.cpp:613
SensorMeasurements::Editor::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: SensorMeasurements.cpp:186
SensorMeasurements.h
SensorMeasurements::read
bool read(yarp::os::idl::WireReader &reader) override
Definition: SensorMeasurements.cpp:31
SensorMeasurements::Editor::write
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
Definition: SensorMeasurements.cpp:289
SensorMeasurements::Editor::get_measurements
const std::vector< SensorMeasurement > & get_measurements() const
Definition: SensorMeasurements.cpp:162
yarp::os::idl::WireReader::readNested
bool readNested(WirePortable &obj)
Definition: WireReader.cpp:91
yarp::os::idl::WireReader::readListBegin
void readListBegin(yarp::os::idl::WireState &nstate, std::uint32_t &len)
Definition: WireReader.cpp:589
yarp::os::idl::WireState
IDL-friendly state.
Definition: WireState.h:22
SensorMeasurements::Editor::stop_editing
void stop_editing()
Definition: SensorMeasurements.cpp:134
yarp::os::idl::WireWriter::writeTag
bool writeTag(const char *tag, int split, int len) const
Definition: WireWriter.cpp:141
yarp::os::idl::WireReader
IDL-friendly connection reader.
Definition: WireReader.h:33
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::os::idl::WireReader::fail
void fail()
Definition: WireReader.cpp:74
SensorMeasurements::write
bool write(const yarp::os::idl::WireWriter &writer) const override
Definition: SensorMeasurements.cpp:50
yarp::os::Wire::yarp
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition: Wire.h:34
SensorMeasurements::Editor::will_set_measurements
virtual bool will_set_measurements()
Definition: SensorMeasurements.cpp:168
SensorMeasurements::Editor::isValid
bool isValid() const
Definition: SensorMeasurements.cpp:116
yarp::os::idl::WireWriter::writeVocab
bool writeVocab(std::int32_t x) const
Definition: WireWriter.cpp:124
yarp::os::idl::WireWriter::writeListBegin
bool writeListBegin(int tag, std::uint32_t len) const
Definition: WireWriter.cpp:204
SensorMeasurements::Editor::state
SensorMeasurements & state()
Definition: SensorMeasurements.cpp:122
yarp::os::idl::WireReader::accept
void accept()
Definition: WireReader.cpp:49
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::idl::WireWriter::writeListEnd
bool writeListEnd() const
Definition: WireWriter.cpp:227
yarp::os::Bottle::read
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:243
SensorMeasurements::Editor::Editor
Editor()
Definition: SensorMeasurements.cpp:77
SensorMeasurements::SensorMeasurements
SensorMeasurements()
Definition: SensorMeasurements.cpp:17
yarp::os::idl::WireReader::getLength
int getLength() const
Definition: WireReader.h:153
SensorMeasurements::Editor::did_set_measurements
virtual bool did_set_measurements()
Definition: SensorMeasurements.cpp:174
yarp::os::idl::WireReader::readListHeader
bool readListHeader()
Definition: WireReader.cpp:470
yarp::os::idl::WireReader::isError
bool isError()
Definition: WireReader.cpp:547