YARP
Yet Another Robot Platform
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
18Value::Value() :
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
65Value::Value(const Value& alt) :
66 Portable(),
67 Searchable(alt),
68 proxy(nullptr)
69{
70 setProxy(static_cast<Storable*>(alt.clone()));
71}
72
73
74const Value& Value::operator=(const Value& alt)
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
180bool Value::isBlob() const
181{
182 ok();
183 return proxy->isBlob();
184}
185
186bool Value::asBool() const
187{
188 ok();
189 return proxy->asBool();
190}
191
192std::int8_t Value::asInt8() const
193{
194 ok();
195 return proxy->asInt8();
196}
197
198std::int16_t Value::asInt16() const
199{
200 ok();
201 return proxy->asInt16();
202}
203
204std::int32_t Value::asInt32() const
205{
206 ok();
207 return proxy->asInt32();
208}
209
210std::int64_t Value::asInt64() const
211{
212 ok();
213 return proxy->asInt64();
214}
215
217{
218 ok();
219 return proxy->asFloat32();
220}
221
223{
224 ok();
225 return proxy->asFloat64();
226}
227
229{
230 ok();
231 return proxy->asVocab32();
232}
233
234std::string Value::asString() const
235{
236 ok();
237 return proxy->asString();
238}
239
241{
242 ok();
243 return proxy->asList();
244}
245
247{
248 ok();
249 return proxy->asDict();
250}
251
253{
254 ok();
255 if (proxy->isDict()) {
256 return proxy->asDict();
257 }
258 return proxy->asList();
259}
260
261const char* Value::asBlob() const
262{
263 ok();
264 return proxy->asBlob();
265}
266
268{
269 ok();
270 return proxy->asBlobLength();
271}
272
274{
275 if (proxy != nullptr) {
276 delete proxy;
277 proxy = nullptr;
278 }
279 std::int32_t x = connection.expectInt32();
280 if ((x & 0xffff) != x) {
281 return false;
282 }
283 if ((x & BOTTLE_TAG_LIST) == 0) {
284 return false;
285 }
286 std::int32_t len = connection.expectInt32();
287 if (len == 0) {
288 return true;
289 }
290 if (len != 1) {
291 return false;
292 }
293 if (x == BOTTLE_TAG_LIST) {
294 x = connection.expectInt32();
295 } else {
296 x &= ~BOTTLE_TAG_LIST;
297 }
298 if (connection.isError()) {
299 return false;
300 }
301 Storable* s = Storable::createByCode(x);
302 setProxy(s);
303 if (proxy == nullptr) {
304 return false;
305 }
306 return s->readRaw(connection);
307}
308
309bool Value::write(ConnectionWriter& connection) const
310{
311 if (proxy == nullptr) {
312 connection.appendInt32(BOTTLE_TAG_LIST);
313 connection.appendInt32(0);
314 return !connection.isError();
315 }
316 connection.appendInt32(BOTTLE_TAG_LIST);
317 connection.appendInt32(1);
318 return proxy->write(connection);
319}
320
321bool Value::check(const std::string& key) const
322{
323 ok();
324 return proxy->check(key);
325}
326
327Value& Value::find(const std::string& key) const
328{
329 ok();
330 return proxy->find(key);
331}
332
333Bottle& Value::findGroup(const std::string& key) const
334{
335 ok();
336 return proxy->findGroup(key);
337}
338
339bool Value::operator==(const Value& alt) const
340{
341 ok();
342 return (*proxy) == alt;
343}
344
345
346bool Value::operator!=(const Value& alt) const
347{
348 return !((*this) == alt);
349}
350
351void Value::fromString(const char* str)
352{
353 setProxy(static_cast<Storable*>(makeValue(str)));
354}
355
356std::string Value::toString() const
357{
358 ok();
359 return proxy->toString();
360}
361
363{
364 ok();
365 return proxy->create();
366}
367
369{
370 ok();
371 return proxy->clone();
372}
373
374int Value::getCode() const
375{
376 ok();
377 return proxy->getCode();
378}
379
380bool Value::isNull() const
381{
382 ok();
383 return proxy->isNull();
384}
385
386bool Value::isLeaf() const
387{
388 return false;
389}
390
391Value* Value::makeInt8(std::int8_t x)
392{
393 return new StoreInt8(x);
394}
395
396Value* Value::makeInt16(std::int16_t x)
397{
398 return new StoreInt16(x);
399}
400
401Value* Value::makeInt32(std::int32_t x)
402{
403 return new StoreInt32(x);
404}
405
406Value* Value::makeInt64(std::int64_t x)
407{
408 return new StoreInt64(x);
409}
410
412{
413 return new StoreFloat32(x);
414}
415
417{
418 return new StoreFloat64(x);
419}
420
421Value* Value::makeString(const std::string& str)
422{
423 return new StoreString(str);
424}
425
426
428{
429 return new StoreVocab32(v);
430}
431
432
433Value* Value::makeBlob(void* data, int length)
434{
435 std::string s((char*)data, length);
436 return new StoreBlob(s);
437}
438
439
441{
442 return new StoreList();
443}
444
445
446Value* Value::makeList(const char* txt)
447{
448 Value* v = makeList();
449 if (v != nullptr) {
450 v->asList()->fromString(txt);
451 }
452 return v;
453}
454
455
456Value* Value::makeValue(const std::string& txt)
457{
458 Bottle bot(txt);
459 if (bot.size() > 1) {
460 return makeString(txt);
461 }
462 return bot.get(0).clone();
463}
464
465
467{
468 return BottleImpl::getNull();
469}
470
471
472void Value::setProxy(Storable* proxy)
473{
474 if (this->proxy != nullptr) {
475 delete this->proxy;
476 this->proxy = nullptr;
477 }
478 this->proxy = proxy;
479}
480
481
482void Value::ok() const
483{
484 const Value* op = this;
485 if (proxy == nullptr) {
486 ((Value*)op)->setProxy(static_cast<Storable*>(makeList()));
487 }
488}
#define BOTTLE_TAG_LIST
Definition: Bottle.h:28
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:64
void fromString(const std::string &text)
Initializes bottle from a string.
Definition: Bottle.cpp:204
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
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:56
A single value (typically within a Bottle).
Definition: Value.h:43
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:156
static Value * makeList()
Create a list Value.
Definition: Value.cpp:440
virtual std::int8_t asInt8() const
Get 8-bit integer value.
Definition: Value.cpp:192
bool operator!=(const Value &alt) const
Inequality test.
Definition: Value.cpp:346
virtual Value * clone() const
Create a copy of the value.
Definition: Value.cpp:368
bool write(ConnectionWriter &connection) const override
Write this object to a network connection.
Definition: Value.cpp:309
static Value * makeFloat64(yarp::conf::float64_t x)
Create a 64-bit floating point Value.
Definition: Value.cpp:416
static Value * makeInt8(std::int8_t x)
Create a 8-bit integer Value.
Definition: Value.cpp:391
virtual std::int64_t asInt64() const
Get 64-bit integer value.
Definition: Value.cpp:210
virtual Searchable * asSearchable() const
Get dictionary or list value.
Definition: Value.cpp:252
virtual bool isBool() const
Checks if value is a boolean.
Definition: Value.cpp:114
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
static Value * makeVocab32(yarp::conf::vocab32_t v)
Create a vocabulary identifier Value.
Definition: Value.cpp:427
static Value * makeInt64(std::int64_t x)
Create a 64-bit integer Value.
Definition: Value.cpp:406
virtual bool isLeaf() const
Definition: Value.cpp:386
virtual bool asBool() const
Get boolean value.
Definition: Value.cpp:186
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual size_t asBlobLength() const
Get binary data length.
Definition: Value.cpp:267
static Value * makeValue(const std::string &txt)
Create a Value from a text description.
Definition: Value.cpp:456
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:466
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:240
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:396
static Value * makeFloat32(yarp::conf::float32_t x)
Create a 32-bit floating point Value.
Definition: Value.cpp:411
virtual Property * asDict() const
Get dictionary (hash table) value.
Definition: Value.cpp:246
static Value * makeBlob(void *data, int length)
Create a Value containing binary data.
Definition: Value.cpp:433
virtual bool isBlob() const
Checks if value is a binary object.
Definition: Value.cpp:180
virtual const char * asBlob() const
Get binary data value.
Definition: Value.cpp:261
~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:333
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:356
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:421
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:374
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:401
virtual Value * create() const
Create a new value of the same type.
Definition: Value.cpp:362
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Value.cpp:327
bool read(ConnectionReader &connection) override
Read this object from a network connection.
Definition: Value.cpp:273
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:380
virtual bool isVocab32() const
Checks if value is a 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:321
bool operator==(const Value &alt) const
Equality test.
Definition: Value.cpp:339
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:351
virtual yarp::conf::float32_t asFloat32() const
Get 32-bit floating point value.
Definition: Value.cpp:216
virtual std::int16_t asInt16() const
Get 16-bit integer value.
Definition: Value.cpp:198
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
A single item in a Bottle.
Definition: Storable.h:42
virtual bool readRaw(ConnectionReader &connection)=0
A binary blob item.
Definition: Storable.h:975
A 32-bit floating point number item.
Definition: Storable.h:679
A 64-bit floating point number item.
Definition: Storable.h:754
A 16-bit integer item.
Definition: Storable.h:423
A 32-bit integer item.
Definition: Storable.h:509
A 64-bit integer item.
Definition: Storable.h:594
A 8-bit integer item.
Definition: Storable.h:337
A nested list of items.
Definition: Storable.h:1038
A vocabulary item.
Definition: Storable.h:829
std::int32_t vocab32_t
Definition: numeric.h:78
double float64_t
Definition: numeric.h:77
float float32_t
Definition: numeric.h:76
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.