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 }
yarp::os::idl::WireWriter::~WireWriter
~WireWriter()
Definition: WireWriter.cpp:43
yarp::os::ConnectionWriter::appendFloat32
virtual void appendFloat32(yarp::conf::float32_t data)=0
Send a representation of a 32-bit floating point number to the network connection.
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
yarp::os::idl::WireReader::getIsVocab
bool getIsVocab() const
Definition: WireReader.cpp:660
yarp::os::ConnectionWriter::appendBlock
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
yarp::os::ConnectionWriter::isNull
virtual bool isNull() const
Definition: ConnectionWriter.cpp:19
yarp::os::idl::WireWriter::writeFloat64
bool writeFloat64(yarp::conf::float64_t x) const
Definition: WireWriter.cpp:117
yarp::os::idl::WireWriter::writeOnewayResponse
bool writeOnewayResponse() const
Definition: WireWriter.cpp:242
BOTTLE_TAG_LIST
#define BOTTLE_TAG_LIST
Definition: Bottle.h:30
yarp::os::idl::WireWriter::isValid
bool isValid() const
Definition: WireWriter.cpp:131
yarp::os::ConnectionWriter::appendFloat64
virtual void appendFloat64(yarp::conf::float64_t data)=0
Send a representation of a 64-bit floating point number to the network connection.
yarp::os::idl::WireWriter::writeI8
bool writeI8(std::int8_t x) const
Definition: WireWriter.cpp:82
yarp::os::ConnectionWriter::appendInt8
virtual void appendInt8(std::int8_t data)=0
Send a representation of a 8-bit integer to the network connection.
BOTTLE_TAG_STRING
#define BOTTLE_TAG_STRING
Definition: Bottle.h:28
yarp::os::idl::WireWriter::writeMapBegin
bool writeMapBegin(int tag, int tag2, std::uint32_t len) const
Definition: WireWriter.cpp:218
yarp::os::idl::WireWriter::WireWriter
WireWriter(ConnectionWriter &writer)
Definition: WireWriter.cpp:22
yarp::os::idl::WireReader::getMode
bool getMode() const
Definition: WireReader.cpp:655
BOTTLE_TAG_INT8
#define BOTTLE_TAG_INT8
Definition: Bottle.h:21
yarp::os::idl::WireWriter::writeString
bool writeString(const std::string &tag) const
Definition: WireWriter.cpp:166
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::idl::WireWriter::writeBool
bool writeBool(bool x) const
Definition: WireWriter.cpp:75
yarp::os::idl::WireReader::getString
const std::string & getString() const
Definition: WireReader.cpp:665
yarp::os::idl::WireWriter::writeI32
bool writeI32(std::int32_t x) const
Definition: WireWriter.cpp:96
yarp::os::idl::WireWriter::isNull
bool isNull() const
Definition: WireWriter.cpp:50
yarp::os::PortWriter
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
yarp::os::idl::WireWriter::isError
bool isError() const
Definition: WireWriter.cpp:136
yarp::os::idl::WireWriter::writeListHeader
bool writeListHeader(int len) const
Definition: WireWriter.cpp:183
BOTTLE_TAG_INT32
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
yarp::os::idl::WireWriter::writeNested
bool writeNested(const WirePortable &obj) const
Definition: WireWriter.cpp:65
yarp::os::ConnectionWriter::isError
virtual bool isError() const =0
yarp::os::idl
Definition: BareStyle.h:18
BOTTLE_TAG_VOCAB
#define BOTTLE_TAG_VOCAB
Definition: Bottle.h:25
BOTTLE_TAG_FLOAT32
#define BOTTLE_TAG_FLOAT32
Definition: Bottle.h:26
yarp::os::idl::WireWriter::writeI16
bool writeI16(std::int16_t x) const
Definition: WireWriter.cpp:89
yarp::os::idl::WireWriter::writeSetBegin
bool writeSetBegin(int tag, std::uint32_t len) const
Definition: WireWriter.cpp:213
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
BOTTLE_TAG_INT64
#define BOTTLE_TAG_INT64
Definition: Bottle.h:24
WireWriter.h
yarp::os::Vocab::encode
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
yarp::os::idl::WireWriter::writeMapEnd
bool writeMapEnd() const
Definition: WireWriter.cpp:237
yarp::os::idl::WireWriter::write
bool write(const WirePortable &obj) const
Definition: WireWriter.cpp:55
yarp::os::idl::WirePortable
A "tamed" Portable, that promises to serialize itself in an IDL-friendly way.
Definition: WirePortable.h:26
yarp::os::idl::WireWriter::writeBinary
bool writeBinary(const std::string &tag) const
Definition: WireWriter.cpp:174
yarp::os::PortWriter::write
virtual bool write(ConnectionWriter &writer) const =0
Write this object to a network connection.
yarp::os::idl::WireWriter::writeSetEnd
bool writeSetEnd() const
Definition: WireWriter.cpp:232
yarp::os::ConnectionWriter::convertTextMode
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
yarp::os::idl::WireWriter::writeTag
bool writeTag(const char *tag, int split, int len) const
Definition: WireWriter.cpp:141
yarp::conf::float32_t
float float32_t
Definition: numeric.h:50
yarp::os::idl::WireWriter::writeFloat32
bool writeFloat32(yarp::conf::float32_t x) const
Definition: WireWriter.cpp:110
yarp::os::ConnectionWriter::appendInt32
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
yarp::os::idl::WireReader
IDL-friendly connection reader.
Definition: WireReader.h:33
BOTTLE_TAG_BLOB
#define BOTTLE_TAG_BLOB
Definition: Bottle.h:29
yarp::os::ConnectionWriter::appendInt64
virtual void appendInt64(std::int64_t data)=0
Send a representation of a 64-bit integer to the network connection.
BOTTLE_TAG_FLOAT64
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
yarp::os::idl::WireWriter::writeVocab
bool writeVocab(std::int32_t x) const
Definition: WireWriter.cpp:124
yarp::os::idl::WireWriter::writeListBegin
bool writeListBegin(int tag, std::uint32_t len) const
Definition: WireWriter.cpp:204
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::ConnectionWriter::appendString
virtual void appendString(const char *str, const char terminate='\n') final
Send a character sequence to the network connection.
Definition: ConnectionWriter.h:131
yarp::conf::vocab32_t
std::int32_t vocab32_t
Definition: numeric.h:52
yarp::os::idl::WireWriter::writeListEnd
bool writeListEnd() const
Definition: WireWriter.cpp:227
yarp::os::idl::WireWriter::writeI64
bool writeI64(std::int64_t x) const
Definition: WireWriter.cpp:103
yarp::conf::float64_t
double float64_t
Definition: numeric.h:51
BOTTLE_TAG_INT16
#define BOTTLE_TAG_INT16
Definition: Bottle.h:22
VOCAB_IS
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:17
yarp::os::ConnectionWriter::appendInt16
virtual void appendInt16(std::int16_t data)=0
Send a representation of a 16-bit integer to the network connection.
VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:18
yarp::os::ConnectionWriter::isValid
virtual bool isValid() const =0
yarp::os::idl::WirePortable::write
virtual bool write(const yarp::os::idl::WireWriter &writer) const
Definition: WirePortable.cpp:20