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
195{
196 implementation->edit();
197 return implementation->addDict();
198}
199
201{
202 implementation->edit();
203 Storable* stb = implementation->pop();
204 Value val(*stb);
205 // here we take responsibility for deallocation of the Storable instance
206 delete stb;
207 return val;
208}
209
210void Bottle::fromString(const std::string& text)
211{
212 implementation->edit();
213 implementation->invalid = false;
214 implementation->fromString(text);
215}
216
217std::string Bottle::toString() const
218{
219 return implementation->toString();
220}
221
222void Bottle::fromBinary(const char* buf, size_t len)
223{
224 implementation->edit();
225 implementation->fromBinary(buf, len);
226}
227
228const char* Bottle::toBinary(size_t* size)
229{
230 if (size != nullptr) {
231 *size = implementation->byteCount();
232 }
233 return implementation->getBytes();
234}
235
237{
238 return implementation->write(writer);
239}
240
242{
243 implementation->onCommencement();
244}
245
247{
248 implementation->edit();
249 return implementation->read(reader);
250}
251
252Value& Bottle::get(size_t index) const
253{
254 return implementation->get(index);
255}
256
257size_t Bottle::size() const
258{
259 return static_cast<int>(implementation->size());
260}
261
263{
264 implementation->hasChanged();
265}
266
268{
269 return implementation->getSpecialization();
270}
271
272void Bottle::copy(const Bottle& alt, size_t first, size_t len)
273{
274 implementation->edit();
275 if (alt.isNull()) {
276 clear();
277 implementation->invalid = true;
278 return;
279 }
280 implementation->copyRange(alt.implementation, first, len);
281}
282
283bool Bottle::check(const std::string& key) const
284{
285 Bottle& val = findGroup(key);
286 if (!val.isNull()) {
287 return true;
288 }
289 Value& val2 = find(key);
290 return !val2.isNull();
291}
292
293Value& Bottle::find(const std::string& key) const
294{
295 Value& val = implementation->findBit(key);
296 return val;
297}
298
299Bottle& Bottle::findGroup(const std::string& key) const
300{
301 Value& bb = implementation->findGroupBit(key);
302
303 if (bb.isList()) {
304 return *(bb.asList());
305 }
306 return getNullBottle();
307}
308
309void Bottle::add(Value* value)
310{
311 implementation->edit();
312 implementation->addBit(value);
313}
314
315void Bottle::add(const Value& value)
316{
317 implementation->edit();
318 implementation->addBit(value);
319}
320
322{
323 static NullBottle bottleNull;
324 return bottleNull;
325}
326
327bool Bottle::operator==(const Bottle& alt) const
328{
329 return toString() == alt.toString();
330}
331
332bool Bottle::write(PortReader& reader, bool textMode)
333{
334 DummyConnector con;
335 con.setTextMode(textMode);
336 write(con.getWriter());
337 return reader.read(con.getReader());
338}
339
340bool Bottle::read(const PortWriter& writer, bool textMode)
341{
342 implementation->edit();
343 DummyConnector con;
344 con.setTextMode(textMode);
345 writer.write(con.getWriter());
346 return read(con.getReader());
347}
348
349bool Bottle::isNull() const
350{
351 return implementation->invalid;
352}
353
354bool Bottle::operator!=(const Bottle& alt) const
355{
356 return !((*this) == alt);
357}
358
360{
361 implementation->edit();
362 for (size_t i = 0; i < alt.size(); i++) {
363 add(alt.get(i));
364 }
365}
366
368{
369 Bottle b;
370 if (isNull()) {
371 return *this;
372 }
373 b.copy(*this, 1, size() - 1);
374 return b;
375}
376
377std::string Bottle::toString(int x)
378{
380}
381
382std::string Bottle::describeBottleCode(int code)
383{
384 int unit = code & ~(BOTTLE_TAG_LIST | BOTTLE_TAG_DICT);
385 std::string unitName = "mixed";
386 switch (unit) {
387 case 0:
388 unitName = "mixed";
389 break;
390 case BOTTLE_TAG_INT32:
391 unitName = "int32";
392 break;
394 unitName = "vocab32";
395 break;
397 unitName = "vocab64";
398 break;
400 unitName = "float64";
401 break;
403 unitName = "string";
404 break;
405 case BOTTLE_TAG_BLOB:
406 unitName = "blob";
407 break;
408 default:
409 unitName = "unknown";
410 break;
411 }
412 std::string result = unitName;
413 if ((code & BOTTLE_TAG_LIST) != 0) {
414 result = "list of " + unitName;
415 } else if ((code & BOTTLE_TAG_DICT) != 0) {
416 result = "dict of " + unitName;
417 }
418 return result;
419}
420
422{
424}
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:26
#define BOTTLE_TAG_VOCAB64
Definition Bottle.h:24
#define BOTTLE_TAG_DICT
Definition Bottle.h:30
#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:65
static Bottle & getNullBottle()
A special Bottle with no content.
Definition Bottle.cpp:321
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:315
static std::string describeBottleCode(int code)
Convert a numeric bottle code to a string.
Definition Bottle.cpp:382
Value pop()
Removes a Value v from the end of the list and returns this value.
Definition Bottle.cpp:200
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:210
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
Definition Bottle.cpp:359
size_t size_type
Definition Bottle.h:70
int getSpecialization()
Get numeric bottle code for this bottle.
Definition Bottle.cpp:267
~Bottle() override
Destructor.
Definition Bottle.cpp:116
Property & addDict()
Places an empty key/value object in the bottle, at the end of the list.
Definition Bottle.cpp:194
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:257
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:246
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:421
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:252
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition Bottle.cpp:299
void copy(const Bottle &alt, size_type first=0, size_type len=npos)
Copy all or part of another Bottle.
Definition Bottle.cpp:272
static const size_type npos
Definition Bottle.h:73
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition Bottle.cpp:283
Bottle tail() const
Get all but the first element of a bottle.
Definition Bottle.cpp:367
bool operator==(const Bottle &alt) const
Equality test.
Definition Bottle.cpp:327
void onCommencement() const override
This is called when the port is about to begin writing operations.
Definition Bottle.cpp:241
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:236
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:354
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:349
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:217
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:262
const char * toBinary(size_t *size=nullptr)
Returns binary representation of bottle.
Definition Bottle.cpp:228
void fromBinary(const char *buf, size_t len)
Initializes bottle from a binary representation.
Definition Bottle.cpp:222
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Bottle.cpp:293
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:33
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:44
#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