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 {
21 namespace os {
22 class Property;
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 namespace impl {
25 class Storable;
26 }
27 #endif // DOXYGEN_SHOULD_SKIP_THIS
28 } // namespace os
29 } // namespace yarp
30 
31 namespace yarp {
32 namespace os {
33 
44 class YARP_os_API Value : public Portable, public Searchable
45 {
46 public:
47  using Searchable::check;
49 
53  explicit Value();
54 
61  explicit Value(std::int32_t x, bool isVocab32 = false);
62 
67  explicit Value(yarp::conf::float64_t x);
68 
75  explicit Value(const std::string& str, bool isVocab32 = false);
76 
82  Value(void* data, int length);
83 
88  Value(const Value& alt);
89 
95  const Value& operator=(const Value& alt);
96 
100  ~Value() override;
101 
106  virtual bool isBool() const;
107 
113  YARP_DEPRECATED_MSG("Use isInt32 instead") // Since YARP 3.5.0
114  inline virtual bool isInt() const final
115  {
116  return isInt32();
117  }
118 
124  virtual bool isInt8() const;
125 
131  virtual bool isInt16() const;
132 
138  virtual bool isInt32() const;
139 
145  virtual bool isInt64() const;
146 
153  YARP_DEPRECATED_MSG("Use isFloat64 instead") // Since YARP 3.5.0
154  inline virtual bool isDouble() const final
155  {
156  return isFloat64();
157  }
158 
164  virtual bool isFloat32() const;
165 
171  virtual bool isFloat64() const;
172 
177  virtual bool isString() const;
178 
183  virtual bool isList() const;
184 
190  virtual bool isDict() const;
191 
197  virtual bool isVocab32() const;
198 
199 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
206  YARP_DEPRECATED_MSG("Use isVocab32 instead") // Since YARP 3.5.0
207  virtual bool isVocab() const
208  {
209  return isVocab32();
210  }
211 #endif // YARP_NO_DEPRECATED
212 
218  virtual bool isBlob() const;
219 
226  virtual bool asBool() const;
227 
228 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
237  YARP_DEPRECATED_MSG("Use asInt32 instead")
238  inline virtual int asInt() const final
239  {
240  return static_cast<int>(asInt32());
241  }
242 #endif // YARP_NO_DEPRECATED
243 
253  virtual std::int8_t asInt8() const;
254 
264  virtual std::int16_t asInt16() const;
265 
275  virtual std::int32_t asInt32() const;
276 
286  virtual std::int64_t asInt64() const;
287 
288 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
297  YARP_DEPRECATED_MSG("Use asFloat64 instead")
298  inline virtual double asDouble() const final
299  {
300  return static_cast<double>(asFloat64());
301  }
302 #endif // YARP_NO_DEPRECATED
303 
313  virtual yarp::conf::float32_t asFloat32() const;
314 
324  virtual yarp::conf::float64_t asFloat64() const;
325 
330  virtual yarp::conf::vocab32_t asVocab32() const;
331 
332 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
338  YARP_DEPRECATED_MSG("Use asVocab32 instead")
340  {
341  return asVocab32();
342  }
343 #endif
344 
350  virtual std::string asString() const;
351 
357  virtual Bottle* asList() const;
358 
364  virtual Property* asDict() const;
365 
371  virtual Searchable* asSearchable() const;
372 
378  virtual const char* asBlob() const;
379 
385  virtual size_t asBlobLength() const;
386 
387  // documented in Portable
388  bool read(ConnectionReader& connection) override;
389 
390  // documented in Portable
391  bool write(ConnectionWriter& connection) const override;
392 
393  // documented in Searchable
394  bool check(const std::string& key) const override;
395 
396  // documented in Searchable
397  Value& find(const std::string& key) const override;
398 
399  // documented in Searchable
400  Bottle& findGroup(const std::string& key) const override;
401 
407  bool operator==(const Value& alt) const;
408 
414  bool operator!=(const Value& alt) const;
415 
422  void fromString(const char* str);
423 
424  std::string toString() const override;
425 
430  virtual Value* create() const;
431 
436  virtual Value* clone() const;
437 
442  virtual std::int32_t getCode() const;
443 
444  bool isNull() const override;
445 
446  virtual bool isLeaf() const;
447 
448 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
456  YARP_DEPRECATED_MSG("Use makeInt32 instead")
457  inline static Value* makeInt(int x)
458  {
459  return makeInt32(static_cast<std::int32_t>(x));
460  }
461 #endif // YARP_NO_DEPRECATED
462 
468  static Value* makeInt8(std::int8_t x);
469 
475  static Value* makeInt16(std::int16_t x);
476 
482  static Value* makeInt32(std::int32_t x);
483 
489  static Value* makeInt64(std::int64_t x);
490 
491 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
499  YARP_DEPRECATED_MSG("Use makeFloat64 instead")
500  inline static Value* makeDouble(double x)
501  {
502  return makeFloat64(static_cast<yarp::conf::float64_t>(x));
503  }
504 #endif // YARP_NO_DEPRECATED
505 
511  static Value* makeFloat32(yarp::conf::float32_t x);
512 
518  static Value* makeFloat64(yarp::conf::float64_t x);
519 
525  static Value* makeString(const std::string& str);
526 
532  static Value* makeVocab32(yarp::conf::vocab32_t v);
533 
542  static Value* makeVocab32(char a, char b = 0, char c = 0, char d = 0)
543  {
544  return makeVocab32(yarp::os::createVocab32(a, b, c, d));
545  }
546 
553  static Value* makeVocab32(const std::string& str)
554  {
555  return makeVocab32(yarp::os::Vocab32::encode(str));
556  }
557 
558 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
565  YARP_DEPRECATED_MSG("Use makeVocab32 instead")
567  {
568  return makeVocab32(v);
569  }
570 
577  YARP_DEPRECATED_MSG("Use makeVocab32 instead")
578  static Value* makeVocab(const std::string& str)
579  {
580  return makeVocab32(str);
581  }
582 #endif // YARP_NO_DEPRECATED
583 
590  static Value* makeBlob(void* data, int length);
591 
596  static Value* makeList();
597 
603  static Value* makeList(const char* txt);
604 
611  static Value* makeValue(const std::string& txt);
612 
613 
618  static Value& getNullValue();
619 
620 #ifndef DOXYGEN_SHOULD_SKIP_THIS
621 private:
623 
624  void setProxy(yarp::os::impl::Storable* proxy);
625  void ok() const;
626 #endif // DOXYGEN_SHOULD_SKIP_THIS
627 };
628 
629 } // namespace os
630 } // namespace yarp
631 
632 #endif // YARP_OS_VALUE_H
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
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:26
A class for storing options and configuration information.
Definition: Property.h:34
A base class for nested structures that can be searched.
Definition: Searchable.h:66
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:45
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:553
virtual int asInt() const final
Get integer value.
Definition: Value.h:238
virtual yarp::conf::vocab32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.h:339
virtual bool isVocab() const
Checks if value is a vocabulary identifier.
Definition: Value.h:207
static Value * makeVocab32(char a, char b=0, char c=0, char d=0)
Create a vocabulary identifier Value.
Definition: Value.h:542
static Value * makeInt(int x)
Create an integer Value.
Definition: Value.h:457
static Value * makeDouble(double x)
Create a floating point Value.
Definition: Value.h:500
virtual double asDouble() const final
Get floating point value.
Definition: Value.h:298
static Value * makeVocab(yarp::conf::vocab32_t v)
Create a vocabulary identifier Value.
Definition: Value.h:566
A single item in a Bottle.
Definition: Storable.h:44
#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
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:28
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:923
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1099
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_os_API
Definition: api.h:18