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"},
50};
51
53 // auto-convert text mode interaction
54 connection.convertTextMode();
56 bool ok = connection.expectBlock((char*)&header, sizeof(header));
57 if (!ok) {
58 return false;
59 }
60 if (header.listLen > 0 &&
61 header.listTag == (BOTTLE_TAG_LIST | getBottleTag()))
62 {
63 if ((size_t)getListSize() != (size_t)(header.listLen)) {
64 resize(header.listLen);
65 }
66 char* ptr = getMemoryBlock();
67 yCAssert(VECTOR, ptr != nullptr);
69 ok = connection.expectBlock(ptr, elemSize*header.listLen);
70 if (!ok) {
71 return false;
72 }
73 } else {
74 return false;
75 }
76
77 return !connection.isError();
78}
79
82
83 //header.totalLen = sizeof(header)+sizeof(double)*this->size();
85 header.listLen = (int)getListSize();
86
87 connection.appendBlock((char*)&header, sizeof(header));
88
89 if (header.listLen > 0)
90 {
91 const char *ptr = getMemoryBlock();
93 yCAssert(VECTOR, ptr != nullptr);
94
95 connection.appendExternalBlock(ptr, elemSize*header.listLen);
96 }
97
98 // if someone is foolish enough to connect in text mode,
99 // let them see something readable.
100 connection.convertTextMode();
101
102 return !connection.isError();
103}
104
105std::string VectorBase::getFormatStr(int tag) const
106{
107 std::string ret;
108 auto it = tag2FormatStr.find(tag);
109
110 if (it != tag2FormatStr.end())
111 {
112 ret = it->second;
113 }
114
115 return ret;
116}
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:26
#define BOTTLE_TAG_INT64
Definition Bottle.h:22
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_STRING
Definition Bottle.h:27
#define BOTTLE_TAG_LIST
Definition Bottle.h:29
#define BOTTLE_TAG_VOCAB32
Definition Bottle.h:23
#define BOTTLE_TAG_FLOAT32
Definition Bottle.h:25
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:80
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition Vector.cpp:52
virtual std::string getFormatStr(int tag) const
Definition Vector.cpp:105
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