YARP
Yet Another Robot Platform
ManagedBytes.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/ManagedBytes.h>
11 
12 #include <yarp/os/Bottle.h>
15 
16 #include <cstdlib>
17 #include <cstring>
18 
19 using namespace yarp::os;
20 
22  Portable(),
23  b(Bytes(nullptr, 0)),
24  owned(false),
25  use(0),
26  use_set(false)
27 {
28 }
29 
31  Portable(),
32  b(Bytes(new char[len], len)),
33  owned(true),
34  use(0),
35  use_set(false)
36 {
37 }
38 
39 ManagedBytes::ManagedBytes(const Bytes& ext, bool owned) :
40  Portable(),
41  b(ext),
42  owned(owned),
43  use(0),
44  use_set(false)
45 {
46 }
47 
49  Portable(),
50  b(alt.b),
51  owned(false),
52  use(0),
53  use_set(false)
54 {
55  if (alt.owned) {
56  copy();
57  }
58 }
59 
60 void ManagedBytes::moveOwnership(ManagedBytes &other)
61 {
62  b = other.b;
63  owned = other.owned;
64  use = other.use;
65  use_set = other.use_set;
66  other.owned = false;
67  other.clear();
68 }
69 
71 {
72  moveOwnership(other);
73 }
74 
76 {
77  if (&other != this) {
78  clear();
79  moveOwnership(other);
80  }
81  return *this;
82 }
83 
85 {
86  if (&alt != this) {
87  clear();
88  b = alt.b;
89  use = alt.use;
90  use_set = alt.use_set;
91  owned = false;
92  if (alt.owned) {
93  copy();
94  }
95  }
96  return *this;
97 }
98 
100 {
101  clear();
102 }
103 
104 
105 void ManagedBytes::allocate(size_t len)
106 {
107  clear();
108  char* buf = new char[len];
109  b = Bytes(buf, len);
110  owned = true;
111  use = 0;
112  use_set = false;
113 }
114 
115 bool ManagedBytes::allocateOnNeed(size_t neededLen, size_t allocateLen)
116 {
117  if (length() < neededLen && allocateLen >= neededLen) {
118  char* buf = new char[allocateLen];
119  yarp::os::NetworkBase::assertion(buf != nullptr);
120  memcpy(buf, get(), length());
121  if (owned) {
122  delete[] get();
123  owned = false;
124  }
125  b = Bytes(buf, allocateLen);
126  owned = true;
127  return true;
128  }
129  return false;
130 }
131 
133 {
134  if (!owned) {
135  yarp::conf::ssize_t len = length();
136  char* buf = new char[len];
137  yarp::os::NetworkBase::assertion(buf != nullptr);
138  memcpy(buf, get(), len);
139  b = Bytes(buf, len);
140  owned = true;
141  }
142 }
143 
144 size_t ManagedBytes::length() const
145 {
146  return b.length();
147 }
148 
149 size_t ManagedBytes::used() const
150 {
151  return use_set ? use : length();
152 }
153 
154 const char* ManagedBytes::get() const
155 {
156  return b.get();
157 }
158 
160 {
161  return b.get();
162 }
163 
165 {
166  if (owned) {
167  if (get() != nullptr) {
168  delete[] get();
169  }
170  owned = false;
171  }
172  b = Bytes(nullptr, 0);
173  use = 0;
174  use_set = false;
175 }
176 
178 {
179  return b;
180 }
181 
183 {
184  return b;
185 }
186 
188 {
189  return {get(), used()};
190 }
191 
192 size_t ManagedBytes::setUsed(size_t used)
193 {
194  use_set = true;
195  use = used;
196  return this->used();
197 }
198 
200 {
201  use = 0;
202  use_set = false;
203  return this->used();
204 }
205 
206 
208 {
209  reader.convertTextMode();
210  std::int32_t listTag;
211  std::int32_t listLen;
212  std::int32_t blobLen;
213  listTag = reader.expectInt32();
214  listLen = reader.expectInt32();
215  blobLen = reader.expectInt32();
216  if (listTag != BOTTLE_TAG_LIST + BOTTLE_TAG_BLOB) {
217  return false;
218  }
219  if (listLen != 1) {
220  return false;
221  }
222  allocate(blobLen);
223  if (get() == nullptr) {
224  return false;
225  }
226  return reader.expectBlock(get(), length());
227 }
228 
230 {
232  writer.appendInt32(1);
233  writer.appendInt32(static_cast<std::int32_t>(length()));
234  writer.appendExternalBlock(get(), length());
235  writer.convertTextMode();
236  return !writer.isError();
237 }
yarp::os::ManagedBytes::usedBytes
Bytes usedBytes()
Definition: ManagedBytes.cpp:187
yarp::os::ManagedBytes::copy
void copy()
Makes sure data block is owned, making a copy if necessary.
Definition: ManagedBytes.cpp:132
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
yarp::os::ManagedBytes::operator=
ManagedBytes & operator=(ManagedBytes &&other) noexcept
Move assignment operator.
Definition: ManagedBytes.cpp:75
yarp::os::ManagedBytes::read
bool read(ConnectionReader &reader) override
Read this object from a network connection.
Definition: ManagedBytes.cpp:207
ConnectionWriter.h
yarp::os::ManagedBytes::setUsed
size_t setUsed(size_t used)
explicitly declare how many of the bytes are in use.
Definition: ManagedBytes.cpp:192
yarp::os::NetworkBase::assertion
static void assertion(bool shouldBeTrue)
An assertion.
Definition: Network.cpp:1063
yarp::os::ManagedBytes::clear
void clear()
Disassociate object with any data block (deleting block if appropriate).
Definition: ManagedBytes.cpp:164
yarp::os::ManagedBytes::ManagedBytes
ManagedBytes()
Constructor.
Definition: ManagedBytes.cpp:21
yarp::os::ManagedBytes::write
bool write(ConnectionWriter &writer) const override
Write this object to a network connection.
Definition: ManagedBytes.cpp:229
yarp::os::ManagedBytes::allocateOnNeed
bool allocateOnNeed(size_t neededLen, size_t allocateLen)
Definition: ManagedBytes.cpp:115
yarp::os::ManagedBytes::bytes
const Bytes & bytes() const
Definition: ManagedBytes.cpp:177
yarp::os::ConnectionWriter::isError
virtual bool isError() const =0
yarp::os::ConnectionReader::expectInt32
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
ManagedBytes.h
yarp::os::ManagedBytes
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:25
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::os::Bytes::get
const char * get() const
Definition: Bytes.cpp:30
yarp::os::Bytes::length
size_t length() const
Definition: Bytes.cpp:25
yarp::os::ManagedBytes::resetUsed
size_t resetUsed()
Definition: ManagedBytes.cpp:199
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
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::ManagedBytes::used
size_t used() const
Definition: ManagedBytes.cpp:149
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::os::ManagedBytes::allocate
void allocate(size_t len)
Makes a data block of the specified length that will be deleted if this object is destroyed.
Definition: ManagedBytes.cpp:105
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
BOTTLE_TAG_BLOB
#define BOTTLE_TAG_BLOB
Definition: Bottle.h:29
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::ManagedBytes::~ManagedBytes
virtual ~ManagedBytes()
Destructor.
Definition: ManagedBytes.cpp:99
yarp::os::ManagedBytes::get
const char * get() const
Definition: ManagedBytes.cpp:154
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::ManagedBytes::length
size_t length() const
Definition: ManagedBytes.cpp:144
yarp::os::ConnectionReader::expectBlock
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
Bottle.h
ConnectionReader.h