YARP
Yet Another Robot Platform
Bottle.h
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  * Copyright (C) 2006, 2008 Arjan Gijsberts
5  * All rights reserved.
6  *
7  * This software may be modified and distributed under the terms of the
8  * BSD-3-Clause license. See the accompanying LICENSE file for details.
9  */
10 
11 #ifndef YARP_OS_BOTTLE_H
12 #define YARP_OS_BOTTLE_H
13 
14 #include <yarp/os/Portable.h>
15 #include <yarp/os/Property.h>
16 #include <yarp/os/Searchable.h>
17 #include <yarp/os/Value.h>
18 
19 #include <string>
20 
21 #define BOTTLE_TAG_INT8 32 // 0000 0000 0010 0000
22 #define BOTTLE_TAG_INT16 64 // 0000 0000 0100 0000
23 #define BOTTLE_TAG_INT32 1 // 0000 0000 0000 0001
24 #define BOTTLE_TAG_INT64 (1 + 16) // 0000 0000 0001 0001
25 #define BOTTLE_TAG_VOCAB (1 + 8) // 0000 0000 0000 1001
26 #define BOTTLE_TAG_FLOAT32 128 // 0000 0000 1000 0000
27 #define BOTTLE_TAG_FLOAT64 (2 + 8) // 0000 0000 0000 1010
28 #define BOTTLE_TAG_STRING (4) // 0000 0000 0000 0100
29 #define BOTTLE_TAG_BLOB (4 + 8) // 0000 0000 0000 1100
30 #define BOTTLE_TAG_LIST 256 // 0000 0001 0000 0000
31 #define BOTTLE_TAG_DICT 512 // 0000 0010 0000 0000
32 
33 YARP_DEPRECATED_INTERNAL_MSG("Use BOTTLE_TAG_INT32 instead") // Since YARP 3.0.0
34 constexpr std::int32_t BOTTLE_TAG_DOUBLE = BOTTLE_TAG_FLOAT64;
35 
36 YARP_DEPRECATED_INTERNAL_MSG("Use BOTTLE_TAG_FLOAT64 instead") // Since YARP 3.0.0
37 constexpr std::int32_t BOTTLE_TAG_INT = BOTTLE_TAG_INT32;
38 
39 namespace yarp {
40 namespace 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;
84  using Searchable::findGroup;
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  virtual ~Bottle();
143 
147  void clear();
148 
155  YARP_DEPRECATED_INTERNAL_MSG("Use addInt32 instead") // Since YARP 3.0.0
156  inline void addInt(int x)
157  {
158  addInt32(static_cast<std::int32_t>(x));
159  }
160 
166  void addInt8(std::int8_t x);
167 
173  void addInt16(std::int16_t x);
174 
180  void addInt32(std::int32_t x);
181 
187  void addInt64(std::int64_t x);
188 
194  void addVocab(int x);
195 
203  YARP_DEPRECATED_INTERNAL_MSG("Use addFloat64 instead") // Since YARP 3.0.0
204  inline void addDouble(double x)
205  {
206  addFloat64(static_cast<yarp::conf::float64_t>(x));
207  }
208 
215  void addFloat32(yarp::conf::float32_t x);
216 
223  void addFloat64(yarp::conf::float64_t x);
224 
230  void addString(const char* str);
231 
237  void addString(const std::string& str);
238 
244  void add(const Value& value);
245 
255  void add(Value* value);
256 
265  Bottle& addList();
266 
273  Property& addDict();
274 
281  Value pop();
282 
295  Value& get(size_type index) const;
296 
302  size_type size() const;
303 
314  void fromString(const std::string& text);
315 
322  void fromBinary(const char* buf, size_t len);
323 
334  const char* toBinary(size_t* size = nullptr);
335 
344  std::string toString() const override;
345 
352  bool write(ConnectionWriter& writer) const override;
353 
360  bool read(ConnectionReader& reader) override;
361 
372  bool write(PortReader& reader, bool textMode = false);
373 
383  bool read(const PortWriter& writer, bool textMode = false);
384 
385  void onCommencement() const override;
386 
387  bool check(const std::string& key) const override;
388 
389  Value& find(const std::string& key) const override;
390 
391  Bottle& findGroup(const std::string& key) const override;
392 
393  bool isNull() const override;
394 
402  void copy(const Bottle& alt, size_type first = 0, size_type len = npos);
403 
409  static Bottle& getNullBottle();
410 
417  bool operator==(const Bottle& alt) const;
418 
425  bool operator!=(const Bottle& alt) const;
426 
433  void append(const Bottle& alt);
434 
441  Bottle tail() const;
442 
450  void hasChanged();
451 
452  static std::string toString(int x);
453 
459  int getSpecialization();
460 
468  static std::string describeBottleCode(int code);
469 
470 
471 protected:
472  void setReadOnly(bool readOnly);
473 
474 
475 private:
476  friend class yarp::os::NetworkBase;
477 #ifndef DOXYGEN_SHOULD_SKIP_THIS
478  friend class yarp::os::impl::Storable;
479  friend class yarp::os::impl::StoreList;
480  friend class yarp::os::impl::BottleImpl;
482 #endif // DOXYGEN_SHOULD_SKIP_THIS
483 };
484 
485 } // namespace os
486 } // namespace yarp
487 
488 #endif // YARP_OS_BOTTLE_H
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Bottle::size_type
size_t size_type
Definition: Bottle.h:78
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::Bottle::npos
static const size_type npos
Definition: Bottle.h:81
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:827
BOTTLE_TAG_INT
constexpr std::int32_t BOTTLE_TAG_INT
Definition: Bottle.h:37
yarp::os::impl::BottleImpl
A flexible data format for holding a bunch of numbers and strings.
Definition: BottleImpl.h:36
Portable.h
YARP_os_API
#define YARP_os_API
Definition: api.h:19
yarp::os::impl::Storable
A single item in a Bottle.
Definition: Storable.h:47
Searchable.h
yarp::os::PortWriter
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
BOTTLE_TAG_INT32
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
Property.h
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::os::impl::StoreList
A nested list of items.
Definition: Storable.h:1043
yarp::os::Bottle::Bottle
Bottle(std::initializer_list< yarp::os::Value > values)
Initializer list constructor.
yarp::conf::float32_t
float float32_t
Definition: numeric.h:50
YARP_DEPRECATED_INTERNAL_MSG
#define YARP_DEPRECATED_INTERNAL_MSG(X)
Definition: api.h:139
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
BOTTLE_TAG_FLOAT64
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
toString
std::string toString(const T &value)
convert an arbitrary type to string.
Definition: fakeMotionControl.cpp:121
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::conf::float64_t
double float64_t
Definition: numeric.h:51
implementation
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:971
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::os::NetworkBase
Utilities for manipulating the YARP network, excluding initialization and shutdown.
Definition: Network.h:44
Value.h
BOTTLE_TAG_DOUBLE
constexpr std::int32_t BOTTLE_TAG_DOUBLE
Definition: Bottle.h:34
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37