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