YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Bottle.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-FileCopyrightText: 2006, 2008 Arjan Gijsberts
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include <yarp/os/Bottle.h>
10#include <yarp/os/NetType.h>
13
14
19using yarp::os::Value;
22
23namespace {
24YARP_OS_LOG_COMPONENT(BOTTLE, "yarp.os.Bottle")
25}
26
27// FIXME this can be constexpr, but swig 3.0.8 is not happy
28const Bottle::size_type Bottle::npos = static_cast<Bottle::size_type>(-1);
29
30class NullBottle : public Bottle
31{
32public:
34 Bottle()
35 {
36 setReadOnly(true);
37 }
38 bool isNull() const override
39 {
40 return true;
41 }
42};
43
45 Portable(),
46 Searchable(),
48{
49 yCAssert(BOTTLE, implementation != nullptr);
50 implementation->invalid = false;
51 implementation->ro = false;
52}
53
54Bottle::Bottle(const std::string& text) :
55 Portable(),
56 Searchable(),
58{
59 yCAssert(BOTTLE, implementation != nullptr);
60 implementation->invalid = false;
61 implementation->ro = false;
62 fromString(text);
63}
64
66 Portable(),
69{
70 yCAssert(BOTTLE, implementation != nullptr);
71 implementation->invalid = false;
72 implementation->ro = false;
73 copy(rhs);
74}
75
77 Portable(std::move(static_cast<Portable&>(rhs))),
78 Searchable(std::move(static_cast<Searchable&>(rhs))),
79 implementation(rhs.implementation)
80{
81 implementation->parent = this;
82 rhs.implementation = new BottleImpl(&rhs);
83}
84
85Bottle::Bottle(std::initializer_list<Value> values) :
86 Portable(),
87 Searchable(),
89{
90 yCAssert(BOTTLE, implementation != nullptr);
91 implementation->invalid = false;
92 implementation->ro = false;
93
94 for (const auto& val : values) {
95 add(val);
96 }
97}
98
100{
101 if (&rhs != this) {
102 implementation->edit();
103 copy(rhs);
104 }
105 return *this;
106}
107
109{
110 std::swap(implementation, rhs.implementation);
111 implementation->parent = this;
112 rhs.implementation->parent = &rhs;
113 return *this;
114}
115
117{
118 delete implementation;
119}
120
122{
123 implementation->edit();
124 implementation->invalid = false;
125 implementation->clear();
126}
127
128void Bottle::addInt8(std::int8_t x)
129{
130 implementation->edit();
131 implementation->addInt8(x);
132}
133
134void Bottle::addInt16(std::int16_t x)
135{
136 implementation->edit();
137 implementation->addInt16(x);
138}
139
140void Bottle::addInt32(std::int32_t x)
141{
142 implementation->edit();
143 implementation->addInt32(x);
144}
145
146void Bottle::addInt64(std::int64_t x)
147{
148 implementation->edit();
149 implementation->addInt64(x);
150}
151
153{
154 implementation->edit();
155 implementation->addFloat32(x);
156}
157
159{
160 implementation->edit();
161 implementation->addFloat64(x);
162}
163
165{
166 implementation->edit();
167 implementation->addVocab32(x);
168}
169
171{
172 implementation->edit();
173 implementation->addVocab64(x);
174}
175
176void Bottle::addString(const char* str)
177{
178 implementation->edit();
179 implementation->addString(str);
180}
181
182void Bottle::addString(const std::string& str)
183{
184 implementation->edit();
185 implementation->addString(str);
186}
187
189{
190 implementation->edit();
191 return implementation->addList();
192}
193
194
196{
197 implementation->edit();
198 Storable* stb = implementation->pop();
199 Value val(*stb);
200 // here we take responsibility for deallocation of the Storable instance
201 delete stb;
202 return val;
203}
204
205void Bottle::fromString(const std::string& text)
206{
207 implementation->edit();
208 implementation->invalid = false;
209 implementation->fromString(text);
210}
211
212std::string Bottle::toString() const
213{
214 return implementation->toString();
215}
216
217void Bottle::fromBinary(const char* buf, size_t len)
218{
219 implementation->edit();
220 implementation->fromBinary(buf, len);
221}
222
223const char* Bottle::toBinary(size_t* size)
224{
225 if (size != nullptr) {
226 *size = implementation->byteCount();
227 }
228 return implementation->getBytes();
229}
230
232{
233 return implementation->write(writer);
234}
235
237{
238 implementation->onCommencement();
239}
240
242{
243 implementation->edit();
244 return implementation->read(reader);
245}
246
247Value& Bottle::get(size_t index) const
248{
249 return implementation->get(index);
250}
251
252size_t Bottle::size() const
253{
254 return static_cast<int>(implementation->size());
255}
256
258{
259 implementation->hasChanged();
260}
261
263{
264 return implementation->getSpecialization();
265}
266
267void Bottle::copy(const Bottle& alt, size_t first, size_t len)
268{
269 implementation->edit();
270 if (alt.isNull()) {
271 clear();
272 implementation->invalid = true;
273 return;
274 }
275 implementation->copyRange(alt.implementation, first, len);
276}
277
278bool Bottle::check(const std::string& key) const
279{
280 Bottle& val = findGroup(key);
281 if (!val.isNull()) {
282 return true;
283 }
284 Value& val2 = find(key);
285 return !val2.isNull();
286}
287
288Value& Bottle::find(const std::string& key) const
289{
290 Value& val = implementation->findBit(key);
291 return val;
292}
293
294Bottle& Bottle::findGroup(const std::string& key) const
295{
296 Value& bb = implementation->findGroupBit(key);
297
298 if (bb.isList()) {
299 return *(bb.asList());
300 }
301 return getNullBottle();
302}
303
304void Bottle::add(Value* value)
305{
306 implementation->edit();
307 implementation->addBit(value);
308}
309
310void Bottle::add(const Value& value)
311{
312 implementation->edit();
313 implementation->addBit(value);
314}
315
317{
318 static NullBottle bottleNull;
319 return bottleNull;
320}
321
322bool Bottle::operator==(const Bottle& alt) const
323{
324 return toString() == alt.toString();
325}
326
327bool Bottle::write(PortReader& reader, bool textMode)
328{
329 DummyConnector con;
330 con.setTextMode(textMode);
331 write(con.getWriter());
332 return reader.read(con.getReader());
333}
334
335bool Bottle::read(const PortWriter& writer, bool textMode)
336{
337 implementation->edit();
338 DummyConnector con;
339 con.setTextMode(textMode);
340 writer.write(con.getWriter());
341 return read(con.getReader());
342}
343
344bool Bottle::isNull() const
345{
346 return implementation->invalid;
347}
348
349bool Bottle::operator!=(const Bottle& alt) const
350{
351 return !((*this) == alt);
352}
353
355{
356 implementation->edit();
357 for (size_t i = 0; i < alt.size(); i++) {
358 add(alt.get(i));
359 }
360}
361
363{
364 Bottle b;
365 if (isNull()) {
366 return *this;
367 }
368 b.copy(*this, 1, size() - 1);
369 return b;
370}
371
372std::string Bottle::toString(int x)
373{
375}
376
377std::string Bottle::describeBottleCode(int code)
378{
379 //unit is the code with the flag LIST removed
380 int unit = code & ~(BOTTLE_TAG_LIST);
381
382 std::string unitName = "mixed";
383 switch (unit) {
384 case 0:
385 unitName = "mixed";
386 break;
387 case BOTTLE_TAG_INT32:
388 unitName = "int32";
389 break;
391 unitName = "vocab32";
392 break;
394 unitName = "vocab64";
395 break;
397 unitName = "float64";
398 break;
400 unitName = "string";
401 break;
402 case BOTTLE_TAG_BLOB:
403 unitName = "blob";
404 break;
405 default:
406 unitName = "unknown";
407 break;
408 }
409 std::string result = unitName;
410 if ((code & BOTTLE_TAG_LIST) != 0) {
411 result = "list of " + unitName;
412 }
413 return result;
414}
415
417{
419}
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:26
#define BOTTLE_TAG_VOCAB64
Definition Bottle.h:24
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_STRING
Definition Bottle.h:27
#define BOTTLE_TAG_BLOB
Definition Bottle.h:28
#define BOTTLE_TAG_LIST
Definition Bottle.h:29
#define BOTTLE_TAG_VOCAB32
Definition Bottle.h:23
RandScalar * implementation(void *t)
bool isNull() const override
Checks if the object is invalid.
Definition Bottle.cpp:38
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
static Bottle & getNullBottle()
A special Bottle with no content.
Definition Bottle.cpp:316
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:310
static std::string describeBottleCode(int code)
Convert a numeric bottle code to a string.
Definition Bottle.cpp:377
Value pop()
Removes a Value v from the end of the list and returns this value.
Definition Bottle.cpp:195
Bottle & operator=(const Bottle &rhs)
Copy assignment operator.
Definition Bottle.cpp:99
void addVocab32(yarp::conf::vocab32_t x)
Places a 32 bit vocabulary item in the bottle, at the end of the list.
Definition Bottle.cpp:164
void fromString(const std::string &text)
Initializes bottle from a string.
Definition Bottle.cpp:205
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
Definition Bottle.cpp:354
size_t size_type
Definition Bottle.h:69
int getSpecialization()
Get numeric bottle code for this bottle.
Definition Bottle.cpp:262
~Bottle() override
Destructor.
Definition Bottle.cpp:116
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition Bottle.cpp:188
void addInt8(std::int8_t x)
Places a 8-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:128
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:252
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:241
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition Bottle.cpp:158
void addFloat32(yarp::conf::float32_t x)
Places a 32-bit floating point number in the bottle, at the end of the list.
Definition Bottle.cpp:152
void setReadOnly(bool readOnly)
Definition Bottle.cpp:416
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:247
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition Bottle.cpp:294
void copy(const Bottle &alt, size_type first=0, size_type len=npos)
Copy all or part of another Bottle.
Definition Bottle.cpp:267
static const size_type npos
Definition Bottle.h:72
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition Bottle.cpp:278
Bottle tail() const
Get all but the first element of a bottle.
Definition Bottle.cpp:362
bool operator==(const Bottle &alt) const
Equality test.
Definition Bottle.cpp:322
void onCommencement() const override
This is called when the port is about to begin writing operations.
Definition Bottle.cpp:236
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt16(std::int16_t x)
Places a 16-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:134
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition Bottle.cpp:231
void addInt64(std::int64_t x)
Places a 64-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:146
bool operator!=(const Bottle &alt) const
Inequality test.
Definition Bottle.cpp:349
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
bool isNull() const override
Checks if the object is invalid.
Definition Bottle.cpp:344
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:176
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:212
void addVocab64(yarp::conf::vocab64_t x)
Places a 64 bit vocabulary item in the bottle, at the end of the list.
Definition Bottle.cpp:170
void hasChanged()
Declare that the content of the Bottle has been changed.
Definition Bottle.cpp:257
const char * toBinary(size_t *size=nullptr)
Returns binary representation of bottle.
Definition Bottle.cpp:223
void fromBinary(const char *buf, size_t len)
Initializes bottle from a binary representation.
Definition Bottle.cpp:217
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Bottle.cpp:288
Bottle()
Constructor.
Definition Bottle.cpp:44
A mini-server for performing network communication in the background.
T * read(bool shouldWait=true) override
Read an available object from the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
An interface for reading from a network connection.
An interface for writing to a network connection.
A dummy connection to test yarp::os::Portable implementations.
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
void setTextMode(bool textmode)
Set the textMode of the dummy connection.
ConnectionReader & getReader(ConnectionWriter *replyWriter=nullptr)
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
virtual bool read(ConnectionReader &reader)=0
Read this object from a 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.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition Portable.h:25
A class for storing options and configuration information.
Definition Property.h:32
A base class for nested structures that can be searched.
Definition Searchable.h:31
A single value (typically within a Bottle).
Definition Value.h:44
A flexible data format for holding a bunch of numbers and strings.
Definition BottleImpl.h:34
A single item in a Bottle.
Definition Storable.h:43
#define yCAssert(component, x)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string to_string(IntegerType x)
Definition numeric.h:116
std::int32_t vocab32_t
Definition numeric.h:78
double float64_t
Definition numeric.h:77
float float32_t
Definition numeric.h:76
std::int64_t vocab64_t
Definition numeric.h:79