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 
8 using namespace yarp::os;
9 using namespace yarp::os::idl;
10 
11 namespace {
13 constexpr yarp::conf::vocab32_t VOCAB_FAIL = yarp::os::createVocab32('f', 'a', 'i', 'l');
15 constexpr yarp::conf::vocab32_t VOCAB_DONE = yarp::os::createVocab32('d', 'o', 'n', 'e');
16 } // namespace
17 
18 
20  writer(writer)
21 {
22  get_mode = get_is_vocab32 = false;
23  need_ok = false;
24  writer.convertTextMode();
25 }
26 
28  writer(reader.getWriter())
29 {
30  get_is_vocab32 = false;
31  need_ok = false;
32  writer.convertTextMode();
33  get_mode = reader.getMode();
34  if (get_mode) {
35  get_string = reader.getString();
36  get_is_vocab32 = reader.getIsVocab32();
37  }
38 }
39 
41 {
42  if (need_ok) {
43  writeBool(true);
44  }
45 }
46 
47 bool WireWriter::isNull() const
48 {
49  return writer.isNull();
50 }
51 
52 bool WireWriter::write(const WirePortable& obj) const
53 {
54  return obj.write(*this);
55 }
56 
58 {
59  return obj.write(writer);
60 }
61 
62 bool WireWriter::writeNested(const WirePortable& obj) const
63 {
64  return obj.write(writer);
65 }
66 
68 {
69  return obj.write(writer);
70 }
71 
72 bool WireWriter::writeBool(bool x) const
73 {
75  writer.appendInt32(x ? VOCAB_OK : VOCAB_FAIL);
76  return !writer.isError();
77 }
78 
79 bool WireWriter::writeI8(std::int8_t x) const
80 {
82  writer.appendInt8(x);
83  return !writer.isError();
84 }
85 
86 bool WireWriter::writeI16(std::int16_t x) const
87 {
89  writer.appendInt16(x);
90  return !writer.isError();
91 }
92 
93 bool WireWriter::writeI32(std::int32_t x) const
94 {
96  writer.appendInt32(x);
97  return !writer.isError();
98 }
99 
100 bool WireWriter::writeI64(std::int64_t x) const
101 {
103  writer.appendInt64(x);
104  return !writer.isError();
105 }
106 
108 {
110  writer.appendFloat32(x);
111  return !writer.isError();
112 }
113 
115 {
117  writer.appendFloat64(x);
118  return !writer.isError();
119 }
120 
121 bool WireWriter::writeUI8(std::uint8_t x) const
122 {
123  return writeI8(reinterpret_cast<std::int8_t&>(x));
124 }
125 
126 bool WireWriter::writeUI16(std::uint16_t x) const
127 {
128  return writeI16(reinterpret_cast<std::int16_t&>(x));
129 }
130 
131 bool WireWriter::writeUI32(std::uint32_t x) const
132 {
133  return writeI32(reinterpret_cast<std::int32_t&>(x));
134 }
135 
136 bool WireWriter::writeUI64(std::uint64_t x) const
137 {
138  return writeI64(reinterpret_cast<std::int64_t&>(x));
139 }
140 
142 {
144  writer.appendInt32(x);
145  return !writer.isError();
146 }
147 
149 {
150  int tmp = x;
151  return writeI32(tmp);
152 }
153 
155 {
156  return writer.isValid();
157 }
158 
160 {
161  return writer.isError();
162 }
163 
164 bool WireWriter::writeTag(const char* tag, int split, int len) const
165 {
166  YARP_UNUSED(len);
167  if (split == 0) {
168  return writeString(tag);
169  }
170  std::string bit;
171  char ch = 'x';
172  while (ch != '\0') {
173  ch = *tag;
174  tag++;
175  if (ch == '\0' || ch == '_') {
176  if (bit.length() <= 4) {
177  writeVocab32(bit);
178  } else {
179  writeString(bit);
180  }
181  bit.clear();
182  } else {
183  bit += ch;
184  }
185  }
186  return true;
187 }
188 
189 bool WireWriter::writeString(const std::string& tag) const
190 {
192  // WARNING tag.length() value is not checked here
193  writer.appendString(tag);
194  return !writer.isError();
195 }
196 
197 bool WireWriter::writeBinary(const std::string& tag) const
198 {
200  // WARNING tag.length() value is not checked here
201  writer.appendInt32(static_cast<int>(tag.length()));
202  writer.appendBlock(tag.c_str(), tag.length());
203  return !writer.isError();
204 }
205 
206 bool WireWriter::writeListHeader(int len) const
207 {
209  if (get_mode) {
210  writer.appendInt32(len + 3);
212  writer.appendInt32(VOCAB_IS);
213  if (get_is_vocab32) {
215  writer.appendInt32(Vocab32::encode(get_string));
216  } else {
217  writeString(get_string);
218  }
219  need_ok = true;
220  } else {
221  writer.appendInt32(len);
222  }
223  return !writer.isError();
224 }
225 
226 
227 bool WireWriter::writeListBegin(int tag, std::uint32_t len) const
228 {
229  YARP_UNUSED(tag);
230  // this should be optimized for double/int/etc
232  writer.appendInt32(static_cast<int>(len));
233  return !writer.isError();
234 }
235 
236 bool WireWriter::writeSetBegin(int tag, std::uint32_t len) const
237 {
238  return writeListBegin(tag, len);
239 }
240 
241 bool WireWriter::writeMapBegin(int tag, int tag2, std::uint32_t len) const
242 {
243  YARP_UNUSED(tag);
244  YARP_UNUSED(tag2);
246  writer.appendInt32(static_cast<int>(len));
247  return !writer.isError();
248 }
249 
251 {
252  return true;
253 }
254 
256 {
257  return true;
258 }
259 
261 {
262  return true;
263 }
264 
266 {
267  if (!writeListHeader(1)) {
268  return false;
269  }
271  writer.appendInt32(VOCAB_DONE);
272  return true;
273 }
#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
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.
virtual void appendString(const char *str, const char terminate='\n') final
Send a character sequence to the network connection.
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
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:23
virtual bool write(const yarp::os::idl::WireWriter &writer) const
IDL-friendly connection reader.
Definition: WireReader.h:30
const std::string & getString() const
Definition: WireReader.cpp:708
bool writeI64(std::int64_t x) const
Definition: WireWriter.cpp:100
bool writeUI16(std::uint16_t x) const
Definition: WireWriter.cpp:126
bool writeUI64(std::uint64_t x) const
Definition: WireWriter.cpp:136
bool writeVocab32(yarp::conf::vocab32_t x) const
Definition: WireWriter.cpp:141
bool writeI32(std::int32_t x) const
Definition: WireWriter.cpp:93
bool writeUI32(std::uint32_t x) const
Definition: WireWriter.cpp:131
bool write(const WirePortable &obj) const
Definition: WireWriter.cpp:52
bool writeUI8(std::uint8_t x) const
Definition: WireWriter.cpp:121
bool writeBool(bool x) const
Definition: WireWriter.cpp:72
bool writeListHeader(int len) const
Definition: WireWriter.cpp:206
bool writeTag(const char *tag, int split, int len) const
Definition: WireWriter.cpp:164
bool writeMapBegin(int tag, int tag2, std::uint32_t len) const
Definition: WireWriter.cpp:241
bool writeListBegin(int tag, std::uint32_t len) const
Definition: WireWriter.cpp:227
bool writeFloat64(yarp::conf::float64_t x) const
Definition: WireWriter.cpp:114
bool writeOnewayResponse() const
Definition: WireWriter.cpp:265
bool writeString(const std::string &tag) const
Definition: WireWriter.cpp:189
bool writeNested(const WirePortable &obj) const
Definition: WireWriter.cpp:62
bool writeSizeT(std::size_t x) const
Definition: WireWriter.cpp:148
WireWriter(ConnectionWriter &writer)
Definition: WireWriter.cpp:19
bool writeSetBegin(int tag, std::uint32_t len) const
Definition: WireWriter.cpp:236
bool writeI16(std::int16_t x) const
Definition: WireWriter.cpp:86
bool writeI8(std::int8_t x) const
Definition: WireWriter.cpp:79
bool writeBinary(const std::string &tag) const
Definition: WireWriter.cpp:197
bool writeFloat32(yarp::conf::float32_t x) const
Definition: WireWriter.cpp:107
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:27
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:28
#define YARP_UNUSED(var)
Definition: api.h:162