YARP
Yet Another Robot Platform
WireReader.h
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 #ifndef YARP_OS_IDL_WIREREADER_H
7 #define YARP_OS_IDL_WIREREADER_H
8 
9 #include <yarp/conf/numeric.h>
10 
11 #include <yarp/os/Bottle.h>
14 #include <yarp/os/Vocab.h>
16 #include <yarp/os/idl/WireState.h>
17 
18 #include <string>
19 
20 namespace yarp::os::idl {
21 
27 {
28 public:
30 
31  ~WireReader();
32 
33  void expectAccept();
34 
35  void accept();
36 
37  void allowGetMode();
38 
39  bool clear();
40 
41  void fail();
42 
43  bool read(WirePortable& obj);
44 
45  bool read(yarp::os::PortReader& obj);
46 
47  bool readNested(WirePortable& obj);
48 
49  bool readNested(yarp::os::PortReader& obj);
50 
51  bool readBool(bool& x);
52 
53  bool readI8(std::int8_t& x);
54 
55  bool readI16(std::int16_t& x);
56 
57  bool readI32(std::int32_t& x);
58 
59  bool readI64(std::int64_t& x);
60 
61  bool readFloat32(yarp::conf::float32_t& x);
62 
63  bool readFloat64(yarp::conf::float64_t& x);
64 
65  bool readUI8(std::uint8_t& x);
66 
67  bool readUI16(std::uint16_t& x);
68 
69  bool readUI32(std::uint32_t& x);
70 
71  bool readUI64(std::uint64_t& x);
72 
73  bool readVocab32(yarp::conf::vocab32_t& x);
74 
75  bool readSizeT(std::size_t& x);
76 
77  std::int8_t expectInt8()
78  {
79  std::int8_t x;
80  readI8(x);
81  return x;
82  }
83  std::int16_t expectInt16()
84  {
85  std::int16_t x;
86  readI16(x);
87  return x;
88  }
89  std::int32_t expectInt32()
90  {
91  std::int32_t x;
92  readI32(x);
93  return x;
94  }
95 
96  std::int64_t expectInt64()
97  {
98  std::int64_t x;
99  readI64(x);
100  return x;
101  }
102 
104  {
106  readFloat32(x);
107  return x;
108  }
109 
111  {
113  readFloat64(x);
114  return x;
115  }
116 
117  bool readString(std::string& str, bool* is_vocab = nullptr);
118 
119  bool readBinary(std::string& str);
120 
121  template <typename EnumBase, typename ConverterType>
122  bool readEnum(EnumBase& x)
123  {
124  std::int32_t tag = state->code;
125  if (tag < 0) {
126  if (noMore()) {
127  return false;
128  }
129  tag = reader.expectInt32();
130  }
131  if (noMore()) {
132  return false;
133  }
134  switch(tag) {
135  case BOTTLE_TAG_INT8:
136  x = static_cast<EnumBase>(reader.expectInt8());
137  state->len--;
138  return !reader.isError();
139  case BOTTLE_TAG_INT16:
140  x = static_cast<EnumBase>(reader.expectInt16());
141  state->len--;
142  return !reader.isError();
143  case BOTTLE_TAG_INT32: [[fallthrough]];
144  case BOTTLE_TAG_VOCAB32:
145  x = static_cast<EnumBase>(reader.expectInt32());
146  state->len--;
147  return !reader.isError();
148  case BOTTLE_TAG_INT64:
149  x = static_cast<EnumBase>(reader.expectInt64());
150  state->len--;
151  return !reader.isError();
152  case BOTTLE_TAG_STRING: {
153  std::int32_t len = reader.expectInt32();
154  if (reader.isError() || len < 1 || noMore()) {
155  return false;
156  }
157  std::string str;
158  str.resize(len);
159  reader.expectBlock(const_cast<char*>(str.data()), len);
160  str.resize(len - 1);
161  state->len--;
162  if (reader.isError()) {
163  return false;
164  }
165  x = ConverterType::fromString(str);
166  return (x >= 0);
167  }}
168  return false;
169  }
170 
171  bool readListHeader();
172 
173  bool readListHeader(int len);
174 
175  bool readListReturn();
176 
177  int getLength() const
178  {
179  return state->len;
180  }
181 
182  ConnectionReader& getReader();
183 
184  ConnectionWriter& getWriter();
185 
186  bool isValid();
187 
188  bool isError();
189 
190  std::string readTag(size_t len = static_cast<size_t>(-1));
191 
192  void readListBegin(yarp::os::idl::WireState& nstate, std::uint32_t& len);
193 
194  void readSetBegin(yarp::os::idl::WireState& nstate, std::uint32_t& len);
195 
196  void readMapBegin(yarp::os::idl::WireState& nstate, yarp::os::idl::WireState& nstate2, std::uint32_t& len);
197 
198  void readListEnd();
199 
200  void readSetEnd();
201 
202  void readMapEnd();
203 
204  bool noMore();
205 
206  bool getMode() const;
207 
208  bool getIsVocab32() const;
209 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
210  YARP_DEPRECATED_MSG("Use getIsVocab instead")
211  bool getIsVocab() const {
212  return getIsVocab32();
213  }
214 #endif // YARP_NO_DEPRECATED
215 
216  const std::string& getString() const;
217 
218 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
219  YARP_DEPRECATED_MSG("Use readI8 instead")
220  bool readByte(std::int8_t& x)
221  {
222  return readI8(x);
223  }
224 
225  YARP_DEPRECATED_MSG("Use readFloat64 instead")
226  bool readDouble(double& x)
227  {
228  return readFloat64(x);
229  }
230 #endif // YARP_NO_DEPRECATED
231 
232 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5.0
233  YARP_DEPRECATED_MSG("Use readVocab32 instead")
234  bool readVocab(std::int32_t& x) {
235  return readVocab32(x);
236  }
237 #endif // YARP_NO_DEPRECATED
238 
239 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
240  YARP_DEPRECATED_MSG("Use expectInt32 instead")
241  int expectInt()
242  {
243  return static_cast<int>(expectInt32());
244  }
245 
246  YARP_DEPRECATED_MSG("Use expectFloat64 instead")
247  double expectDouble()
248  {
249  return static_cast<double>(expectFloat64());
250  }
251 #endif // YARP_NO_DEPRECATED
252 
253 private:
254  NullConnectionWriter null_writer;
255  ConnectionReader& reader;
256  WireState baseState;
257  WireState* state {&baseState};
258  bool flush_if_needed {false};
259  bool support_get_mode {false};
260  bool expecting {false};
261  bool get_is_vocab {false};
263  bool get_mode {false};
264 
265 
266  void scanString(std::string& str, bool is_vocab);
267 };
268 
269 } // namespace yarp::os::idl
270 
271 
272 #endif // YARP_OS_IDL_WIREREADER_H
#define BOTTLE_TAG_INT8
Definition: Bottle.h:19
#define BOTTLE_TAG_INT64
Definition: Bottle.h:22
#define BOTTLE_TAG_INT16
Definition: Bottle.h:20
#define BOTTLE_TAG_INT32
Definition: Bottle.h:21
#define BOTTLE_TAG_STRING
Definition: Bottle.h:26
#define BOTTLE_TAG_VOCAB32
Definition: Bottle.h:23
An interface for reading from a network connection.
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
virtual std::int64_t expectInt64()=0
Read a 64-bit integer from the network connection.
virtual bool isError() const =0
virtual std::int16_t expectInt16()=0
Read a 16-bit integer from the network connection.
virtual std::int8_t expectInt8()=0
Read a 8-bit integer from the network connection.
An interface for writing to a network connection.
A dummy ConnectionWriter that consumes data without effect.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
A "tamed" Portable, that promises to serialize itself in an IDL-friendly way.
Definition: WirePortable.h:21
IDL-friendly connection reader.
Definition: WireReader.h:27
std::int8_t expectInt8()
Definition: WireReader.h:77
yarp::conf::float64_t expectFloat64()
Definition: WireReader.h:110
std::int64_t expectInt64()
Definition: WireReader.h:96
std::int32_t expectInt32()
Definition: WireReader.h:89
yarp::conf::float32_t expectFloat32()
Definition: WireReader.h:103
bool readEnum(EnumBase &x)
Definition: WireReader.h:122
std::int16_t expectInt16()
Definition: WireReader.h:83
IDL-friendly state.
Definition: WireState.h:17
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:66
std::int32_t vocab32_t
Definition: numeric.h:78
double float64_t
Definition: numeric.h:77
float float32_t
Definition: numeric.h:76
bool isValid()
Check if time is valid (non-zero).
Definition: Time.cpp:314
std::string readString(bool *eof)
Definition: Terminal.cpp:76
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:915
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:338
#define YARP_os_API
Definition: api.h:18