YARP
Yet Another Robot Platform
Vector.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include <yarp/sig/Vector.h>
11 
12 #include <yarp/conf/system.h>
13 
14 #include <yarp/os/Bottle.h>
17 #include <yarp/os/LogComponent.h>
18 #include <yarp/os/NetInt32.h>
19 #include <yarp/os/NetFloat64.h>
20 
21 #include <yarp/sig/Matrix.h>
22 
23 #include <vector>
24 #include <cinttypes>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <map>
28 
29 using namespace yarp::sig;
30 using namespace yarp::os;
31 
32 namespace {
33 YARP_LOG_COMPONENT(VECTOR, "yarp.sig.Vector")
34 }
35 
37 
40 {
41  yarp::os::NetInt32 listTag{0};
42  yarp::os::NetInt32 listLen{0};
43 };
45 
46 const std::map<int, std::string> tag2FormatStr = {
47  {BOTTLE_TAG_INT32, PRId32},
48  {BOTTLE_TAG_INT64, PRId64},
49  {BOTTLE_TAG_VOCAB, "c"},
50  {BOTTLE_TAG_STRING, "s"},
51  {BOTTLE_TAG_FLOAT64, "lf"},
52 };
53 
55  // auto-convert text mode interaction
56  connection.convertTextMode();
58  bool ok = connection.expectBlock((char*)&header, sizeof(header));
59  if (!ok) return false;
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  char* ptr = getMemoryBlock();
66  yCAssert(VECTOR, ptr != nullptr);
67  int elemSize=getElementSize();
68  ok = connection.expectBlock(ptr, elemSize*header.listLen);
69  if (!ok) return false;
70  } else {
71  return false;
72  }
73 
74  return !connection.isError();
75 }
76 
79 
80  //header.totalLen = sizeof(header)+sizeof(double)*this->size();
81  header.listTag = (BOTTLE_TAG_LIST | getBottleTag());
82  header.listLen = (int)getListSize();
83 
84  connection.appendBlock((char*)&header, sizeof(header));
85  const char *ptr = getMemoryBlock();
86  int elemSize=getElementSize();
87  yCAssert(VECTOR, ptr != nullptr);
88 
89  connection.appendExternalBlock(ptr, elemSize*header.listLen);
90 
91  // if someone is foolish enough to connect in text mode,
92  // let them see something readable.
93  connection.convertTextMode();
94 
95  return !connection.isError();
96 }
97 
98 std::string VectorBase::getFormatStr(int tag) const
99 {
100  std::string ret;
101  auto it = tag2FormatStr.find(tag);
102 
103  if (it != tag2FormatStr.end())
104  {
105  ret = it->second;
106  }
107 
108  return ret;
109 }
yarp::os::ConnectionWriter::appendBlock
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
Vector.h
contains the definition of a Vector type
yarp::sig
Signal processing.
Definition: Image.h:25
YARP_END_PACK
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:194
BOTTLE_TAG_LIST
#define BOTTLE_TAG_LIST
Definition: Bottle.h:30
ConnectionWriter.h
NetInt32.h
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
YARP_BEGIN_PACK
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:193
BOTTLE_TAG_STRING
#define BOTTLE_TAG_STRING
Definition: Bottle.h:28
Matrix.h
contains the definition of a Matrix type
tag2FormatStr
const std::map< int, std::string > tag2FormatStr
Definition: Vector.cpp:46
yarp::sig::VectorBase::getFormatStr
virtual std::string getFormatStr(int tag) const
Definition: Vector.cpp:98
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
BOTTLE_TAG_INT32
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
VectorPortContentHeader
Definition: Vector.cpp:40
yarp::os::ConnectionWriter::isError
virtual bool isError() const =0
BOTTLE_TAG_VOCAB
#define BOTTLE_TAG_VOCAB
Definition: Bottle.h:25
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
BOTTLE_TAG_INT64
#define BOTTLE_TAG_INT64
Definition: Bottle.h:24
yarp::os::ConnectionReader::isError
virtual bool isError() const =0
NetFloat64.h
yarp::os::ConnectionReader::convertTextMode
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
yarp::sig::VectorBase::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: Vector.cpp:54
LogComponent.h
system.h
yarp::os::ConnectionWriter::convertTextMode
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
BOTTLE_TAG_FLOAT64
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
VectorPortContentHeader::listLen
yarp::os::NetInt32 listLen
Definition: Vector.cpp:42
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::ConnectionWriter::appendExternalBlock
virtual void appendExternalBlock(const char *data, size_t len)=0
Send a block of data to the network connection, without making a copy.
yarp::os::ConnectionReader::expectBlock
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
Bottle.h
VectorPortContentHeader::listTag
yarp::os::NetInt32 listTag
Definition: Vector.cpp:41
yarp::sig::VectorBase::write
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition: Vector.cpp:77
ConnectionReader.h
yarp::os::NetInt32
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:33