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