YARP
Yet Another Robot Platform
BottleImpl.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_IMPL_BOTTLEIMPL_H
9 #define YARP_OS_IMPL_BOTTLEIMPL_H
10 
11 #include <yarp/os/Bytes.h>
12 #include <yarp/os/impl/Storable.h>
13 
14 #include <vector>
15 
16 namespace yarp {
17 namespace os {
18 
19 class Bottle;
20 class Property;
21 class ConnectionReader;
22 class ConnectionWriter;
23 
24 namespace impl {
25 
26 
33 {
34 public:
35  using size_type = size_t;
36  static constexpr size_type npos = static_cast<size_type>(-1);
37 
38  BottleImpl();
39  BottleImpl(Searchable* parent);
40  virtual ~BottleImpl();
41 
43 
44  bool isInt8(int index);
45  bool isInt16(int index);
46  bool isInt32(int index);
47  bool isInt64(int index);
48  bool isFloat32(int index);
49  bool isFloat64(int index);
50  bool isString(int index);
51  bool isList(int index);
52 
53  Storable* pop();
54 
55  Storable& get(size_type index) const;
56 
57  void addInt8(std::int8_t x)
58  {
59  add(new StoreInt8(x));
60  }
61 
62  void addInt16(std::int16_t x)
63  {
64  add(new StoreInt16(x));
65  }
66 
67  void addInt32(std::int32_t x)
68  {
69  add(new StoreInt32(x));
70  }
71 
72  void addInt64(std::int64_t x)
73  {
74  add(new StoreInt64(x));
75  }
76 
78  {
79  add(new StoreFloat32(x));
80  }
81 
83  {
84  add(new StoreFloat64(x));
85  }
86 
88  {
89  add(new StoreVocab32(x));
90  }
91 
92  void addString(const std::string& text)
93  {
94  add(new StoreString(text));
95  }
96 
97  yarp::os::Bottle& addList();
98 
99  yarp::os::Property& addDict();
100 
101  void clear();
102 
103  void fromString(const std::string& line);
104  std::string toString() const;
105  size_type size() const;
106 
107  bool read(ConnectionReader& reader);
108  bool write(ConnectionWriter& writer) const;
109 
110  void onCommencement();
111 
112  const char* getBytes() const;
113  size_t byteCount() const;
114 
115  void copyRange(const BottleImpl* alt, size_type first = 0, size_type len = npos);
116 
117  bool fromBytes(const yarp::os::Bytes& data);
118  void toBytes(yarp::os::Bytes& data);
119 
120  bool fromBytes(yarp::os::ConnectionReader& reader);
121 
122  void fromBinary(const char* text, size_t len);
123 
124  void specialize(std::int32_t subCode);
125  int getSpecialization();
126  void setNested(bool nested);
127 
128  std::int32_t subCode();
129 
131  {
132  // all Values are Storables -- important invariant!
133  add((Storable*)(bit));
134  }
135 
136  void addBit(const yarp::os::Value& bit)
137  {
138  // all Values are Storables -- important invariant!
139  if (!bit.isNull()) {
140  add((Storable*)(bit.clone()));
141  }
142  }
143 
144  yarp::os::Value& addBit(const char* str)
145  {
146  size_type len = size();
147  std::string x(str);
148  smartAdd(x);
149  if (size() > len) {
150  return get((int)size() - 1);
151  }
152  return get(-1);
153  }
154 
155  static StoreNull& getNull()
156  {
157  static StoreNull storeNull;
158  return storeNull;
159  }
160 
161  // check if a piece of text is a completed bottle
162  static bool isComplete(const char* txt);
163 
164  void hasChanged()
165  {
166  dirty = true;
167  }
168 
169  bool checkIndex(size_type index) const;
170 
171  bool invalid;
172  bool ro;
173 
174  void edit();
175 
176  Value& findGroupBit(const std::string& key) const;
177  Value& findBit(const std::string& key) const;
178 
179 private:
180  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<Storable*>) content;
181  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<char>) data;
182  int speciality;
183  bool nested;
184  bool dirty;
185 
186  void add(Storable* s);
187  void smartAdd(const std::string& str);
188 
189  /*
190  * Bottle is using a lazy synchronization method. Whenever some operation
191  * is performed, a dirty flag is set, and when it is used, the synch()
192  * method is called.
193  *
194  * The const version of the synch() method performs a const_cast, and
195  * calls the non-const version. This allows to call it in const methods.
196  * Conceptually this is not completely wrong because it does not modify
197  * the external state of the class, but just some internal representation.
198  */
199  void synch();
200  void synch() const;
201 };
202 
203 } // namespace impl
204 } // namespace os
205 } // namespace yarp
206 
207 
208 #endif // YARP_OS_IMPL_BOTTLEIMPL_H
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
An interface for reading from a network connection.
An interface for writing to a network connection.
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
A single value (typically within a Bottle).
Definition: Value.h:45
virtual Value * clone() const
Create a copy of the value.
Definition: Value.cpp:368
bool isNull() const override
Checks if the object is invalid.
Definition: Value.cpp:380
A flexible data format for holding a bunch of numbers and strings.
Definition: BottleImpl.h:33
void addFloat64(yarp::conf::float64_t x)
Definition: BottleImpl.h:82
void addInt32(std::int32_t x)
Definition: BottleImpl.h:67
void addVocab32(yarp::conf::vocab32_t x)
Definition: BottleImpl.h:87
void addBit(yarp::os::Value *bit)
Definition: BottleImpl.h:130
void addBit(const yarp::os::Value &bit)
Definition: BottleImpl.h:136
void addInt8(std::int8_t x)
Definition: BottleImpl.h:57
void addString(const std::string &text)
Definition: BottleImpl.h:92
void addFloat32(yarp::conf::float32_t x)
Definition: BottleImpl.h:77
static StoreNull & getNull()
Definition: BottleImpl.h:155
yarp::os::Value & addBit(const char *str)
Definition: BottleImpl.h:144
void addInt64(std::int64_t x)
Definition: BottleImpl.h:72
void addInt16(std::int16_t x)
Definition: BottleImpl.h:62
A single item in a Bottle.
Definition: Storable.h:44
A 32-bit floating point number item.
Definition: Storable.h:681
A 64-bit floating point number item.
Definition: Storable.h:756
A 16-bit integer item.
Definition: Storable.h:425
A 32-bit integer item.
Definition: Storable.h:511
A 64-bit integer item.
Definition: Storable.h:596
A 8-bit integer item.
Definition: Storable.h:339
An empty item.
Definition: Storable.h:286
A vocabulary item.
Definition: Storable.h:831
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
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:922
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1098
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:336
#define YARP_os_impl_API
Definition: api.h:46