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