YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
WireWriter.cpp
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
7
8using namespace yarp::os;
9using namespace yarp::os::idl;
10
11namespace {
13constexpr yarp::conf::vocab32_t VOCAB_FAIL = yarp::os::createVocab32('f', 'a', 'i', 'l');
15constexpr yarp::conf::vocab32_t VOCAB_DONE = yarp::os::createVocab32('d', 'o', 'n', 'e');
16} // namespace
17
18
20 writer(writer)
21{
22 writer.convertTextMode();
23}
24
26 get_mode(reader.getMode()),
27 get_string(get_mode ? reader.getString() : ""),
28 get_is_vocab32(get_mode ? reader.getIsVocab32() : false),
29 reader(&reader.getReader()),
30 writer(reader.getWriter())
31{
32 writer.convertTextMode();
33}
34
36{
37 if (need_ok) {
38 writeBool(true);
39 }
40 if(reader) {
41 reader->flushWriter();
42 }
43}
44
46{
47 return writer.isNull();
48}
49
50bool WireWriter::write(const WirePortable& obj) const
51{
52 return obj.write(*this);
53}
54
56{
57 return obj.write(writer);
58}
59
61{
62 return obj.write(writer);
63}
64
66{
67 return obj.write(writer);
68}
69
70bool WireWriter::writeBool(bool x, bool skip_tag) const
71{
72 if (!skip_tag) {
74 }
75 writer.appendInt32(x ? VOCAB_OK : VOCAB_FAIL);
76 return !writer.isError();
77}
78
79bool WireWriter::writeI8(std::int8_t x, bool skip_tag) const
80{
81 if (!skip_tag) {
83 }
84 writer.appendInt8(x);
85 return !writer.isError();
86}
87
88bool WireWriter::writeI16(std::int16_t x, bool skip_tag) const
89{
90 if (!skip_tag) {
92 }
93 writer.appendInt16(x);
94 return !writer.isError();
95}
96
97bool WireWriter::writeI32(std::int32_t x, bool skip_tag) const
98{
99 if (!skip_tag) {
101 }
102 writer.appendInt32(x);
103 return !writer.isError();
104}
105
106bool WireWriter::writeI64(std::int64_t x, bool skip_tag) const
107{
108 if (!skip_tag) {
110 }
111 writer.appendInt64(x);
112 return !writer.isError();
113}
114
116{
117 if (!skip_tag) {
119 }
120 writer.appendFloat32(x);
121 return !writer.isError();
122}
123
125{
126 if (!skip_tag) {
128 }
129 writer.appendFloat64(x);
130 return !writer.isError();
131}
132
133bool WireWriter::writeUI8(std::uint8_t x, bool skip_tag) const
134{
135 return writeI8(reinterpret_cast<std::int8_t&>(x), skip_tag);
136}
137
138bool WireWriter::writeUI16(std::uint16_t x, bool skip_tag) const
139{
140 return writeI16(reinterpret_cast<std::int16_t&>(x), skip_tag);
141}
142
143bool WireWriter::writeUI32(std::uint32_t x, bool skip_tag) const
144{
145 return writeI32(reinterpret_cast<std::int32_t&>(x), skip_tag);
146}
147
148bool WireWriter::writeUI64(std::uint64_t x, bool skip_tag) const
149{
150 return writeI64(reinterpret_cast<std::int64_t&>(x), skip_tag);
151}
152
154{
155 if (!skip_tag) {
157 }
158 writer.appendInt32(x);
159 return !writer.isError();
160}
161
163{
164 if (!skip_tag) {
166 }
167 writer.appendInt64(x);
168 return !writer.isError();
169}
170
171bool WireWriter::writeSizeT(std::size_t x, bool skip_tag) const
172{
173 int tmp = x;
174 return writeI32(tmp, skip_tag);
175}
176
178{
179 return writer.isValid();
180}
181
183{
184 return writer.isError();
185}
186
187bool WireWriter::writeTag(const char* tag, int split, int len) const
188{
189 YARP_UNUSED(len);
190 if (split == 0) {
191 return writeString(tag);
192 }
193 std::string bit;
194 char ch = 'x';
195 while (ch != '\0') {
196 ch = *tag;
197 tag++;
198 if (ch == '\0' || ch == '_') {
199 if (bit.length() <= 4) {
201 } else {
203 }
204 bit.clear();
205 } else {
206 bit += ch;
207 }
208 }
209 return true;
210}
211
212bool WireWriter::writeString(const std::string& str, bool skip_tag) const
213{
214 if (!skip_tag) {
216 }
217 // WARNING str.length() value is not checked here
218 writer.appendString(str);
219 return !writer.isError();
220}
221
222bool WireWriter::writeBlock(const char* data, size_t len) const
223{
224 // FIXME Check if data is nullptr or len is 0?
225 writer.appendBlock(data, len);
226 return !writer.isError();
227}
228
229bool WireWriter::writeBinary(const std::string& blob, bool skip_tag) const
230{
231 if (!skip_tag) {
233 }
234 // WARNING blob.length() value is not checked here
235 writer.appendInt32(static_cast<int>(blob.length()));
236 writer.appendBlock(blob.c_str(), blob.length());
237 return !writer.isError();
238}
239
241{
243 if (get_mode) {
244 writer.appendInt32(len + 3);
246 writer.appendInt32(VOCAB_IS);
247 if (get_is_vocab32) {
249 writer.appendInt32(Vocab32::encode(get_string));
250 } else {
251 writeString(get_string);
252 }
253 need_ok = true;
254 } else {
255 writer.appendInt32(len);
256 }
257 return !writer.isError();
258}
259
260bool WireWriter::writeListBegin(int tag, size_t len) const
261{
262 writer.appendInt32(BOTTLE_TAG_LIST | tag);
263 // FIXME check len
264 writer.appendInt32(static_cast<int>(len));
265 return !writer.isError();
266}
267
268bool WireWriter::writeSetBegin(int tag, size_t len) const
269{
270 return writeListBegin(tag, len);
271}
272
273bool WireWriter::writeMapBegin(int tag, int tag2, size_t len) const
274{
275 YARP_UNUSED(tag);
278 // FIXME check len
279 writer.appendInt32(static_cast<int>(len));
280 return !writer.isError();
281}
282
284{
285 return true;
286}
287
289{
290 return true;
291}
292
294{
295 return true;
296}
297
299{
300 if (!writeListHeader(1)) {
301 return false;
302 }
304 writer.appendInt32(VOCAB_DONE);
305 return true;
306}
307
309{
310 if (reader) {
311 reader->flushWriter();
312 }
313}
#define BOTTLE_TAG_INT8
Definition Bottle.h:19
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:26
#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_BLOB
Definition Bottle.h:28
#define BOTTLE_TAG_LIST
Definition Bottle.h:29
#define BOTTLE_TAG_VOCAB32
Definition Bottle.h:23
#define BOTTLE_TAG_FLOAT32
Definition Bottle.h:25
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_IS
A mini-server for performing network communication in the background.
virtual void flushWriter()=0
An interface for writing to a network connection.
virtual bool isError() const =0
virtual void appendInt64(std::int64_t data)=0
Send a representation of a 64-bit integer to the network connection.
virtual void appendInt8(std::int8_t data)=0
Send a representation of a 8-bit integer to the network connection.
virtual void appendFloat32(yarp::conf::float32_t data)=0
Send a representation of a 32-bit floating point number to the network connection.
virtual void appendInt16(std::int16_t data)=0
Send a representation of a 16-bit integer to the network connection.
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
virtual void appendFloat64(yarp::conf::float64_t data)=0
Send a representation of a 64-bit floating point number to the network connection.
virtual bool isValid() const =0
virtual bool isNull() const
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
void appendString(const std::string &str)
Send a string to the network connection.
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition PortWriter.h:23
virtual bool write(ConnectionWriter &writer) const =0
Write this object to a network connection.
A "tamed" Portable, that promises to serialize itself in an IDL-friendly way.
virtual bool write(const yarp::os::idl::WireWriter &writer) const
IDL-friendly connection reader.
Definition WireReader.h:27
bool writeVocab32(yarp::conf::vocab32_t x, bool skip_tag=false) const
bool writeUI8(std::uint8_t x, bool skip_tag=false) const
bool writeBool(bool x, bool skip_tag=false) const
bool write(const WirePortable &obj) const
bool writeI32(std::int32_t x, bool skip_tag=false) const
bool writeI16(std::int16_t x, bool skip_tag=false) const
bool writeListHeader(int len) const
bool writeTag(const char *tag, int split, int len) const
bool writeUI64(std::uint64_t x, bool skip_tag=false) const
bool writeFloat64(yarp::conf::float64_t x, bool skip_tag=false) const
bool writeI64(std::int64_t x, bool skip_tag=false) const
bool writeFloat32(yarp::conf::float32_t x, bool skip_tag=false) const
bool writeUI32(std::uint32_t x, bool skip_tag=false) const
bool writeBlock(const char *data, size_t len) const
bool writeOnewayResponse() const
bool writeString(const std::string &str, bool skip_tag=false) const
bool writeNested(const WirePortable &obj) const
bool writeSetBegin(int tag, size_t len) const
bool writeI8(std::int8_t x, bool skip_tag=false) const
WireWriter(ConnectionWriter &writer)
bool writeListBegin(int tag, size_t len) const
bool writeBinary(const std::string &blob, bool skip_tag=false) const
bool writeVocab64(yarp::conf::vocab64_t x, bool skip_tag=false) const
bool writeMapBegin(int tag, int tag2, size_t len) const
bool writeUI16(std::uint16_t x, bool skip_tag=false) const
bool writeSizeT(std::size_t x, bool skip_tag=false) const
std::int32_t vocab32_t
Definition numeric.h:78
std::int64_t vocab64_t
Definition numeric.h:79
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition Vocab32.cpp:11
An interface to the operating system, including Port based communication.
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition Vocab32.h:27
#define YARP_UNUSED(var)
Definition api.h:162