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
169{
170 ok();
171 return proxy->isVocab32();
172}
173
175{
176 ok();
177 return proxy->isVocab64();
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
235{
236 ok();
237 return proxy->asVocab64();
238}
239
240std::string Value::asString() const
241{
242 ok();
243 return proxy->asString();
244}
245
247{
248 ok();
249 return proxy->asList();
250}
251
252const char* Value::asBlob() const
253{
254 ok();
255 return proxy->asBlob();
256}
257
259{
260 ok();
261 return proxy->asBlobLength();
262}
263
265{
266 if (proxy != nullptr) {
267 delete proxy;
268 proxy = nullptr;
269 }
270 std::int32_t x = connection.expectInt32();
271 if ((x & 0xffff) != x) {
272 return false;
273 }
274 if ((x & BOTTLE_TAG_LIST) == 0) {
275 return false;
276 }
277 std::int32_t len = connection.expectInt32();
278 if (len == 0) {
279 return true;
280 }
281 if (len != 1) {
282 return false;
283 }
284 if (x == BOTTLE_TAG_LIST) {
285 x = connection.expectInt32();
286 } else {
287 x &= ~BOTTLE_TAG_LIST;
288 }
289 if (connection.isError()) {
290 return false;
291 }
293 setProxy(s);
294 if (proxy == nullptr) {
295 return false;
296 }
297 return s->readRaw(connection);
298}
299
300bool Value::write(ConnectionWriter& connection) const
301{
302 if (proxy == nullptr) {
303 connection.appendInt32(BOTTLE_TAG_LIST);
304 connection.appendInt32(0);
305 return !connection.isError();
306 }
307 connection.appendInt32(BOTTLE_TAG_LIST);
308 connection.appendInt32(1);
309 return proxy->write(connection);
310}
311
312bool Value::check(const std::string& key) const
313{
314 ok();
315 return proxy->check(key);
316}
317
318Value& Value::find(const std::string& key) const
319{
320 ok();
321 return proxy->find(key);
322}
323
324Bottle& Value::findGroup(const std::string& key) const
325{
326 ok();
327 return proxy->findGroup(key);
328}
329
330bool Value::operator==(const Value& alt) const
331{
332 ok();
333 return (*proxy) == alt;
334}
335
336
337bool Value::operator!=(const Value& alt) const
338{
339 return !((*this) == alt);
340}
341
342void Value::fromString(const char* str)
343{
344 setProxy(static_cast<Storable*>(makeValue(str)));
345}
346
347std::string Value::toString() const
348{
349 ok();
350 return proxy->toString();
351}
352
354{
355 ok();
356 return proxy->create();
357}
358
360{
361 ok();
362 return proxy->clone();
363}
364
365int Value::getCode() const
366{
367 ok();
368 return proxy->getCode();
369}
370
371bool Value::isNull() const
372{
373 ok();
374 return proxy->isNull();
375}
376
377bool Value::isLeaf() const
378{
379 return false;
380}
381
382Value* Value::makeInt8(std::int8_t x)
383{
384 return new StoreInt8(x);
385}
386
387Value* Value::makeInt16(std::int16_t x)
388{
389 return new StoreInt16(x);
390}
391
392Value* Value::makeInt32(std::int32_t x)
393{
394 return new StoreInt32(x);
395}
396
397Value* Value::makeInt64(std::int64_t x)
398{
399 return new StoreInt64(x);
400}
401
406
411
412Value* Value::makeString(const std::string& str)
413{
414 return new StoreString(str);
415}
416
417
422
423
424Value* Value::makeBlob(void* data, int length)
425{
426 std::string s((char*)data, length);
427 return new StoreBlob(s);
428}
429
430
432{
433 return new StoreList();
434}
435
436
437Value* Value::makeList(const char* txt)
438{
439 Value* v = makeList();
440 if (v != nullptr) {
441 v->asList()->fromString(txt);
442 }
443 return v;
444}
445
446
447Value* Value::makeValue(const std::string& txt)
448{
449 Bottle bot(txt);
450 if (bot.size() > 1) {
451 return makeString(txt);
452 }
453 return bot.get(0).clone();
454}
455
456
461
462
463void Value::setProxy(Storable* proxy)
464{
465 if (this->proxy != nullptr) {
466 delete this->proxy;
467 this->proxy = nullptr;
468 }
469 this->proxy = proxy;
470}
471
472
473void Value::ok() const
474{
475 const Value* op = this;
476 if (proxy == nullptr) {
477 ((Value*)op)->setProxy(static_cast<Storable*>(makeList()));
478 }
479}
#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:64
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:252
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:247
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 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: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:431
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:337
virtual Value * clone() const
Create a copy of the value.
Definition Value.cpp:359
bool write(ConnectionWriter &connection) const override
Write this object to a network connection.
Definition Value.cpp:300
static Value * makeFloat64(yarp::conf::float64_t x)
Create a 64-bit floating point Value.
Definition Value.cpp:407
static Value * makeInt8(std::int8_t x)
Create a 8-bit integer Value.
Definition Value.cpp:382
virtual std::int64_t asInt64() const
Get 64-bit integer value.
Definition Value.cpp:210
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:228
static Value * makeVocab32(yarp::conf::vocab32_t v)
Create a 32 bit vocabulary identifier Value.
Definition Value.cpp:418
static Value * makeInt64(std::int64_t x)
Create a 64-bit integer Value.
Definition Value.cpp:397
virtual bool isLeaf() const
Definition Value.cpp:377
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186
virtual bool isVocab64() const
Checks if value is a 64bit vocabulary identifier.
Definition Value.cpp:174
virtual yarp::conf::vocab64_t asVocab64() const
Get 64 bit vocabulary identifier as an integer.
Definition Value.cpp:234
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:258
static Value * makeValue(const std::string &txt)
Create a Value from a text description.
Definition Value.cpp:447
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:457
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:246
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:387
static Value * makeFloat32(yarp::conf::float32_t x)
Create a 32-bit floating point Value.
Definition Value.cpp:402
static Value * makeBlob(void *data, int length)
Create a Value containing binary data.
Definition Value.cpp:424
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:252
~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:324
std::string toString() const override
Return a standard text representation of the content of the object.
Definition Value.cpp:347
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:412
virtual std::int32_t getCode() const
Get standard type code of value.
Definition Value.cpp:365
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:392
virtual Value * create() const
Create a new value of the same type.
Definition Value.cpp:353
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Value.cpp:318
bool read(ConnectionReader &connection) override
Read this object from a network connection.
Definition Value.cpp:264
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:371
virtual bool isVocab32() const
Checks if value is a 32bit vocabulary identifier.
Definition Value.cpp:168
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition Value.cpp:312
bool operator==(const Value &alt) const
Equality test.
Definition Value.cpp:330
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:342
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:240
static StoreNull & getNull()
Definition BottleImpl.h:159
A single item in a Bottle.
Definition Storable.h:43
static Storable * createByCode(std::int32_t id)
Definition Storable.cpp:64
virtual bool readRaw(ConnectionReader &connection)=0
A binary blob item.
Definition Storable.h:1054
A 32-bit floating point number item.
Definition Storable.h:672
A 64-bit floating point number item.
Definition Storable.h:747
A 16-bit integer item.
Definition Storable.h:416
A 32-bit integer item.
Definition Storable.h:502
A 64-bit integer item.
Definition Storable.h:587
A 8-bit integer item.
Definition Storable.h:330
A nested list of items.
Definition Storable.h:1117
A 32 bit vocabulary item.
Definition Storable.h:822
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.