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