YARP
Yet Another Robot Platform
WireWriter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 using namespace yarp::os;
12 using namespace yarp::os::idl;
13 
14 namespace {
16 constexpr yarp::conf::vocab32_t VOCAB_FAIL = yarp::os::createVocab('f', 'a', 'i', 'l');
18 constexpr yarp::conf::vocab32_t VOCAB_DONE = yarp::os::createVocab('d', 'o', 'n', 'e');
19 } // namespace
20 
21 
23  writer(writer)
24 {
25  get_mode = get_is_vocab = false;
26  need_ok = false;
27  writer.convertTextMode();
28 }
29 
31  writer(reader.getWriter())
32 {
33  get_is_vocab = false;
34  need_ok = false;
35  writer.convertTextMode();
36  get_mode = reader.getMode();
37  if (get_mode) {
38  get_string = reader.getString();
39  get_is_vocab = reader.getIsVocab();
40  }
41 }
42 
44 {
45  if (need_ok) {
46  writeBool(true);
47  }
48 }
49 
50 bool WireWriter::isNull() const
51 {
52  return writer.isNull();
53 }
54 
55 bool WireWriter::write(const WirePortable& obj) const
56 {
57  return obj.write(*this);
58 }
59 
61 {
62  return obj.write(writer);
63 }
64 
65 bool WireWriter::writeNested(const WirePortable& obj) const
66 {
67  return obj.write(writer);
68 }
69 
71 {
72  return obj.write(writer);
73 }
74 
75 bool WireWriter::writeBool(bool x) const
76 {
78  writer.appendInt32(x ? VOCAB_OK : VOCAB_FAIL);
79  return !writer.isError();
80 }
81 
82 bool WireWriter::writeI8(std::int8_t x) const
83 {
85  writer.appendInt8(x);
86  return !writer.isError();
87 }
88 
89 bool WireWriter::writeI16(std::int16_t x) const
90 {
92  writer.appendInt16(x);
93  return !writer.isError();
94 }
95 
96 bool WireWriter::writeI32(std::int32_t x) const
97 {
99  writer.appendInt32(x);
100  return !writer.isError();
101 }
102 
103 bool WireWriter::writeI64(std::int64_t x) const
104 {
106  writer.appendInt64(x);
107  return !writer.isError();
108 }
109 
111 {
113  writer.appendFloat32(x);
114  return !writer.isError();
115 }
116 
118 {
120  writer.appendFloat64(x);
121  return !writer.isError();
122 }
123 
124 bool WireWriter::writeVocab(std::int32_t x) const
125 {
127  writer.appendInt32(x);
128  return !writer.isError();
129 }
130 
132 {
133  return writer.isValid();
134 }
135 
137 {
138  return writer.isError();
139 }
140 
141 bool WireWriter::writeTag(const char* tag, int split, int len) const
142 {
143  YARP_UNUSED(len);
144  if (split == 0) {
145  return writeString(tag);
146  }
147  std::string bit;
148  char ch = 'x';
149  while (ch != '\0') {
150  ch = *tag;
151  tag++;
152  if (ch == '\0' || ch == '_') {
153  if (bit.length() <= 4) {
155  } else {
156  writeString(bit);
157  }
158  bit.clear();
159  } else {
160  bit += ch;
161  }
162  }
163  return true;
164 }
165 
166 bool WireWriter::writeString(const std::string& tag) const
167 {
169  // WARNING tag.length() value is not checked here
170  writer.appendString(tag);
171  return !writer.isError();
172 }
173 
174 bool WireWriter::writeBinary(const std::string& tag) const
175 {
177  // WARNING tag.length() value is not checked here
178  writer.appendInt32(static_cast<int>(tag.length()));
179  writer.appendBlock(tag.c_str(), tag.length());
180  return !writer.isError();
181 }
182 
183 bool WireWriter::writeListHeader(int len) const
184 {
186  if (get_mode) {
187  writer.appendInt32(len + 3);
189  writer.appendInt32(VOCAB_IS);
190  if (get_is_vocab) {
192  writer.appendInt32(Vocab::encode(get_string));
193  } else {
194  writeString(get_string);
195  }
196  need_ok = true;
197  } else {
198  writer.appendInt32(len);
199  }
200  return !writer.isError();
201 }
202 
203 
204 bool WireWriter::writeListBegin(int tag, std::uint32_t len) const
205 {
206  YARP_UNUSED(tag);
207  // this should be optimized for double/int/etc
209  writer.appendInt32(static_cast<int>(len));
210  return !writer.isError();
211 }
212 
213 bool WireWriter::writeSetBegin(int tag, std::uint32_t len) const
214 {
215  return writeListBegin(tag, len);
216 }
217 
218 bool WireWriter::writeMapBegin(int tag, int tag2, std::uint32_t len) const
219 {
220  YARP_UNUSED(tag);
221  YARP_UNUSED(tag2);
223  writer.appendInt32(static_cast<int>(len));
224  return !writer.isError();
225 }
226 
228 {
229  return true;
230 }
231 
233 {
234  return true;
235 }
236 
238 {
239  return true;
240 }
241 
243 {
244  if (!writeListHeader(1)) {
245  return false;
246  }
248  writer.appendInt32(VOCAB_DONE);
249  return true;
250 }
#define BOTTLE_TAG_INT8
Definition: Bottle.h:21
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
#define BOTTLE_TAG_VOCAB
Definition: Bottle.h:25
#define BOTTLE_TAG_INT64
Definition: Bottle.h:24
#define BOTTLE_TAG_INT16
Definition: Bottle.h:22
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
#define BOTTLE_TAG_STRING
Definition: Bottle.h:28
#define BOTTLE_TAG_BLOB
Definition: Bottle.h:29
#define BOTTLE_TAG_LIST
Definition: Bottle.h:30
#define BOTTLE_TAG_FLOAT32
Definition: Bottle.h:26
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:18
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:17
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:27
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:26
virtual bool write(const yarp::os::idl::WireWriter &writer) const
IDL-friendly connection reader.
Definition: WireReader.h:33
const std::string & getString() const
Definition: WireReader.cpp:665
bool writeI64(std::int64_t x) const
Definition: WireWriter.cpp:103
bool writeI32(std::int32_t x) const
Definition: WireWriter.cpp:96
bool write(const WirePortable &obj) const
Definition: WireWriter.cpp:55
bool writeBool(bool x) const
Definition: WireWriter.cpp:75
bool writeListHeader(int len) const
Definition: WireWriter.cpp:183
bool writeTag(const char *tag, int split, int len) const
Definition: WireWriter.cpp:141
bool writeMapBegin(int tag, int tag2, std::uint32_t len) const
Definition: WireWriter.cpp:218
bool writeListBegin(int tag, std::uint32_t len) const
Definition: WireWriter.cpp:204
bool writeFloat64(yarp::conf::float64_t x) const
Definition: WireWriter.cpp:117
bool writeOnewayResponse() const
Definition: WireWriter.cpp:242
bool writeString(const std::string &tag) const
Definition: WireWriter.cpp:166
bool writeNested(const WirePortable &obj) const
Definition: WireWriter.cpp:65
WireWriter(ConnectionWriter &writer)
Definition: WireWriter.cpp:22
bool writeSetBegin(int tag, std::uint32_t len) const
Definition: WireWriter.cpp:213
bool writeVocab(std::int32_t x) const
Definition: WireWriter.cpp:124
bool writeI16(std::int16_t x) const
Definition: WireWriter.cpp:89
bool writeI8(std::int8_t x) const
Definition: WireWriter.cpp:82
bool writeBinary(const std::string &tag) const
Definition: WireWriter.cpp:174
bool writeFloat32(yarp::conf::float32_t x) const
Definition: WireWriter.cpp:110
std::int32_t vocab32_t
Definition: numeric.h:52
double float64_t
Definition: numeric.h:51
float float32_t
Definition: numeric.h:50
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
An interface to the operating system, including Port based communication.
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
#define YARP_UNUSED(var)
Definition: api.h:159