YARP
Yet Another Robot Platform
SensorMeasurement.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 // Autogenerated by Thrift Compiler (0.14.1-yarped)
7 //
8 // This is an automatically generated file.
9 // It could get re-generated if the ALLOW_IDL_GENERATION flag is on.
10 
11 #include <SensorMeasurement.h>
12 
13 // Default constructor
15  WirePortable(),
16  measurement(),
17  timestamp(0)
18 {
19 }
20 
21 // Constructor with field values
23  const double timestamp) :
24  WirePortable(),
25  measurement(measurement),
26  timestamp(timestamp)
27 {
28 }
29 
30 // Read structure on a Wire
32 {
33  if (!read_measurement(reader)) {
34  return false;
35  }
36  if (!read_timestamp(reader)) {
37  return false;
38  }
39  return !reader.isError();
40 }
41 
42 // Read structure on a Connection
44 {
45  yarp::os::idl::WireReader reader(connection);
46  if (!reader.readListHeader(2)) {
47  return false;
48  }
49  return read(reader);
50 }
51 
52 // Write structure on a Wire
54 {
55  if (!write_measurement(writer)) {
56  return false;
57  }
58  if (!write_timestamp(writer)) {
59  return false;
60  }
61  return !writer.isError();
62 }
63 
64 // Write structure on a Connection
66 {
67  yarp::os::idl::WireWriter writer(connection);
68  if (!writer.writeListHeader(2)) {
69  return false;
70  }
71  return write(writer);
72 }
73 
74 // Convert to a printable string
75 std::string SensorMeasurement::toString() const
76 {
78  b.read(*this);
79  return b.toString();
80 }
81 
82 // Editor: default constructor
84 {
85  group = 0;
86  obj_owned = true;
87  obj = new SensorMeasurement;
88  dirty_flags(false);
89  yarp().setOwner(*this);
90 }
91 
92 // Editor: constructor with base class
94 {
95  group = 0;
96  obj_owned = false;
97  edit(obj, false);
98  yarp().setOwner(*this);
99 }
100 
101 // Editor: destructor
103 {
104  if (obj_owned) {
105  delete obj;
106  }
107 }
108 
109 // Editor: edit
111 {
112  if (obj_owned) {
113  delete this->obj;
114  }
115  this->obj = &obj;
116  obj_owned = false;
117  dirty_flags(dirty);
118  return true;
119 }
120 
121 // Editor: validity check
123 {
124  return obj != nullptr;
125 }
126 
127 // Editor: state
129 {
130  return *obj;
131 }
132 
133 // Editor: grouping begin
135 {
136  group++;
137 }
138 
139 // Editor: grouping end
141 {
142  group--;
143  if (group == 0 && is_dirty) {
144  communicate();
145  }
146 }
147 // Editor: measurement setter
149 {
150  will_set_measurement();
151  obj->measurement = measurement;
152  mark_dirty_measurement();
153  communicate();
154  did_set_measurement();
155 }
156 
157 // Editor: measurement getter
159 {
160  return obj->measurement;
161 }
162 
163 // Editor: measurement will_set
165 {
166  return true;
167 }
168 
169 // Editor: measurement did_set
171 {
172  return true;
173 }
174 
175 // Editor: timestamp setter
177 {
178  will_set_timestamp();
179  obj->timestamp = timestamp;
180  mark_dirty_timestamp();
181  communicate();
182  did_set_timestamp();
183 }
184 
185 // Editor: timestamp getter
187 {
188  return obj->timestamp;
189 }
190 
191 // Editor: timestamp will_set
193 {
194  return true;
195 }
196 
197 // Editor: timestamp did_set
199 {
200  return true;
201 }
202 
203 // Editor: clean
205 {
206  dirty_flags(false);
207 }
208 
209 // Editor: read
211 {
212  if (!isValid()) {
213  return false;
214  }
215  yarp::os::idl::WireReader reader(connection);
216  reader.expectAccept();
217  if (!reader.readListHeader()) {
218  return false;
219  }
220  int len = reader.getLength();
221  if (len == 0) {
222  yarp::os::idl::WireWriter writer(reader);
223  if (writer.isNull()) {
224  return true;
225  }
226  if (!writer.writeListHeader(1)) {
227  return false;
228  }
229  writer.writeString("send: 'help' or 'patch (param1 val1) (param2 val2)'");
230  return true;
231  }
232  std::string tag;
233  if (!reader.readString(tag)) {
234  return false;
235  }
236  if (tag == "help") {
237  yarp::os::idl::WireWriter writer(reader);
238  if (writer.isNull()) {
239  return true;
240  }
241  if (!writer.writeListHeader(2)) {
242  return false;
243  }
244  if (!writer.writeTag("many", 1, 0)) {
245  return false;
246  }
247  if (reader.getLength() > 0) {
248  std::string field;
249  if (!reader.readString(field)) {
250  return false;
251  }
252  if (field == "measurement") {
253  if (!writer.writeListHeader(1)) {
254  return false;
255  }
256  if (!writer.writeString("yarp::sig::Vector measurement")) {
257  return false;
258  }
259  }
260  if (field == "timestamp") {
261  if (!writer.writeListHeader(1)) {
262  return false;
263  }
264  if (!writer.writeString("double timestamp")) {
265  return false;
266  }
267  }
268  }
269  if (!writer.writeListHeader(3)) {
270  return false;
271  }
272  writer.writeString("*** Available fields:");
273  writer.writeString("measurement");
274  writer.writeString("timestamp");
275  return true;
276  }
277  bool nested = true;
278  bool have_act = false;
279  if (tag != "patch") {
280  if (((len - 1) % 2) != 0) {
281  return false;
282  }
283  len = 1 + ((len - 1) / 2);
284  nested = false;
285  have_act = true;
286  }
287  for (int i = 1; i < len; ++i) {
288  if (nested && !reader.readListHeader(3)) {
289  return false;
290  }
291  std::string act;
292  std::string key;
293  if (have_act) {
294  act = tag;
295  } else if (!reader.readString(act)) {
296  return false;
297  }
298  if (!reader.readString(key)) {
299  return false;
300  }
301  if (key == "measurement") {
302  will_set_measurement();
303  if (!obj->nested_read_measurement(reader)) {
304  return false;
305  }
306  did_set_measurement();
307  } else if (key == "timestamp") {
308  will_set_timestamp();
309  if (!obj->nested_read_timestamp(reader)) {
310  return false;
311  }
312  did_set_timestamp();
313  } else {
314  // would be useful to have a fallback here
315  }
316  }
317  reader.accept();
318  yarp::os::idl::WireWriter writer(reader);
319  if (writer.isNull()) {
320  return true;
321  }
322  writer.writeListHeader(1);
323  writer.writeVocab32('o', 'k');
324  return true;
325 }
326 
327 // Editor: write
329 {
330  if (!isValid()) {
331  return false;
332  }
333  yarp::os::idl::WireWriter writer(connection);
334  if (!writer.writeListHeader(dirty_count + 1)) {
335  return false;
336  }
337  if (!writer.writeString("patch")) {
338  return false;
339  }
340  if (is_dirty_measurement) {
341  if (!writer.writeListHeader(3)) {
342  return false;
343  }
344  if (!writer.writeString("set")) {
345  return false;
346  }
347  if (!writer.writeString("measurement")) {
348  return false;
349  }
350  if (!obj->nested_write_measurement(writer)) {
351  return false;
352  }
353  }
354  if (is_dirty_timestamp) {
355  if (!writer.writeListHeader(3)) {
356  return false;
357  }
358  if (!writer.writeString("set")) {
359  return false;
360  }
361  if (!writer.writeString("timestamp")) {
362  return false;
363  }
364  if (!obj->nested_write_timestamp(writer)) {
365  return false;
366  }
367  }
368  return !writer.isError();
369 }
370 
371 // Editor: send if possible
372 void SensorMeasurement::Editor::communicate()
373 {
374  if (group != 0) {
375  return;
376  }
377  if (yarp().canWrite()) {
378  yarp().write(*this);
379  clean();
380  }
381 }
382 
383 // Editor: mark dirty overall
384 void SensorMeasurement::Editor::mark_dirty()
385 {
386  is_dirty = true;
387 }
388 
389 // Editor: measurement mark_dirty
390 void SensorMeasurement::Editor::mark_dirty_measurement()
391 {
392  if (is_dirty_measurement) {
393  return;
394  }
395  dirty_count++;
396  is_dirty_measurement = true;
397  mark_dirty();
398 }
399 
400 // Editor: timestamp mark_dirty
401 void SensorMeasurement::Editor::mark_dirty_timestamp()
402 {
403  if (is_dirty_timestamp) {
404  return;
405  }
406  dirty_count++;
407  is_dirty_timestamp = true;
408  mark_dirty();
409 }
410 
411 // Editor: dirty_flags
412 void SensorMeasurement::Editor::dirty_flags(bool flag)
413 {
414  is_dirty = flag;
415  is_dirty_measurement = flag;
416  is_dirty_timestamp = flag;
417  dirty_count = flag ? 2 : 0;
418 }
419 
420 // read measurement field
421 bool SensorMeasurement::read_measurement(yarp::os::idl::WireReader& reader)
422 {
423  if (!reader.read(measurement)) {
424  reader.fail();
425  return false;
426  }
427  return true;
428 }
429 
430 // write measurement field
431 bool SensorMeasurement::write_measurement(const yarp::os::idl::WireWriter& writer) const
432 {
433  if (!writer.write(measurement)) {
434  return false;
435  }
436  return true;
437 }
438 
439 // read (nested) measurement field
440 bool SensorMeasurement::nested_read_measurement(yarp::os::idl::WireReader& reader)
441 {
442  if (!reader.readNested(measurement)) {
443  reader.fail();
444  return false;
445  }
446  return true;
447 }
448 
449 // write (nested) measurement field
450 bool SensorMeasurement::nested_write_measurement(const yarp::os::idl::WireWriter& writer) const
451 {
452  if (!writer.writeNested(measurement)) {
453  return false;
454  }
455  return true;
456 }
457 
458 // read timestamp field
459 bool SensorMeasurement::read_timestamp(yarp::os::idl::WireReader& reader)
460 {
461  if (!reader.readFloat64(timestamp)) {
462  reader.fail();
463  return false;
464  }
465  return true;
466 }
467 
468 // write timestamp field
469 bool SensorMeasurement::write_timestamp(const yarp::os::idl::WireWriter& writer) const
470 {
471  if (!writer.writeFloat64(timestamp)) {
472  return false;
473  }
474  return true;
475 }
476 
477 // read (nested) timestamp field
478 bool SensorMeasurement::nested_read_timestamp(yarp::os::idl::WireReader& reader)
479 {
480  if (!reader.readFloat64(timestamp)) {
481  reader.fail();
482  return false;
483  }
484  return true;
485 }
486 
487 // write (nested) timestamp field
488 bool SensorMeasurement::nested_write_timestamp(const yarp::os::idl::WireWriter& writer) const
489 {
490  if (!writer.writeFloat64(timestamp)) {
491  return false;
492  }
493  return true;
494 }
void set_timestamp(const double timestamp)
const yarp::sig::Vector & get_measurement() const
SensorMeasurement & state()
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
void set_measurement(const yarp::sig::Vector &measurement)
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
bool edit(SensorMeasurement &obj, bool dirty=true)
bool read(yarp::os::idl::WireReader &reader) override
bool write(const yarp::os::idl::WireWriter &writer) const override
yarp::sig::Vector measurement
std::string toString() const
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:240
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
An interface for reading from a network connection.
An interface for writing to a network connection.
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition: Wire.h:29
IDL-friendly connection reader.
Definition: WireReader.h:30
bool readNested(WirePortable &obj)
Definition: WireReader.cpp:88
bool readString(std::string &str, bool *is_vocab=nullptr)
Definition: WireReader.cpp:382
bool read(WirePortable &obj)
Definition: WireReader.cpp:78
bool readFloat64(yarp::conf::float64_t &x)
Definition: WireReader.cpp:277
IDL-friendly connection writer.
Definition: WireWriter.h:30
bool writeVocab32(yarp::conf::vocab32_t x) const
Definition: WireWriter.cpp:141
bool write(const WirePortable &obj) const
Definition: WireWriter.cpp:52
bool writeListHeader(int len) const
Definition: WireWriter.cpp:206
bool writeTag(const char *tag, int split, int len) const
Definition: WireWriter.cpp:164
bool writeFloat64(yarp::conf::float64_t x) const
Definition: WireWriter.cpp:114
bool writeString(const std::string &tag) const
Definition: WireWriter.cpp:189
bool writeNested(const WirePortable &obj) const
Definition: WireWriter.cpp:62
bool isValid()
Check if time is valid (non-zero).
Definition: Time.cpp:314
The main, catch-all namespace for YARP.
Definition: dirs.h:16