YARP
Yet Another Robot Platform
Value.h
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 #ifndef YARP_OS_VALUE_H
8 #define YARP_OS_VALUE_H
9 
10 #include <yarp/conf/numeric.h>
11 
12 #include <yarp/os/Bottle.h>
13 #include <yarp/os/Portable.h>
14 #include <yarp/os/Searchable.h>
15 #include <yarp/os/Vocab.h>
16 
17 #include <cstddef> // defines size_t
18 #include <string>
19 
20 namespace yarp::os {
21 class Property;
22 } // namespace yarp::os
23 
24 #ifndef DOXYGEN_SHOULD_SKIP_THIS
25 namespace yarp::os::impl {
26 class Storable;
27 } // namespace yarp::os::impl
28 #endif // DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace yarp::os {
31 
42 class YARP_os_API Value : public Portable, public Searchable
43 {
44 public:
45  using Searchable::check;
47 
51  explicit Value();
52 
59  explicit Value(std::int32_t x, bool isVocab32 = false);
60 
65  explicit Value(yarp::conf::float64_t x);
66 
73  explicit Value(const std::string& str, bool isVocab32 = false);
74 
80  Value(void* data, int length);
81 
86  Value(const Value& alt);
87 
93  const Value& operator=(const Value& alt);
94 
98  ~Value() override;
99 
104  virtual bool isBool() const;
105 
111  YARP_DEPRECATED_MSG("Use isInt32 instead") // Since YARP 3.5.0
112  inline virtual bool isInt() const final
113  {
114  return isInt32();
115  }
116 
122  virtual bool isInt8() const;
123 
129  virtual bool isInt16() const;
130 
136  virtual bool isInt32() const;
137 
143  virtual bool isInt64() const;
144 
151  YARP_DEPRECATED_MSG("Use isFloat64 instead") // Since YARP 3.5.0
152  inline virtual bool isDouble() const final
153  {
154  return isFloat64();
155  }
156 
162  virtual bool isFloat32() const;
163 
169  virtual bool isFloat64() const;
170 
175  virtual bool isString() const;
176 
181  virtual bool isList() const;
182 
188  virtual bool isDict() const;
189 
195  virtual bool isVocab32() const;
196 
197 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
204  YARP_DEPRECATED_MSG("Use isVocab32 instead") // Since YARP 3.5.0
205  virtual bool isVocab() const
206  {
207  return isVocab32();
208  }
209 #endif // YARP_NO_DEPRECATED
210 
216  virtual bool isBlob() const;
217 
224  virtual bool asBool() const;
225 
226 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
235  YARP_DEPRECATED_MSG("Use asInt32 instead")
236  inline virtual int asInt() const final
237  {
238  return static_cast<int>(asInt32());
239  }
240 #endif // YARP_NO_DEPRECATED
241 
251  virtual std::int8_t asInt8() const;
252 
262  virtual std::int16_t asInt16() const;
263 
273  virtual std::int32_t asInt32() const;
274 
284  virtual std::int64_t asInt64() const;
285 
286 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
295  YARP_DEPRECATED_MSG("Use asFloat64 instead")
296  inline virtual double asDouble() const final
297  {
298  return static_cast<double>(asFloat64());
299  }
300 #endif // YARP_NO_DEPRECATED
301 
311  virtual yarp::conf::float32_t asFloat32() const;
312 
322  virtual yarp::conf::float64_t asFloat64() const;
323 
328  virtual yarp::conf::vocab32_t asVocab32() const;
329 
330 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
336  YARP_DEPRECATED_MSG("Use asVocab32 instead")
338  {
339  return asVocab32();
340  }
341 #endif
342 
348  virtual std::string asString() const;
349 
355  virtual Bottle* asList() const;
356 
362  virtual Property* asDict() const;
363 
369  virtual Searchable* asSearchable() const;
370 
376  virtual const char* asBlob() const;
377 
383  virtual size_t asBlobLength() const;
384 
385  // documented in Portable
386  bool read(ConnectionReader& connection) override;
387 
388  // documented in Portable
389  bool write(ConnectionWriter& connection) const override;
390 
391  // documented in Searchable
392  bool check(const std::string& key) const override;
393 
394  // documented in Searchable
395  Value& find(const std::string& key) const override;
396 
397  // documented in Searchable
398  Bottle& findGroup(const std::string& key) const override;
399 
405  bool operator==(const Value& alt) const;
406 
412  bool operator!=(const Value& alt) const;
413 
420  void fromString(const char* str);
421 
422  std::string toString() const override;
423 
428  virtual Value* create() const;
429 
434  virtual Value* clone() const;
435 
440  virtual std::int32_t getCode() const;
441 
442  bool isNull() const override;
443 
444  virtual bool isLeaf() const;
445 
446 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
454  YARP_DEPRECATED_MSG("Use makeInt32 instead")
455  inline static Value* makeInt(int x)
456  {
457  return makeInt32(static_cast<std::int32_t>(x));
458  }
459 #endif // YARP_NO_DEPRECATED
460 
466  static Value* makeInt8(std::int8_t x);
467 
473  static Value* makeInt16(std::int16_t x);
474 
480  static Value* makeInt32(std::int32_t x);
481 
487  static Value* makeInt64(std::int64_t x);
488 
489 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
497  YARP_DEPRECATED_MSG("Use makeFloat64 instead")
498  inline static Value* makeDouble(double x)
499  {
500  return makeFloat64(static_cast<yarp::conf::float64_t>(x));
501  }
502 #endif // YARP_NO_DEPRECATED
503 
509  static Value* makeFloat32(yarp::conf::float32_t x);
510 
516  static Value* makeFloat64(yarp::conf::float64_t x);
517 
523  static Value* makeString(const std::string& str);
524 
530  static Value* makeVocab32(yarp::conf::vocab32_t v);
531 
540  static Value* makeVocab32(char a, char b = 0, char c = 0, char d = 0)
541  {
542  return makeVocab32(yarp::os::createVocab32(a, b, c, d));
543  }
544 
551  static Value* makeVocab32(const std::string& str)
552  {
553  return makeVocab32(yarp::os::Vocab32::encode(str));
554  }
555 
556 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
563  YARP_DEPRECATED_MSG("Use makeVocab32 instead")
565  {
566  return makeVocab32(v);
567  }
568 
575  YARP_DEPRECATED_MSG("Use makeVocab32 instead")
576  static Value* makeVocab(const std::string& str)
577  {
578  return makeVocab32(str);
579  }
580 #endif // YARP_NO_DEPRECATED
581 
588  static Value* makeBlob(void* data, int length);
589 
594  static Value* makeList();
595 
601  static Value* makeList(const char* txt);
602 
609  static Value* makeValue(const std::string& txt);
610 
611 
616  static Value& getNullValue();
617 
618 #ifndef DOXYGEN_SHOULD_SKIP_THIS
619 private:
621 
622  void setProxy(yarp::os::impl::Storable* proxy);
623  void ok() const;
624 #endif // DOXYGEN_SHOULD_SKIP_THIS
625 };
626 
627 } // namespace yarp::os
628 
629 #endif // YARP_OS_VALUE_H
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
An interface for reading from a network connection.
An interface for writing 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:63
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
A single value (typically within a Bottle).
Definition: Value.h:43
static Value * makeVocab32(const std::string &str)
Create a vocabulary identifier Value If the string is longer than 4 characters, only the first 4 are ...
Definition: Value.h:551
virtual int asInt() const final
Get integer value.
Definition: Value.h:236
virtual yarp::conf::vocab32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.h:337
virtual bool isVocab() const
Checks if value is a vocabulary identifier.
Definition: Value.h:205
static Value * makeVocab32(char a, char b=0, char c=0, char d=0)
Create a vocabulary identifier Value.
Definition: Value.h:540
static Value * makeInt(int x)
Create an integer Value.
Definition: Value.h:455
static Value * makeDouble(double x)
Create a floating point Value.
Definition: Value.h:498
virtual double asDouble() const final
Get floating point value.
Definition: Value.h:296
static Value * makeVocab(yarp::conf::vocab32_t v)
Create a vocabulary identifier Value.
Definition: Value.h:564
A single item in a Bottle.
Definition: Storable.h:42
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
std::string toString(const T &value)
convert an arbitrary type to string.
std::int32_t vocab32_t
Definition: numeric.h:78
double float64_t
Definition: numeric.h:77
float float32_t
Definition: numeric.h:76
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:11
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition: Vocab.h:27
std::string findGroup(const robotinterface::ParamList &list, const std::string &name)
Definition: Types.cpp:47
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:915
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1091
#define YARP_os_API
Definition: api.h:18