22 #include <yarp/os/Bottle.h> 23 #include <yarp/os/Log.h> 24 #include <yarp/os/LogStream.h> 25 #include <yarp/os/ConnectionWriter.h> 26 #include "event-driven/vCodec.h" 47 yarp::os::Bottle * b = yarp::os::Bottle::find(e->getType()).asList();
50 yarp::os::Bottle::addString(e->getType());
51 b = &(yarp::os::Bottle::addList());
70 for(
size_t tagi = 0; tagi < eb.yarp::os::Bottle::size(); tagi+=2) {
73 const std::string tagname =
74 eb.yarp::os::Bottle::get(tagi).asString();
76 yError() <<
"Could not get tagname during vBottle append." 77 "Check vBottle integrity.";
82 event<> e = createEvent(tagname);
84 std::cerr <<
"Warning: could not get bottle type during vBottle::" 85 "append<>(). Check vBottle integrity." << std::endl;
88 if(!std::dynamic_pointer_cast<T>(e))
continue;
92 yarp::os::Bottle *b_from = eb.yarp::os::Bottle::get(tagi+1).asList();
94 std::cerr <<
"Warning: From-list empty during vBottle append." 95 "Check vBottle integrity." << std::endl;
100 yarp::os::Bottle *b_to = yarp::os::Bottle::find(tagname).asList();
102 yarp::os::Bottle::addString(tagname);
103 b_to = &(yarp::os::Bottle::addList());
107 b_to->append(*b_from);
114 template<
class T> vQueue
get() {
127 for(
size_t i = 0; i < Bottle::size(); i+=2) {
130 event<> e = createEvent(Bottle::get(i).asString());
132 yError() <<
"Warning: could not get bottle type during vBottle::" 133 "get<>(). Check vBottle integrity.";
138 if(!std::dynamic_pointer_cast<T>(e)) {
143 Bottle * b = Bottle::get(i+1).asList();
145 yError() <<
"Warning: could not get event data as a list after " 146 "getting correct tag (e.g. AE) in vBottle::getAll(). " 147 "Check vBottle integrity";
154 while(pos_b < b->size()) {
155 if(e->decode(*b, pos_b)) {
156 q.push_back(e->clone());
174 vQueue q = this->get<vEvent>();
215 using yarp::os::Bottle::find;
227 void fromString(
const std::string& text);
228 void append(
const yarp::os::Bottle &alt);
236 using yarp::os::Bottle::findGroup;
238 yarp::os::Bottle tail()
const;
242 std::string getString();
245 yarp::os::Value&
get();
247 yarp::os::Value pop();
259 std::vector<std::int32_t> header1;
261 std::vector<std::int32_t> header3;
264 const char * datablock;
265 unsigned int datalength;
266 std::vector<std::int32_t> internaldata;
269 unsigned int elementINTS;
270 unsigned int elementBYTES;
276 header1.push_back(BOTTLE_TAG_LIST);
277 header1.push_back(2);
278 header1.push_back(BOTTLE_TAG_STRING);
279 header1.push_back(2);
281 header3.push_back(BOTTLE_TAG_LIST|BOTTLE_TAG_INT);
282 header3.push_back(0);
284 elementBYTES =
sizeof(std::int32_t) * elementINTS;
290 header3[1] = elementINTS * (datalength / elementBYTES);
291 this->datablock = datablock;
292 this->datalength = elementBYTES * header3[1] / elementINTS;
299 header3[1] = elementINTS * q.size();
301 if((
int)internaldata.size() < header3[1])
302 internaldata.resize(header3[1]);
304 unsigned int pos = 0;
305 for(
unsigned int i = 0; i < q.size(); i++)
306 q[i]->encode(internaldata, pos);
308 if(pos != (
unsigned int)header3[1])
309 yError() <<
"vBottleMimic: encoding incorrect";
311 this->datablock = (
const char *)internaldata.data();
312 this->datalength = elementBYTES * q.size();
317 header1[3] = eventtype.size();
321 yarp::os::Bottle temp;
322 event<> v = ev::createEvent(eventtype);
324 elementINTS = temp.size();
325 elementBYTES =
sizeof(std::int32_t) * elementINTS;
330 virtual bool read(yarp::os::ConnectionReader& connection) {
335 virtual bool write(yarp::os::ConnectionWriter& connection)
const {
337 connection.appendBlock((
const char *)header1.data(),
338 header1.size() *
sizeof(std::int32_t));
339 connection.appendBlock(header2.c_str(), header1[3]);
340 connection.appendBlock((
const char *)header3.data(),
341 header3.size() *
sizeof(std::int32_t));
342 connection.appendBlock(datablock, datalength);
344 return !connection.isError();
yarp::os::Bottle wrapper for sending events through the yarp system with ensuring compatibility with ...
Definition: vBottle.h:33
virtual bool write(yarp::os::ConnectionWriter &connection) const
write the data on the connection.
Definition: vBottle.h:335
vBottle()
default constructor
Definition: vBottle.h:39
a vBottle that avoids memory allocation where possible and can be correctly decoded when read from a ...
Definition: vBottle.h:254
virtual bool read(yarp::os::ConnectionReader &connection)
does nothing as this is a write-only port.
Definition: vBottle.h:330
vQueue getAll()
decode all events into a vQueue
Definition: vBottle.h:173
void addtoendof(vQueue &q)
add a specific event-type from the vBottle to the end of a vQueue
Definition: vBottle.h:124
vQueue getSorted()
get a specific event-type and ensure they are in correct temporal order
Definition: vBottle.h:165
void append(vBottle &eb)
append
Definition: vBottle.h:66
vQueue getAllSorted()
decode all events into a vQueue and ensure they are in correct temporal order
Definition: vBottle.h:180
void setInternalData(const vQueue &q)
send an entire vQueue. The queue is encoded and allocated into a single contiguous memory space...
Definition: vBottle.h:297
void append(vBottle &eb)
add all the contents of a vBottle to the current vBottle
Definition: vBottle.h:60
void setHeader(std::string eventtype)
set the type of event that this vBottleMimic will send
Definition: vBottle.h:316
void addEvent(event<> e)
add a single event to the vBottle
Definition: vBottle.h:43
vBottleMimic()
instantiate the correct headers for a Bottle
Definition: vBottle.h:275
void setExternalData(const char *datablock, unsigned int datalength)
for data already allocated in contiguous space. Just send this data on a port without memory realloca...
Definition: vBottle.h:289