YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Vector.cpp
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-License-Identifier: BSD-3-Clause
5 */
6
7#include <yarp/sig/Vector.h>
8
9#include <yarp/conf/system.h>
10
11#include <yarp/os/Bottle.h>
15#include <yarp/os/NetInt32.h>
16#include <yarp/os/NetFloat64.h>
17
18#include <yarp/sig/Matrix.h>
19
20#include <vector>
21#include <cinttypes>
22#include <cstdio>
23#include <cstdlib>
24#include <map>
25
26using namespace yarp::sig;
27using namespace yarp::os;
28
29namespace {
30YARP_LOG_COMPONENT(VECTOR, "yarp.sig.Vector")
31}
32
34
42
43const std::map<int, std::string> tag2FormatStr = {
46 {BOTTLE_TAG_VOCAB32, "c"},
47 {BOTTLE_TAG_STRING, "s"},
48 {BOTTLE_TAG_FLOAT64, "lf"},
49};
50
52 // auto-convert text mode interaction
53 connection.convertTextMode();
55 bool ok = connection.expectBlock((char*)&header, sizeof(header));
56 if (!ok) {
57 return false;
58 }
59 if (header.listLen > 0 &&
60 header.listTag == (BOTTLE_TAG_LIST | getBottleTag()))
61 {
62 if ((size_t)getListSize() != (size_t)(header.listLen)) {
63 resize(header.listLen);
64 }
65 char* ptr = getMemoryBlock();
66 yCAssert(VECTOR, ptr != nullptr);
68 ok = connection.expectBlock(ptr, elemSize*header.listLen);
69 if (!ok) {
70 return false;
71 }
72 } else {
73 return false;
74 }
75
76 return !connection.isError();
77}
78
81
82 //header.totalLen = sizeof(header)+sizeof(double)*this->size();
84 header.listLen = (int)getListSize();
85
86 connection.appendBlock((char*)&header, sizeof(header));
87
88 if (header.listLen > 0)
89 {
90 const char *ptr = getMemoryBlock();
92 yCAssert(VECTOR, ptr != nullptr);
93
94 connection.appendExternalBlock(ptr, elemSize*header.listLen);
95 }
96
97 // if someone is foolish enough to connect in text mode,
98 // let them see something readable.
99 connection.convertTextMode();
100
101 return !connection.isError();
102}
103
104std::string VectorBase::getFormatStr(int tag) const
105{
106 std::string ret;
107 auto it = tag2FormatStr.find(tag);
108
109 if (it != tag2FormatStr.end())
110 {
111 ret = it->second;
112 }
113
114 return ret;
115}
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:25
#define BOTTLE_TAG_INT64
Definition Bottle.h:22
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_STRING
Definition Bottle.h:26
#define BOTTLE_TAG_LIST
Definition Bottle.h:28
#define BOTTLE_TAG_VOCAB32
Definition Bottle.h:23
bool ret
contains the definition of a Matrix type
const std::map< int, std::string > tag2FormatStr
Definition Vector.cpp:43
contains the definition of a Vector type
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
virtual bool isError() const =0
An interface for writing to a network connection.
virtual bool isError() const =0
virtual void appendExternalBlock(const char *data, size_t len)=0
Send a block of data to the network connection, without making a copy.
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
virtual size_t getListSize() const =0
virtual size_t getElementSize() const =0
virtual void resize(size_t size)=0
virtual const char * getMemoryBlock() const =0
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition Vector.cpp:79
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition Vector.cpp:51
virtual std::string getFormatStr(int tag) const
Definition Vector.cpp:104
virtual int getBottleTag() const =0
#define yCAssert(component, x)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition NetInt32.h:29
yarp::os::NetInt32 listLen
Definition Vector.cpp:39
yarp::os::NetInt32 listTag
Definition Vector.cpp:38
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition system.h:193
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition system.h:192