YARP
Yet Another Robot Platform
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
19WireWriter::WireWriter(ConnectionWriter& writer) :
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
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) {
191 writeVocab32(bit);
192 } else {
193 writeString(bit);
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);
267 YARP_UNUSED(tag2);
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}
#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
Definition: GenericVocabs.h:15
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:14
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.
Definition: WirePortable.h:21
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
Definition: WireWriter.cpp:153
bool writeUI8(std::uint8_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:133
bool writeBool(bool x, bool skip_tag=false) const
Definition: WireWriter.cpp:70
bool write(const WirePortable &obj) const
Definition: WireWriter.cpp:50
bool writeI32(std::int32_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:97
bool writeI16(std::int16_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:88
bool writeListHeader(int len) const
Definition: WireWriter.cpp:231
bool writeTag(const char *tag, int split, int len) const
Definition: WireWriter.cpp:178
bool writeUI64(std::uint64_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:148
bool writeFloat64(yarp::conf::float64_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:124
bool writeI64(std::int64_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:106
bool writeFloat32(yarp::conf::float32_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:115
bool writeUI32(std::uint32_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:143
bool writeBlock(const char *data, size_t len) const
Definition: WireWriter.cpp:213
bool writeOnewayResponse() const
Definition: WireWriter.cpp:289
bool writeString(const std::string &str, bool skip_tag=false) const
Definition: WireWriter.cpp:203
bool writeNested(const WirePortable &obj) const
Definition: WireWriter.cpp:60
bool writeSetBegin(int tag, size_t len) const
Definition: WireWriter.cpp:259
bool writeI8(std::int8_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:79
WireWriter(ConnectionWriter &writer)
Definition: WireWriter.cpp:19
bool writeListBegin(int tag, size_t len) const
Definition: WireWriter.cpp:251
bool writeBinary(const std::string &blob, bool skip_tag=false) const
Definition: WireWriter.cpp:220
bool writeMapBegin(int tag, int tag2, size_t len) const
Definition: WireWriter.cpp:264
bool writeUI16(std::uint16_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:138
bool writeSizeT(std::size_t x, bool skip_tag=false) const
Definition: WireWriter.cpp:162
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:66
ContainerT split(const typename ContainerT::value_type &s, std::basic_regex< typename ContainerT::value_type::value_type > regex)
Utility to split a string by a separator, into a vector of strings.
Definition: string.h:26
std::int32_t vocab32_t
Definition: numeric.h:78
double float64_t
Definition: numeric.h:77
float float32_t
Definition: numeric.h:76
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