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