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
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
162bool WireWriter::writeSizeT(std::size_t x, bool skip_tag) const
163{
164 int tmp = x;
165 return writeI32(tmp, skip_tag);
166}
167
169{
170 return writer.isValid();
171}
172
174{
175 return writer.isError();
176}
177
178bool WireWriter::writeTag(const char* tag, int split, int len) const
179{
180 YARP_UNUSED(len);
181 if (split == 0) {
182 return writeString(tag);
183 }
184 std::string bit;
185 char ch = 'x';
186 while (ch != '\0') {
187 ch = *tag;
188 tag++;
189 if (ch == '\0' || ch == '_') {
190 if (bit.length() <= 4) {
192 } else {
194 }
195 bit.clear();
196 } else {
197 bit += ch;
198 }
199 }
200 return true;
201}
202
203bool WireWriter::writeString(const std::string& str, bool skip_tag) const
204{
205 if (!skip_tag) {
207 }
208 // WARNING str.length() value is not checked here
209 writer.appendString(str);
210 return !writer.isError();
211}
212
213bool WireWriter::writeBlock(const char* data, size_t len) const
214{
215 // FIXME Check if data is nullptr or len is 0?
216 writer.appendBlock(data, len);
217 return !writer.isError();
218}
219
220bool WireWriter::writeBinary(const std::string& blob, bool skip_tag) const
221{
222 if (!skip_tag) {
224 }
225 // WARNING blob.length() value is not checked here
226 writer.appendInt32(static_cast<int>(blob.length()));
227 writer.appendBlock(blob.c_str(), blob.length());
228 return !writer.isError();
229}
230
232{
234 if (get_mode) {
235 writer.appendInt32(len + 3);
237 writer.appendInt32(VOCAB_IS);
238 if (get_is_vocab32) {
240 writer.appendInt32(Vocab32::encode(get_string));
241 } else {
242 writeString(get_string);
243 }
244 need_ok = true;
245 } else {
246 writer.appendInt32(len);
247 }
248 return !writer.isError();
249}
250
251bool WireWriter::writeListBegin(int tag, size_t len) const
252{
253 writer.appendInt32(BOTTLE_TAG_LIST | tag);
254 // FIXME check len
255 writer.appendInt32(static_cast<int>(len));
256 return !writer.isError();
257}
258
259bool WireWriter::writeSetBegin(int tag, size_t len) const
260{
261 return writeListBegin(tag, len);
262}
263
264bool WireWriter::writeMapBegin(int tag, int tag2, size_t len) const
265{
266 YARP_UNUSED(tag);
269 // FIXME check len
270 writer.appendInt32(static_cast<int>(len));
271 return !writer.isError();
272}
273
275{
276 return true;
277}
278
280{
281 return true;
282}
283
285{
286 return true;
287}
288
290{
291 if (!writeListHeader(1)) {
292 return false;
293 }
295 writer.appendInt32(VOCAB_DONE);
296 return true;
297}
298
300{
301 if (reader) {
302 reader->flushWriter();
303 }
304}
void split(const std::string &s, char delim, std::vector< std::string > &elements)
#define BOTTLE_TAG_INT8
Definition Bottle.h:19
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:25
#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_BLOB
Definition Bottle.h:27
#define BOTTLE_TAG_LIST
Definition Bottle.h:28
#define BOTTLE_TAG_VOCAB32
Definition Bottle.h:23
#define BOTTLE_TAG_FLOAT32
Definition Bottle.h:24
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_IS
A mini-server for performing network communication in the background.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
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
A "tamed" Portable, that promises to serialize itself in an IDL-friendly way.
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 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
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition Vocab.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 Vocab.h:27
#define YARP_UNUSED(var)
Definition api.h:162