YARP
Yet Another Robot Platform
Bottle.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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_VOCAB32 (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 YARP_DEPRECATED_INTERNAL_MSG("Use BOTTLE_TAG_VOCAB32 instead") // Since YARP 3.5.0
40 constexpr std::int32_t BOTTLE_TAG_VOCAB = BOTTLE_TAG_VOCAB32;
41 
42 namespace yarp {
43 namespace os {
44 
45 class NetworkBase;
46 
47 #ifndef DOXYGEN_SHOULD_SKIP_THIS
48 namespace impl {
49 class BottleImpl;
50 class Storable;
51 class StoreList;
52 } // namespace impl
53 #endif // DOXYGEN_SHOULD_SKIP_THIS
54 
55 
75 class YARP_os_API Bottle : public Portable, public Searchable
76 {
77 public:
78 #if defined(SWIG) && (SWIG_VERSION < 0x300011)
79  typedef size_t size_type;
80 #else
81  using size_type = size_t;
82 #endif
83  // FIXME this can be constexpr, but swig 3.0.8 is not happy
84  static const size_type npos;
85 
86  using Searchable::check;
87  using Searchable::findGroup;
88 
92  Bottle();
93 
104  explicit Bottle(const std::string& text);
105 
111  Bottle(const Bottle& rhs);
112 
118  Bottle(Bottle&& rhs) noexcept;
119 
124  Bottle(std::initializer_list<yarp::os::Value> values);
125 
132  Bottle& operator=(const Bottle& rhs);
133 
140  Bottle& operator=(Bottle&& rhs) noexcept;
141 
145  virtual ~Bottle();
146 
150  void clear();
151 
158  YARP_DEPRECATED_INTERNAL_MSG("Use addInt32 instead") // Since YARP 3.0.0
159  inline void addInt(int x)
160  {
161  addInt32(static_cast<std::int32_t>(x));
162  }
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 
197  void addVocab(int x);
198 
206  YARP_DEPRECATED_INTERNAL_MSG("Use addFloat64 instead") // Since YARP 3.0.0
207  inline void addDouble(double x)
208  {
209  addFloat64(static_cast<yarp::conf::float64_t>(x));
210  }
211 
218  void addFloat32(yarp::conf::float32_t x);
219 
226  void addFloat64(yarp::conf::float64_t x);
227 
233  void addString(const char* str);
234 
240  void addString(const std::string& str);
241 
247  void add(const Value& value);
248 
258  void add(Value* value);
259 
268  Bottle& addList();
269 
276  Property& addDict();
277 
284  Value pop();
285 
298  Value& get(size_type index) const;
299 
305  size_type size() const;
306 
317  void fromString(const std::string& text);
318 
325  void fromBinary(const char* buf, size_t len);
326 
337  const char* toBinary(size_t* size = nullptr);
338 
347  std::string toString() const override;
348 
355  bool write(ConnectionWriter& writer) const override;
356 
363  bool read(ConnectionReader& reader) override;
364 
375  bool write(PortReader& reader, bool textMode = false);
376 
386  bool read(const PortWriter& writer, bool textMode = false);
387 
388  void onCommencement() const override;
389 
390  bool check(const std::string& key) const override;
391 
392  Value& find(const std::string& key) const override;
393 
394  Bottle& findGroup(const std::string& key) const override;
395 
396  bool isNull() const override;
397 
405  void copy(const Bottle& alt, size_type first = 0, size_type len = npos);
406 
412  static Bottle& getNullBottle();
413 
420  bool operator==(const Bottle& alt) const;
421 
428  bool operator!=(const Bottle& alt) const;
429 
436  void append(const Bottle& alt);
437 
444  Bottle tail() const;
445 
453  void hasChanged();
454 
455  static std::string toString(int x);
456 
462  int getSpecialization();
463 
471  static std::string describeBottleCode(int code);
472 
473 
474 protected:
475  void setReadOnly(bool readOnly);
476 
477 
478 private:
479  friend class yarp::os::NetworkBase;
480 #ifndef DOXYGEN_SHOULD_SKIP_THIS
481  friend class yarp::os::impl::Storable;
482  friend class yarp::os::impl::StoreList;
483  friend class yarp::os::impl::BottleImpl;
485 #endif // DOXYGEN_SHOULD_SKIP_THIS
486 };
487 
488 } // namespace os
489 } // namespace yarp
490 
491 #endif // YARP_OS_BOTTLE_H
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
constexpr std::int32_t BOTTLE_TAG_INT
Definition: Bottle.h:37
constexpr std::int32_t BOTTLE_TAG_DOUBLE
Definition: Bottle.h:34
constexpr std::int32_t BOTTLE_TAG_VOCAB
Definition: Bottle.h:40
#define BOTTLE_TAG_VOCAB32
Definition: Bottle.h:25
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:76
size_t size_type
Definition: Bottle.h:81
static const size_type npos
Definition: Bottle.h:84
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:44
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
A class for storing options and configuration information.
Definition: Property.h:37
A base class for nested structures that can be searched.
Definition: Searchable.h:69
A single value (typically within a Bottle).
Definition: Value.h:47
A flexible data format for holding a bunch of numbers and strings.
Definition: BottleImpl.h:36
A single item in a Bottle.
Definition: Storable.h:47
A nested list of items.
Definition: Storable.h:1043
std::string toString(const T &value)
convert an arbitrary type to string.
double float64_t
Definition: numeric.h:80
float float32_t
Definition: numeric.h:79
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:920
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1096
The main, catch-all namespace for YARP.
Definition: environment.h:25
#define YARP_DEPRECATED_INTERNAL_MSG(X)
Definition: api.h:139
#define YARP_os_API
Definition: api.h:19