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 readVocab64(yarp::conf::vocab64_t& x);
76
77 bool readSizeT(std::size_t& x);
78
79 std::int8_t expectInt8()
80 {
81 std::int8_t x;
82 readI8(x);
83 return x;
84 }
85 std::int16_t expectInt16()
86 {
87 std::int16_t x;
88 readI16(x);
89 return x;
90 }
91 std::int32_t expectInt32()
92 {
93 std::int32_t x;
94 readI32(x);
95 return x;
96 }
97
98 std::int64_t expectInt64()
99 {
100 std::int64_t x;
101 readI64(x);
102 return x;
103 }
104
106 {
108 readFloat32(x);
109 return x;
110 }
111
113 {
115 readFloat64(x);
116 return x;
117 }
118
119 bool readString(std::string& str, bool* is_vocab = nullptr);
120
121 bool readBlock(char* const data, size_t len);
122
123 bool readBinary(std::string& str);
124
125 template <typename EnumBase, typename ConverterType>
127 {
128 std::int32_t tag = state->code;
129 if (tag < 0) {
130 if (noMore()) {
131 return false;
132 }
133 tag = reader.expectInt32();
134 }
135 if (noMore()) {
136 return false;
137 }
138 switch(tag) {
139 case BOTTLE_TAG_INT8:
140 x = static_cast<EnumBase>(reader.expectInt8());
141 state->len--;
142 return !reader.isError();
143 case BOTTLE_TAG_INT16:
144 x = static_cast<EnumBase>(reader.expectInt16());
145 state->len--;
146 return !reader.isError();
149 x = static_cast<EnumBase>(reader.expectInt32());
150 state->len--;
151 return !reader.isError();
154 x = static_cast<EnumBase>(reader.expectInt64());
155 state->len--;
156 return !reader.isError();
157 case BOTTLE_TAG_STRING: {
158 std::int32_t len = reader.expectInt32();
159 if (reader.isError() || len < 1 || noMore()) {
160 return false;
161 }
162 std::string str;
163 str.resize(len);
164 reader.expectBlock(const_cast<char*>(str.data()), len);
165 str.resize(len - 1);
166 state->len--;
167 if (reader.isError()) {
168 return false;
169 }
170 x = ConverterType::fromString(str);
171 return (x >= 0);
172 }}
173 return false;
174 }
175
176 bool readListHeader();
177
178 bool readListHeader(int len);
179
180 bool readListReturn();
181
182 int getLength() const
183 {
184 return state->len;
185 }
186
187 ConnectionReader& getReader();
188
189 ConnectionWriter& getWriter();
190
191 bool isValid();
192
193 bool isError();
194
195 std::string readTag(size_t len = static_cast<size_t>(-1));
196
197 void readListBegin(yarp::os::idl::WireState& nstate, size_t& len);
198
199 void readSetBegin(yarp::os::idl::WireState& nstate, size_t& len);
200
201 void readMapBegin(yarp::os::idl::WireState& nstate, yarp::os::idl::WireState& nstate2, size_t& len);
202
203 void readListEnd();
204
205 void readSetEnd();
206
207 void readMapEnd();
208
209 bool noMore();
210
211 bool getMode() const;
212
213 bool getIsVocab32() const;
214
215 const std::string& getString() const;
216
217private:
218 NullConnectionWriter null_writer;
219 ConnectionReader& reader;
220 WireState baseState;
221 WireState* state {&baseState};
222 bool flush_if_needed {false};
223 bool support_get_mode {false};
224 bool expecting {false};
225 bool get_is_vocab32 {false};
227 bool get_mode {false};
228
229
230 void scanString(std::string& str, bool is_vocab);
231};
232
233} // namespace yarp::os::idl
234
235
236#endif // YARP_OS_IDL_WIREREADER_H
#define BOTTLE_TAG_INT8
Definition Bottle.h:19
#define BOTTLE_TAG_VOCAB64
Definition Bottle.h:24
#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:27
#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:79
yarp::conf::float64_t expectFloat64()
Definition WireReader.h:112
std::int64_t expectInt64()
Definition WireReader.h:98
std::int32_t expectInt32()
Definition WireReader.h:91
yarp::conf::float32_t expectFloat32()
Definition WireReader.h:105
bool readEnum(EnumBase &x)
Definition WireReader.h:126
std::int16_t expectInt16()
Definition WireReader.h:85
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
std::int64_t vocab64_t
Definition numeric.h:79
#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