YARP  2.3.70.1
Yet Another Robot Platform
Bottle.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006, 2008 RobotCub Consortium, Arjan Gijsberts
3  * Authors: Paul Fitzpatrick, Arjan Gijsberts
4  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
5  */
6 
7 #include <yarp/os/Bottle.h>
9 #include <yarp/os/NetType.h>
10 
12 #include <yarp/os/impl/Logger.h>
13 
14 
15 using yarp::os::Bottle;
19 using yarp::os::Property;
20 using yarp::os::Value;
23 
24 class NullBottle : public Bottle
25 {
26 public:
27  NullBottle() : Bottle() { setReadOnly(true); }
28  virtual bool isNull() const override { return true; }
30 };
31 
32 NullBottle* NullBottle::bottleNull = YARP_NULLPTR;
33 
34 Bottle::Bottle()
35  : Portable(), Searchable(), implementation(new BottleImpl(this))
36 {
37  yAssert(implementation != YARP_NULLPTR);
38  implementation->invalid = false;
39  implementation->ro = false;
40 }
41 
43  : Portable(), Searchable(), implementation(new BottleImpl(this))
44 {
45  yAssert(implementation != YARP_NULLPTR);
46  implementation->invalid = false;
47  implementation->ro = false;
48  fromString(text);
49 }
50 
51 Bottle::Bottle(const Bottle& bottle)
52  : Portable(), Searchable(), implementation(new BottleImpl(this))
53 {
54  yAssert(implementation != YARP_NULLPTR);
55  implementation->invalid = false;
56  implementation->ro = false;
57  copy(bottle);
58 }
59 
61 {
63  copy(bottle);
64  return *this;
65 }
66 
68 {
69  if (implementation) {
70  delete implementation;
71  }
72 }
73 
75 {
77  implementation->invalid = false;
79 }
80 
81 void Bottle::addInt(int x)
82 {
85 }
86 
87 void Bottle::addInt64(const YARP_INT64& x)
88 {
91 }
92 
93 void Bottle::addVocab(int x)
94 {
97 }
98 
99 void Bottle::addDouble(double x)
100 {
101  implementation->edit();
103 }
104 
105 void Bottle::addString(const char* str)
106 {
107  implementation->edit();
109 }
110 
112 {
113  implementation->edit();
115 }
116 
118 {
119  implementation->edit();
120  return implementation->addList();
121 }
122 
124 {
125  implementation->edit();
126  return implementation->addDict();
127 }
128 
130 {
131  implementation->edit();
132  Storable* stb = implementation->pop();
133  Value val(*stb);
134  // here we take responsibility for deallocation of the Storable instance
135  delete stb;
136  return val;
137 }
138 
140 {
141  implementation->edit();
142  implementation->invalid = false;
144 }
145 
147 {
149 }
150 
151 void Bottle::fromBinary(const char* buf, int len)
152 {
153  implementation->edit();
154  implementation->fromBinary(buf, len);
155 }
156 
157 const char* Bottle::toBinary(size_t* size)
158 {
159  if (size != YARP_NULLPTR) {
160  *size = implementation->byteCount();
161  }
162  return implementation->getBytes();
163 }
164 
166 {
167  return implementation->write(writer);
168 }
169 
171 {
173 }
174 
176 {
177  implementation->edit();
178  return implementation->read(reader);
179 }
180 
181 Value& Bottle::get(int index) const
182 {
183  return implementation->get(index);
184 }
185 
186 int Bottle::size() const
187 {
188  return static_cast<int>(implementation->size());
189 }
190 
192 {
194 }
195 
197 {
199 }
200 
201 void Bottle::copy(const Bottle& alt, int first, int len)
202 {
203  implementation->edit();
204  if (alt.isNull()) {
205  clear();
206  implementation->invalid = true;
207  return;
208  }
209  implementation->copyRange(alt.implementation, first, len);
210 }
211 
212 bool Bottle::check(const ConstString& key) const
213 {
214  Bottle& val = findGroup(key);
215  if (!val.isNull()) {
216  return true;
217  }
218  Value& val2 = find(key);
219  return !val2.isNull();
220 }
221 
222 Value& Bottle::find(const ConstString& key) const
223 {
224  Value& val = implementation->findBit(key);
225 
226  if (getMonitor() != YARP_NULLPTR) {
227  SearchReport report;
228  report.key = key;
229  report.isFound = !val.isNull();
230  report.value = val.toString();
231  reportToMonitor(report);
232  }
233 
234  return val;
235 }
236 
238 {
239  Value& bb = implementation->findGroupBit(key);
240 
241  if (getMonitor() != YARP_NULLPTR) {
242  SearchReport report;
243  report.key = key;
244  report.isGroup = true;
245  if (bb.isList()) {
246  report.isFound = true;
247  report.value = bb.toString();
248  }
249  reportToMonitor(report);
250  if (bb.isList()) {
251  ConstString context = getMonitorContext().c_str();
252  context += ".";
253  context += key;
254  bb.asList()->setMonitor(getMonitor(),
255  context.c_str()); // pass on any monitoring
256  }
257  }
258 
259  if (bb.isList()) {
260  return *(bb.asList());
261  }
262  return getNullBottle();
263 }
264 
265 void Bottle::add(Value* value)
266 {
267  implementation->edit();
268  implementation->addBit(value);
269 }
270 
271 void Bottle::add(const Value& value)
272 {
273  implementation->edit();
274  implementation->addBit(value);
275 }
276 
278 {
279  if (NullBottle::bottleNull == YARP_NULLPTR) {
280  NullBottle::bottleNull = new NullBottle();
281  }
282  return *NullBottle::bottleNull;
283 }
284 
286 {
287  if (NullBottle::bottleNull != YARP_NULLPTR) {
288  delete NullBottle::bottleNull;
289  NullBottle::bottleNull = YARP_NULLPTR;
290  }
291 }
292 
293 bool Bottle::operator==(const Bottle& alt)
294 {
295  return ConstString(toString().c_str()) == alt.toString().c_str();
296 }
297 
298 bool Bottle::write(PortReader& reader, bool textMode)
299 {
300  DummyConnector con;
301  con.setTextMode(textMode);
302  write(con.getWriter());
303  return reader.read(con.getReader());
304 }
305 
306 bool Bottle::read(PortWriter& writer, bool textMode)
307 {
308  implementation->edit();
309  DummyConnector con;
310  con.setTextMode(textMode);
311  writer.write(con.getWriter());
312  return read(con.getReader());
313 }
314 
315 bool Bottle::isNull() const
316 {
317  return implementation->invalid;
318 }
319 
320 bool Bottle::operator!=(const Bottle& alt)
321 {
322  return !((*this) == alt);
323 }
324 
325 void Bottle::append(const Bottle& alt)
326 {
327  implementation->edit();
328  for (int i = 0; i < alt.size(); i++) {
329  add(alt.get(i));
330  }
331 }
332 
334 {
335  Bottle b;
336  if (isNull()) {
337  return *this;
338  }
339  b.copy(*this, 1, size() - 1);
340  return b;
341 }
342 
344 {
345  return NetType::toString(x);
346 }
347 
349 {
350  int unit = code & ~(BOTTLE_TAG_LIST | BOTTLE_TAG_DICT);
351  ConstString unitName = "mixed";
352  switch (unit) {
353  case 0:
354  unitName = "mixed";
355  break;
356  case BOTTLE_TAG_INT:
357  unitName = "int";
358  break;
359  case BOTTLE_TAG_VOCAB:
360  unitName = "vocab";
361  break;
362  case BOTTLE_TAG_DOUBLE:
363  unitName = "float";
364  break;
365  case BOTTLE_TAG_STRING:
366  unitName = "string";
367  break;
368  case BOTTLE_TAG_BLOB:
369  unitName = "blob";
370  break;
371  default:
372  unitName = "unknown";
373  break;
374  }
375  ConstString result = unitName;
376  if (code & BOTTLE_TAG_LIST) {
377  result = "list of " + unitName;
378  } else if (code & BOTTLE_TAG_DICT) {
379  result = "dict of " + unitName;
380  }
381  return result;
382 }
383 
384 void Bottle::setReadOnly(bool readOnly)
385 {
386  implementation->ro = readOnly;
387 }
388 
389 
390 #ifndef YARP_NO_DEPRECATED // since YARP 2.3.65
391 void Bottle::add(const char* txt)
392 {
393  addString(txt);
394 }
395 #endif
dest b
Definition: ImageCopy.cpp:91
static ConstString describeBottleCode(int code)
Convert a numeric bottle code to a string.
Definition: Bottle.cpp:348
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
Definition: Bottle.cpp:325
#define BOTTLE_TAG_STRING
Definition: Bottle.h:20
bool operator!=(const Bottle &alt)
Inequality test.
Definition: Bottle.cpp:320
static ConstString toString(int x)
Definition: NetType.cpp:41
int size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:186
yarp::os::Bottle & addList()
static NullBottle * bottleNull
Definition: Bottle.cpp:29
virtual bool write(ConnectionWriter &writer)=0
Write this object to a network connection.
virtual bool isNull() const override
Checks if the object is invalid.
Definition: Bottle.cpp:28
A dummy connection to test yarp::os::Portable implementations.
A single item in a Bottle.
Definition: BottleImpl.h:46
A class for storing options and configuration information.
Definition: Property.h:32
A base class for nested structures that can be searched.
Definition: Searchable.h:56
void addInt64(const YARP_INT64 &x)
Definition: BottleImpl.h:460
This is a base class for objects that can be both read from and be written to the YARP network...
Definition: Portable.h:26
NullBottle()
Definition: Bottle.cpp:27
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:105
#define BOTTLE_TAG_DOUBLE
Definition: Bottle.h:19
int getSpecialization()
Get numeric bottle code for this bottle.
Definition: Bottle.cpp:196
virtual bool isNull() const YARP_OVERRIDE
Checks if the object is invalid.
Definition: Value.cpp:327
void addString(const yarp::os::ConstString &text)
Definition: BottleImpl.h:463
yarp::os::impl::BottleImpl *const implementation
Definition: Bottle.h:397
void onCommencement() YARP_OVERRIDE
This is called when the port is about to begin writing operations.
Definition: Bottle.cpp:170
virtual bool read(ConnectionReader &reader) override
Read this object from a network connection.
Definition: BottleImpl.cpp:563
virtual bool read(ConnectionReader &reader)=0
Read this object from a network connection.
virtual bool write(ConnectionWriter &writer) override
Write this object to a network connection.
Definition: BottleImpl.cpp:532
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition: Bottle.cpp:271
void addBit(yarp::os::Value *bit)
Definition: BottleImpl.h:501
Value pop()
Removes a Value v from the end of the list and returns this value.
Definition: Bottle.cpp:129
const char * toBinary(size_t *size=YARP_NULLPTR)
Returns binary representation of bottle.
Definition: Bottle.cpp:157
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
void fromBinary(const char *text, int len)
Definition: BottleImpl.cpp:453
A string with almost the same api as std::string.
Definition: ConstString.h:44
void copy(const Bottle &alt, int first=0, int len=-1)
Copy all or part of another Bottle.
Definition: Bottle.cpp:201
void addDouble(double x)
Definition: BottleImpl.h:462
void setTextMode(bool textmode)
Set the textMode of the dummy connection.
#define BOTTLE_TAG_VOCAB
Definition: Bottle.h:18
void setReadOnly(bool readOnly)
Definition: Bottle.cpp:384
Bottle & operator=(const Bottle &bottle)
Assignment operator.
Definition: Bottle.cpp:60
An interface for writing to a network connection.
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:74
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
bool operator==(const Bottle &alt)
Equality test.
Definition: Bottle.cpp:293
ConstString toString() const YARP_OVERRIDE
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:146
yarp::os::Property & addDict()
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:202
void fromString(const ConstString &line)
Definition: BottleImpl.cpp:233
A flexible data format for holding a bunch of numbers and strings.
Definition: BottleImpl.h:435
ConstString toString() const YARP_OVERRIDE
Return a standard text representation of the content of the object.
Definition: Value.cpp:303
Storable & get(int index) const
A simple collection of objects that can be described and transmitted in a portable way...
Definition: Bottle.h:57
static void fini()
Definition: Bottle.cpp:285
#define BOTTLE_TAG_BLOB
Definition: Bottle.h:21
void fromString(const ConstString &text)
Initializes bottle from a string.
Definition: Bottle.cpp:139
void copyRange(const BottleImpl *alt, int first=0, int len=-1)
Value & findBit(const ConstString &key) const
Bottle & findGroup(const ConstString &key) const YARP_OVERRIDE
Gets a list corresponding to a given keyword.
Definition: Bottle.cpp:237
Bottle tail() const
Get all but the first element of a bottle.
Definition: Bottle.cpp:333
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:93
#define yAssert(x)
Definition: Log.h:118
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:117
ConnectionReader & getReader()
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
bool read(ConnectionReader &reader) YARP_OVERRIDE
Set the bottle&#39;s value based on input from a network connection.
Definition: Bottle.cpp:175
An interface for reading from a network connection.
virtual ~Bottle()
Destructor.
Definition: Bottle.cpp:67
void addInt(int x)
Places an integer in the bottle, at the end of the list.
Definition: Bottle.cpp:81
#define BOTTLE_TAG_INT
Definition: Bottle.h:17
bool write(ConnectionWriter &writer) YARP_OVERRIDE
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:165
virtual Value & find(const ConstString &key) const YARP_OVERRIDE
Gets a value corresponding to a given keyword.
Definition: Bottle.cpp:222
void addDouble(double x)
Places a floating point number in the bottle, at the end of the list.
Definition: Bottle.cpp:99
A single value (typically within a Bottle).
Definition: Value.h:36
static Bottle & getNullBottle()
A special Bottle with no content.
Definition: Bottle.cpp:277
const char * c_str() const
Accesses the character sequence stored in this object.
Definition: ConstString.cpp:88
virtual void onCommencement() override
This is called when the port is about to begin writing operations.
Definition: BottleImpl.cpp:527
virtual bool isNull() const YARP_OVERRIDE
Checks if the object is invalid.
Definition: Bottle.cpp:315
#define BOTTLE_TAG_LIST
Definition: Bottle.h:23
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
#define BOTTLE_TAG_DICT
Definition: Bottle.h:24
virtual bool isList() const
Checks if value is a list.
Definition: Value.cpp:142
void hasChanged()
Declare that the content of the Bottle has been changed.
Definition: Bottle.cpp:191
void fromBinary(const char *buf, int len)
Initializes bottle from a binary representation.
Definition: Bottle.cpp:151
virtual bool check(const ConstString &key) const YARP_OVERRIDE
Check if there exists a property of the given name.
Definition: Bottle.cpp:212
Bottle()
Constructor.
Definition: Bottle.cpp:34
Property & addDict()
Places an empty key/value object in the bottle, at the end of the list.
Definition: Bottle.cpp:123
void addInt64(const YARP_INT64 &x)
Places a 64 integer in the bottle, at the end of the list.
Definition: Bottle.cpp:87
Value & get(int index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:181
Value & findGroupBit(const ConstString &key) const