YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Value.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-License-Identifier: BSD-3-Clause
5 */
6
7#include <yarp/os/Value.h>
8
9#include <yarp/os/Bottle.h>
13
14using namespace yarp::os;
15using namespace yarp::os::impl;
16
17
19 Portable(),
20 Searchable(),
21 proxy(nullptr)
22{
23}
24
25Value::Value(std::int32_t x, bool isVocab32) :
26 Portable(),
27 Searchable(),
28 proxy(nullptr)
29{
30 if (!isVocab32) {
31 setProxy(static_cast<Storable*>(makeInt32(x)));
32 } else {
33 setProxy(static_cast<Storable*>(makeVocab32(x)));
34 }
35}
36
38 Portable(),
39 Searchable(),
40 proxy(nullptr)
41{
42 setProxy(static_cast<Storable*>(makeFloat64(x)));
43}
44
45Value::Value(const std::string& str, bool isVocab32) :
46 Portable(),
47 Searchable(),
48 proxy(nullptr)
49{
50 if (!isVocab32) {
51 setProxy(static_cast<Storable*>(makeString(str)));
52 } else {
53 setProxy(static_cast<Storable*>(makeVocab32(str)));
54 }
55}
56
57Value::Value(void* data, int length) :
58 Portable(),
59 Searchable(),
60 proxy(nullptr)
61{
62 setProxy(static_cast<Storable*>(makeBlob(data, length)));
63}
64
66 Portable(),
68 proxy(nullptr)
69{
70 setProxy(static_cast<Storable*>(alt.clone()));
71}
72
73
75{
76 if (&alt != this) {
77 if (proxy == nullptr) {
78 if (isLeaf() && (alt.proxy != nullptr)) {
79 // we are guaranteed to be a Storable
80 ((Storable*)this)->copy(*((Storable*)alt.proxy));
81 } else {
82 setProxy(static_cast<Storable*>(alt.clone()));
83 }
84 } else {
85 if (alt.proxy != nullptr) {
86 if (getCode() == alt.getCode()) {
87 // proxies are guaranteed to be Storable
88 ((Storable*)proxy)->copy(*((Storable*)alt.proxy));
89 } else {
90 setProxy(static_cast<Storable*>(alt.clone()));
91 }
92 } else {
93 if (proxy != nullptr) {
94 delete proxy;
95 proxy = nullptr;
96 }
97 if (alt.isLeaf()) {
98 setProxy(static_cast<Storable*>(alt.clone()));
99 }
100 }
101 }
102 }
103 return *this;
104}
105
107{
108 if (proxy != nullptr) {
109 delete proxy;
110 proxy = nullptr;
111 }
112}
113
114bool Value::isBool() const
115{
116 ok();
117 return proxy->isBool();
118}
119
120bool Value::isInt8() const
121{
122 ok();
123 return proxy->isInt8();
124}
125
126bool Value::isInt16() const
127{
128 ok();
129 return proxy->isInt16();
130}
131
132bool Value::isInt32() const
133{
134 ok();
135 return proxy->isInt32();
136}
137
138bool Value::isInt64() const
139{
140 ok();
141 return proxy->isInt64();
142}
143
145{
146 ok();
147 return proxy->isFloat32();
148}
149
151{
152 ok();
153 return proxy->isFloat64();
154}
155
156bool Value::isString() const
157{
158 ok();
159 return proxy->isString();
160}
161
162bool Value::isList() const
163{
164 ok();
165 return proxy->isList();
166}
167
168bool Value::isDict() const
169{
170 ok();
171 return proxy->isDict();
172}
173
175{
176 ok();
177 return proxy->isVocab32();
178}
179
181{
182 ok();
183 return proxy->isVocab64();
184}
185
186bool Value::isBlob() const
187{
188 ok();
189 return proxy->isBlob();
190}
191
192bool Value::asBool() const
193{
194 ok();
195 return proxy->asBool();
196}
197
198std::int8_t Value::asInt8() const
199{
200 ok();
201 return proxy->asInt8();
202}
203
204std::int16_t Value::asInt16() const
205{
206 ok();
207 return proxy->asInt16();
208}
209
210std::int32_t Value::asInt32() const
211{
212 ok();
213 return proxy->asInt32();
214}
215
216std::int64_t Value::asInt64() const
217{
218 ok();
219 return proxy->asInt64();
220}
221
223{
224 ok();
225 return proxy->asFloat32();
226}
227
229{
230 ok();
231 return proxy->asFloat64();
232}
233
235{
236 ok();
237 return proxy->asVocab32();
238}
239
241{
242 ok();
243 return proxy->asVocab64();
244}
245
246std::string Value::asString() const
247{
248 ok();
249 return proxy->asString();
250}
251
253{
254 ok();
255 return proxy->asList();
256}
257
259{
260 ok();
261 return proxy->asDict();
262}
263
265{
266 ok();
267 if (proxy->isDict()) {
268 return proxy->asDict();
269 }
270 return proxy->asList();
271}
272
273const char* Value::asBlob() const
274{
275 ok();
276 return proxy->asBlob();
277}
278
280{
281 ok();
282 return proxy->asBlobLength();
283}
284
286{
287 if (proxy != nullptr) {
288 delete proxy;
289 proxy = nullptr;
290 }
291 std::int32_t x = connection.expectInt32();
292 if ((x & 0xffff) != x) {
293 return false;
294 }
295 if ((x & BOTTLE_TAG_LIST) == 0) {
296 return false;
297 }
298 std::int32_t len = connection.expectInt32();
299 if (len == 0) {
300 return true;
301 }
302 if (len != 1) {
303 return false;
304 }
305 if (x == BOTTLE_TAG_LIST) {
306 x = connection.expectInt32();
307 } else {
308 x &= ~BOTTLE_TAG_LIST;
309 }
310 if (connection.isError()) {
311 return false;
312 }
314 setProxy(s);
315 if (proxy == nullptr) {
316 return false;
317 }
318 return s->readRaw(connection);
319}
320
321bool Value::write(ConnectionWriter& connection) const
322{
323 if (proxy == nullptr) {
324 connection.appendInt32(BOTTLE_TAG_LIST);
325 connection.appendInt32(0);
326 return !connection.isError();
327 }
328 connection.appendInt32(BOTTLE_TAG_LIST);
329 connection.appendInt32(1);
330 return proxy->write(connection);
331}
332
333bool Value::check(const std::string& key) const
334{
335 ok();
336 return proxy->check(key);
337}
338
339Value& Value::find(const std::string& key) const
340{
341 ok();
342 return proxy->find(key);
343}
344
345Bottle& Value::findGroup(const std::string& key) const
346{
347 ok();
348 return proxy->findGroup(key);
349}
350
351bool Value::operator==(const Value& alt) const
352{
353 ok();
354 return (*proxy) == alt;
355}
356
357
358bool Value::operator!=(const Value& alt) const
359{
360 return !((*this) == alt);
361}
362
363void Value::fromString(const char* str)
364{
365 setProxy(static_cast<Storable*>(makeValue(str)));
366}
367
368std::string Value::toString() const
369{
370 ok();
371 return proxy->toString();
372}
373
375{
376 ok();
377 return proxy->create();
378}
379
381{
382 ok();
383 return proxy->clone();
384}
385
386int Value::getCode() const
387{
388 ok();
389 return proxy->getCode();
390}
391
392bool Value::isNull() const
393{
394 ok();
395 return proxy->isNull();
396}
397
398bool Value::isLeaf() const
399{
400 return false;
401}
402
403Value* Value::makeInt8(std::int8_t x)
404{
405 return new StoreInt8(x);
406}
407
408Value* Value::makeInt16(std::int16_t x)
409{
410 return new StoreInt16(x);
411}
412
413Value* Value::makeInt32(std::int32_t x)
414{
415 return new StoreInt32(x);
416}
417
418Value* Value::makeInt64(std::int64_t x)
419{
420 return new StoreInt64(x);
421}
422
427
432
433Value* Value::makeString(const std::string& str)
434{
435 return new StoreString(str);
436}
437
438
443
444
445Value* Value::makeBlob(void* data, int length)
446{
447 std::string s((char*)data, length);
448 return new StoreBlob(s);
449}
450
451
453{
454 return new StoreList();
455}
456
457
458Value* Value::makeList(const char* txt)
459{
460 Value* v = makeList();
461 if (v != nullptr) {
462 v->asList()->fromString(txt);
463 }
464 return v;
465}
466
467
468Value* Value::makeValue(const std::string& txt)
469{
470 Bottle bot(txt);
471 if (bot.size() > 1) {
472 return makeString(txt);
473 }
474 return bot.get(0).clone();
475}
476
477
482
483
484void Value::setProxy(Storable* proxy)
485{
486 if (this->proxy != nullptr) {
487 delete this->proxy;
488 this->proxy = nullptr;
489 }
490 this->proxy = proxy;
491}
492
493
494void Value::ok() const
495{
496 const Value* op = this;
497 if (proxy == nullptr) {
498 ((Value*)op)->setProxy(static_cast<Storable*>(makeList()));
499 }
500}
#define BOTTLE_TAG_LIST
Definition Bottle.h:29
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:257
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:252
A mini-server for performing network communication in the background.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
An interface for reading from a network connection.
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
virtual bool isError() const =0
An interface for writing to a network connection.
virtual bool isError() const =0
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the 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
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition Value.cpp:228
virtual bool isString() const
Checks if value is a string.
Definition Value.cpp:156
static Value * makeList()
Create a list Value.
Definition Value.cpp:452
virtual std::int8_t asInt8() const
Get 8-bit integer value.
Definition Value.cpp:198
bool operator!=(const Value &alt) const
Inequality test.
Definition Value.cpp:358
virtual Value * clone() const
Create a copy of the value.
Definition Value.cpp:380
bool write(ConnectionWriter &connection) const override
Write this object to a network connection.
Definition Value.cpp:321
static Value * makeFloat64(yarp::conf::float64_t x)
Create a 64-bit floating point Value.
Definition Value.cpp:428
static Value * makeInt8(std::int8_t x)
Create a 8-bit integer Value.
Definition Value.cpp:403
virtual std::int64_t asInt64() const
Get 64-bit integer value.
Definition Value.cpp:216
virtual Searchable * asSearchable() const
Get dictionary or list value.
Definition Value.cpp:264
virtual bool isBool() const
Checks if value is a boolean.
Definition Value.cpp:114
virtual yarp::conf::vocab32_t asVocab32() const
Get 32 bit vocabulary identifier as an integer.
Definition Value.cpp:234
static Value * makeVocab32(yarp::conf::vocab32_t v)
Create a 32 bit vocabulary identifier Value.
Definition Value.cpp:439
static Value * makeInt64(std::int64_t x)
Create a 64-bit integer Value.
Definition Value.cpp:418
virtual bool isLeaf() const
Definition Value.cpp:398
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:192
virtual bool isVocab64() const
Checks if value is a 64bit vocabulary identifier.
Definition Value.cpp:180
virtual yarp::conf::vocab64_t asVocab64() const
Get 64 bit vocabulary identifier as an integer.
Definition Value.cpp:240
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:210
virtual size_t asBlobLength() const
Get binary data length.
Definition Value.cpp:279
static Value * makeValue(const std::string &txt)
Create a Value from a text description.
Definition Value.cpp:468
virtual bool isInt16() const
Checks if value is a 16-bit integer.
Definition Value.cpp:126
virtual bool isList() const
Checks if value is a list.
Definition Value.cpp:162
static Value & getNullValue()
Return an invalid, "null" Value.
Definition Value.cpp:478
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:252
virtual bool isInt8() const
Checks if value is a 8-bit integer.
Definition Value.cpp:120
static Value * makeInt16(std::int16_t x)
Create a 16-bit integer Value.
Definition Value.cpp:408
static Value * makeFloat32(yarp::conf::float32_t x)
Create a 32-bit floating point Value.
Definition Value.cpp:423
virtual Property * asDict() const
Get dictionary (hash table) value.
Definition Value.cpp:258
static Value * makeBlob(void *data, int length)
Create a Value containing binary data.
Definition Value.cpp:445
virtual bool isBlob() const
Checks if value is a binary object.
Definition Value.cpp:186
virtual const char * asBlob() const
Get binary data value.
Definition Value.cpp:273
~Value() override
Destructor.
Definition Value.cpp:106
virtual bool isFloat64() const
Checks if value is a 64-bit floating point number.
Definition Value.cpp:150
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition Value.cpp:345
std::string toString() const override
Return a standard text representation of the content of the object.
Definition Value.cpp:368
virtual bool isInt32() const
Checks if value is a 32-bit integer.
Definition Value.cpp:132
static Value * makeString(const std::string &str)
Create a string Value.
Definition Value.cpp:433
virtual bool isDict() const
Checks if value is a dictionary.
Definition Value.cpp:168
virtual std::int32_t getCode() const
Get standard type code of value.
Definition Value.cpp:386
virtual bool isInt64() const
Checks if value is a 64-bit integer.
Definition Value.cpp:138
static Value * makeInt32(std::int32_t x)
Create a 32-bit integer Value.
Definition Value.cpp:413
virtual Value * create() const
Create a new value of the same type.
Definition Value.cpp:374
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Value.cpp:339
bool read(ConnectionReader &connection) override
Read this object from a network connection.
Definition Value.cpp:285
virtual bool isFloat32() const
Checks if value is a 32-bit floating point number.
Definition Value.cpp:144
Value()
Construct a list Value.
Definition Value.cpp:18
bool isNull() const override
Checks if the object is invalid.
Definition Value.cpp:392
virtual bool isVocab32() const
Checks if value is a 32bit vocabulary identifier.
Definition Value.cpp:174
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition Value.cpp:333
bool operator==(const Value &alt) const
Equality test.
Definition Value.cpp:351
const Value & operator=(const Value &alt)
Assignment operator.
Definition Value.cpp:74
void fromString(const char *str)
Set value to correspond to a textual representation.
Definition Value.cpp:363
virtual yarp::conf::float32_t asFloat32() const
Get 32-bit floating point value.
Definition Value.cpp:222
virtual std::int16_t asInt16() const
Get 16-bit integer value.
Definition Value.cpp:204
virtual std::string asString() const
Get string value.
Definition Value.cpp:246
static StoreNull & getNull()
Definition BottleImpl.h:161
A single item in a Bottle.
Definition Storable.h:44
static Storable * createByCode(std::int32_t id)
Definition Storable.cpp:66
virtual bool readRaw(ConnectionReader &connection)=0
A binary blob item.
Definition Storable.h:1073
A 32-bit floating point number item.
Definition Storable.h:691
A 64-bit floating point number item.
Definition Storable.h:766
A 16-bit integer item.
Definition Storable.h:435
A 32-bit integer item.
Definition Storable.h:521
A 64-bit integer item.
Definition Storable.h:606
A 8-bit integer item.
Definition Storable.h:349
A nested list of items.
Definition Storable.h:1136
A 32 bit vocabulary item.
Definition Storable.h:841
std::int32_t vocab32_t
Definition numeric.h:78
std::int64_t vocab64_t
Definition numeric.h:79
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.