YARP
Yet Another Robot Platform
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
37{
40};
42
43const std::map<int, std::string> tag2FormatStr = {
44 {BOTTLE_TAG_INT32, PRId32},
45 {BOTTLE_TAG_INT64, PRId64},
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);
67 int elemSize=getElementSize();
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 const char *ptr = getMemoryBlock();
88 int elemSize=getElementSize();
89 yCAssert(VECTOR, ptr != nullptr);
90
91 connection.appendExternalBlock(ptr, elemSize*header.listLen);
92
93 // if someone is foolish enough to connect in text mode,
94 // let them see something readable.
95 connection.convertTextMode();
96
97 return !connection.isError();
98}
99
100std::string VectorBase::getFormatStr(int tag) const
101{
102 std::string ret;
103 auto it = tag2FormatStr.find(tag);
104
105 if (it != tag2FormatStr.end())
106 {
107 ret = it->second;
108 }
109
110 return ret;
111}
#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
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
virtual std::string getFormatStr(int tag) const
Definition: Vector.cpp:100
virtual int getBottleTag() const =0
#define yCAssert(component, x)
Definition: LogComponent.h:240
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
An interface to the operating system, including Port based communication.
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:29
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:915
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