YARP
Yet Another Robot Platform
PortablePair.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/os/PortablePair.h>
11 
14 
15 
17  Portable& head,
18  Portable& body)
19 {
20  // if someone connects in text mode, use standard
21  // text-to-binary mapping
22  connection.convertTextMode();
23 
24  std::int32_t header = connection.expectInt32();
25  if (header != BOTTLE_TAG_LIST) {
26  return false;
27  }
28  std::int32_t len = connection.expectInt32();
29  if (len != 2) {
30  return false;
31  }
32 
33  bool ok = head.read(connection);
34  if (ok) {
35  ok = body.read(connection);
36  }
37  return ok;
38 }
39 
41  const Portable& head,
42  const Portable& body)
43 {
44  connection.appendInt32(BOTTLE_TAG_LIST); // nested structure
45  connection.appendInt32(2); // with two elements
46  bool ok = head.write(connection);
47  if (ok) {
48  ok = body.write(connection);
49  }
50 
51  if (ok) {
52  // if someone connects in text mode,
53  // let them see something readable.
54  connection.convertTextMode();
55  }
56 
57  return ok;
58 }
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
BOTTLE_TAG_LIST
#define BOTTLE_TAG_LIST
Definition: Bottle.h:30
ConnectionWriter.h
yarp::os::PortablePairBase::readPair
static bool readPair(ConnectionReader &connection, Portable &head, Portable &body)
Reads an object pair from a network connection.
Definition: PortablePair.cpp:16
yarp::os::Portable::write
bool write(ConnectionWriter &writer) const override=0
Write this object to a network connection.
PortablePair.h
yarp::os::ConnectionReader::expectInt32
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
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::os::ConnectionWriter::convertTextMode
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
yarp::os::ConnectionWriter::appendInt32
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::os::PortablePairBase::writePair
static bool writePair(ConnectionWriter &connection, const Portable &head, const Portable &body)
Writes an object pair to a network connection.
Definition: PortablePair.cpp:40
yarp::os::Portable::read
bool read(ConnectionReader &reader) override=0
Read this object from a network connection.
ConnectionReader.h