YARP
Yet Another Robot Platform
Bottle.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-FileCopyrightText: 2006, 2008 Arjan Gijsberts
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef YARP_OS_BOTTLE_H
9 #define YARP_OS_BOTTLE_H
10 
11 #include <yarp/os/Portable.h>
12 #include <yarp/os/Property.h>
13 #include <yarp/os/Searchable.h>
14 #include <yarp/os/Value.h>
15 #include <yarp/os/Vocab.h>
16 
17 #include <string>
18 
19 #define BOTTLE_TAG_INT8 32 // 0000 0000 0010 0000
20 #define BOTTLE_TAG_INT16 64 // 0000 0000 0100 0000
21 #define BOTTLE_TAG_INT32 1 // 0000 0000 0000 0001
22 #define BOTTLE_TAG_INT64 (1 + 16) // 0000 0000 0001 0001
23 #define BOTTLE_TAG_VOCAB32 (1 + 8) // 0000 0000 0000 1001
24 #define BOTTLE_TAG_FLOAT32 128 // 0000 0000 1000 0000
25 #define BOTTLE_TAG_FLOAT64 (2 + 8) // 0000 0000 0000 1010
26 #define BOTTLE_TAG_STRING (4) // 0000 0000 0000 0100
27 #define BOTTLE_TAG_BLOB (4 + 8) // 0000 0000 0000 1100
28 #define BOTTLE_TAG_LIST 256 // 0000 0001 0000 0000
29 #define BOTTLE_TAG_DICT 512 // 0000 0010 0000 0000
30 
31 YARP_DEPRECATED_MSG("Use BOTTLE_TAG_INT32 instead") // Since YARP 3.5.0
32 constexpr std::int32_t BOTTLE_TAG_DOUBLE = BOTTLE_TAG_FLOAT64;
33 
34 YARP_DEPRECATED_MSG("Use BOTTLE_TAG_FLOAT64 instead") // Since YARP 3.5.0
35 constexpr std::int32_t BOTTLE_TAG_INT = BOTTLE_TAG_INT32;
36 
37 YARP_DEPRECATED_MSG("Use BOTTLE_TAG_VOCAB32 instead") // Since YARP 3.5.0
38 constexpr std::int32_t BOTTLE_TAG_VOCAB = BOTTLE_TAG_VOCAB32;
39 
40 namespace yarp::os {
41 
42 class NetworkBase;
43 
44 #ifndef DOXYGEN_SHOULD_SKIP_THIS
45 namespace impl {
46 class BottleImpl;
47 class Storable;
48 class StoreList;
49 } // namespace impl
50 #endif // DOXYGEN_SHOULD_SKIP_THIS
51 
52 
72 class YARP_os_API Bottle : public Portable, public Searchable
73 {
74 public:
75 #if defined(SWIG) && (SWIG_VERSION < 0x300011)
76  typedef size_t size_type;
77 #else
78  using size_type = size_t;
79 #endif
80  // FIXME this can be constexpr, but swig 3.0.8 is not happy
81  static const size_type npos;
82 
83  using Searchable::check;
85 
89  Bottle();
90 
101  explicit Bottle(const std::string& text);
102 
108  Bottle(const Bottle& rhs);
109 
115  Bottle(Bottle&& rhs) noexcept;
116 
121  Bottle(std::initializer_list<yarp::os::Value> values);
122 
129  Bottle& operator=(const Bottle& rhs);
130 
137  Bottle& operator=(Bottle&& rhs) noexcept;
138 
142  ~Bottle() override;
143 
147  void clear();
148 
149 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5.0
157  YARP_DEPRECATED_MSG("Use addInt32 instead")
158  inline void addInt(int x)
159  {
160  addInt32(static_cast<std::int32_t>(x));
161  }
162 #endif // YARP_NO_DEPRECATED
163 
169  void addInt8(std::int8_t x);
170 
176  void addInt16(std::int16_t x);
177 
183  void addInt32(std::int32_t x);
184 
190  void addInt64(std::int64_t x);
191 
192 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5.0
199  YARP_DEPRECATED_MSG("Use addVocab32 instead")
201  {
202  return addVocab32(x);
203  }
204 #endif // YARP_NO_DEPRECATED
205 
211  void addVocab32(yarp::conf::vocab32_t x);
212 
221  void addVocab32(char a, char b = 0, char c = 0, char d = 0)
222  {
223  addVocab32(yarp::os::createVocab32(a, b, c, d));
224  }
225 
231  void addVocab32(const std::string& str)
232  {
233  addVocab32(yarp::os::Vocab32::encode(str));
234  }
235 
236 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5.0
245  YARP_DEPRECATED_MSG("Use addFloat64 instead")
246  inline void addDouble(double x)
247  {
248  addFloat64(static_cast<yarp::conf::float64_t>(x));
249  }
250 #endif // YARP_NO_DEPRECATED
251 
258  void addFloat32(yarp::conf::float32_t x);
259 
266  void addFloat64(yarp::conf::float64_t x);
267 
273  void addString(const char* str);
274 
280  void addString(const std::string& str);
281 
287  void add(const Value& value);
288 
298  void add(Value* value);
299 
308  Bottle& addList();
309 
316  Property& addDict();
317 
324  Value pop();
325 
338  Value& get(size_type index) const;
339 
345  size_type size() const;
346 
357  void fromString(const std::string& text);
358 
365  void fromBinary(const char* buf, size_t len);
366 
377  const char* toBinary(size_t* size = nullptr);
378 
387  std::string toString() const override;
388 
395  bool write(ConnectionWriter& writer) const override;
396 
403  bool read(ConnectionReader& reader) override;
404 
415  bool write(PortReader& reader, bool textMode = false);
416 
426  bool read(const PortWriter& writer, bool textMode = false);
427 
428  void onCommencement() const override;
429 
430  bool check(const std::string& key) const override;
431 
432  Value& find(const std::string& key) const override;
433 
434  Bottle& findGroup(const std::string& key) const override;
435 
436  bool isNull() const override;
437 
445  void copy(const Bottle& alt, size_type first = 0, size_type len = npos);
446 
452  static Bottle& getNullBottle();
453 
460  bool operator==(const Bottle& alt) const;
461 
468  bool operator!=(const Bottle& alt) const;
469 
476  void append(const Bottle& alt);
477 
484  Bottle tail() const;
485 
493  void hasChanged();
494 
495  static std::string toString(int x);
496 
502  int getSpecialization();
503 
511  static std::string describeBottleCode(int code);
512 
513 
514 protected:
515  void setReadOnly(bool readOnly);
516 
517 
518 private:
519  friend class yarp::os::NetworkBase;
520 #ifndef DOXYGEN_SHOULD_SKIP_THIS
521  friend class yarp::os::impl::Storable;
522  friend class yarp::os::impl::StoreList;
523  friend class yarp::os::impl::BottleImpl;
525 #endif // DOXYGEN_SHOULD_SKIP_THIS
526 };
527 
528 } // namespace yarp::os
529 
530 #endif // YARP_OS_BOTTLE_H
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:25
#define BOTTLE_TAG_INT32
Definition: Bottle.h:21
constexpr std::int32_t BOTTLE_TAG_INT
Definition: Bottle.h:35
constexpr std::int32_t BOTTLE_TAG_DOUBLE
Definition: Bottle.h:32
constexpr std::int32_t BOTTLE_TAG_VOCAB
Definition: Bottle.h:38
#define BOTTLE_TAG_VOCAB32
Definition: Bottle.h:23
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:17
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
void addVocab32(const std::string &str)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.h:231
void addDouble(double x)
Places a floating point number in the bottle, at the end of the list.
Definition: Bottle.h:246
size_t size_type
Definition: Bottle.h:78
void addInt(int x)
Places an integer in the bottle, at the end of the list.
Definition: Bottle.h:158
void addVocab32(char a, char b=0, char c=0, char d=0)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.h:221
static const size_type npos
Definition: Bottle.h:81
void addVocab(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.h:200
Bottle(std::initializer_list< yarp::os::Value > values)
Initializer list constructor.
An interface for reading from a network connection.
An interface for writing to a network connection.
Utilities for manipulating the YARP network, excluding initialization and shutdown.
Definition: Network.h:38
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:23
virtual void onCommencement() const
This is called when the port is about to begin writing operations.
Definition: PortWriter.cpp:17
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:25
bool read(ConnectionReader &reader) override=0
Read this object from a network connection.
bool write(ConnectionWriter &writer) const override=0
Write this object to a network connection.
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 isNull() const
Checks if the object is invalid.
Definition: Searchable.cpp:107
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
Searchable & operator=(const Searchable &rhs)=default
Copy assignment operator.
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
A flexible data format for holding a bunch of numbers and strings.
Definition: BottleImpl.h:34
A single item in a Bottle.
Definition: Storable.h:42
A nested list of items.
Definition: Storable.h:1038
#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:27
std::string findGroup(const robotinterface::ParamList &list, const std::string &name)
Definition: Types.cpp:47
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_os_API
Definition: api.h:18