YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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>
17
18#include <string>
19
20namespace yarp::os::idl {
21
27{
28public:
30
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 readBlock(char* const data, size_t len);
120
121 bool readBinary(std::string& str);
122
123 template <typename EnumBase, typename ConverterType>
125 {
126 std::int32_t tag = state->code;
127 if (tag < 0) {
128 if (noMore()) {
129 return false;
130 }
131 tag = reader.expectInt32();
132 }
133 if (noMore()) {
134 return false;
135 }
136 switch(tag) {
137 case BOTTLE_TAG_INT8:
138 x = static_cast<EnumBase>(reader.expectInt8());
139 state->len--;
140 return !reader.isError();
141 case BOTTLE_TAG_INT16:
142 x = static_cast<EnumBase>(reader.expectInt16());
143 state->len--;
144 return !reader.isError();
147 x = static_cast<EnumBase>(reader.expectInt32());
148 state->len--;
149 return !reader.isError();
150 case BOTTLE_TAG_INT64:
151 x = static_cast<EnumBase>(reader.expectInt64());
152 state->len--;
153 return !reader.isError();
154 case BOTTLE_TAG_STRING: {
155 std::int32_t len = reader.expectInt32();
156 if (reader.isError() || len < 1 || noMore()) {
157 return false;
158 }
159 std::string str;
160 str.resize(len);
161 reader.expectBlock(const_cast<char*>(str.data()), len);
162 str.resize(len - 1);
163 state->len--;
164 if (reader.isError()) {
165 return false;
166 }
167 x = ConverterType::fromString(str);
168 return (x >= 0);
169 }}
170 return false;
171 }
172
173 bool readListHeader();
174
175 bool readListHeader(int len);
176
177 bool readListReturn();
178
179 int getLength() const
180 {
181 return state->len;
182 }
183
184 ConnectionReader& getReader();
185
186 ConnectionWriter& getWriter();
187
188 bool isValid();
189
190 bool isError();
191
192 std::string readTag(size_t len = static_cast<size_t>(-1));
193
194 void readListBegin(yarp::os::idl::WireState& nstate, size_t& len);
195
196 void readSetBegin(yarp::os::idl::WireState& nstate, size_t& len);
197
198 void readMapBegin(yarp::os::idl::WireState& nstate, yarp::os::idl::WireState& nstate2, size_t& len);
199
200 void readListEnd();
201
202 void readSetEnd();
203
204 void readMapEnd();
205
206 bool noMore();
207
208 bool getMode() const;
209
210 bool getIsVocab32() const;
211
212 const std::string& getString() const;
213
214private:
215 NullConnectionWriter null_writer;
216 ConnectionReader& reader;
217 WireState baseState;
218 WireState* state {&baseState};
219 bool flush_if_needed {false};
220 bool support_get_mode {false};
221 bool expecting {false};
222 bool get_is_vocab {false};
224 bool get_mode {false};
225
226
227 void scanString(std::string& str, bool is_vocab);
228};
229
230} // namespace yarp::os::idl
231
232
233#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
A mini-server for performing network communication in the background.
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.
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:124
std::int16_t expectInt16()
Definition WireReader.h:83
IDL-friendly state.
Definition WireState.h:17
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
#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