Fast DDS
Version 3.6.1.0
Fast DDS
Toggle main menu visibility
Loading...
Searching...
No Matches
LoanableSequence.hpp
1
// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
18
19
#ifndef FASTDDS_DDS_CORE__LOANABLESEQUENCE_HPP
20
#define FASTDDS_DDS_CORE__LOANABLESEQUENCE_HPP
21
22
#include <cassert>
23
#include <cstdint>
24
#include <vector>
25
#include <type_traits>
26
27
#include <fastdds/dds/core/LoanableTypedCollection.hpp>
28
#include <fastdds/dds/log/Log.hpp>
29
30
namespace
eprosima
{
31
namespace
fastdds
{
32
namespace
dds
{
33
61
template
<
typename
T,
typename
_NonConstEnabler = std::true_type>
62
class
LoanableSequence
:
public
LoanableTypedCollection
<T, _NonConstEnabler>
63
{
64
public
:
65
66
using
size_type
=
LoanableCollection::size_type
;
67
using
element_type
=
LoanableCollection::element_type
;
68
79
LoanableSequence
() =
default
;
80
95
LoanableSequence
(
96
size_type
max)
97
{
98
if
(max <= 0)
99
{
100
return
;
101
}
102
103
resize(max);
104
}
105
113
~LoanableSequence
()
114
{
115
if
(
elements_
&& !
has_ownership_
)
116
{
117
EPROSIMA_LOG_WARNING(SUBSCRIBER,
"Sequence destroyed with active loan"
);
118
return
;
119
}
120
121
release();
122
}
123
137
LoanableSequence
(
138
const
LoanableSequence
& other)
139
{
140
*
this
= other;
141
}
142
159
LoanableSequence
&
operator =
(
160
const
LoanableSequence
& other)
161
{
162
if
(!
has_ownership_
)
163
{
164
release();
165
}
166
167
LoanableCollection::length
(other.
length
());
168
const
element_type
* other_buf = other.
buffer
();
169
for
(
size_type
n = 0; n <
length_
; ++n)
170
{
171
*
static_cast<
T*
>
(
elements_
[n]) = *
static_cast<
const
T*
>
(other_buf[n]);
172
}
173
174
return
*
this
;
175
}
176
177
protected
:
178
179
using
LoanableCollection::maximum_
;
180
using
LoanableCollection::length_
;
181
using
LoanableCollection::elements_
;
182
using
LoanableCollection::has_ownership_
;
183
184
private
:
185
186
void
resize(
187
size_type
maximum
)
override
188
{
189
assert(
has_ownership_
);
190
191
// Resize collection and get new pointer
192
data_.reserve(
maximum
);
193
data_.resize(
maximum
);
194
elements_
=
reinterpret_cast<
element_type
*
>
(data_.data());
195
196
// Allocate individual elements
197
while
(
maximum_
<
maximum
)
198
{
199
data_[
maximum_
++] =
new
T();
200
}
201
}
202
203
void
release()
204
{
205
if
(
has_ownership_
&&
elements_
)
206
{
207
for
(
size_type
n = 0; n <
maximum_
; ++n)
208
{
209
T* elem = data_[n];
210
delete
elem;
211
}
212
std::vector<T*>().swap(data_);
213
}
214
215
maximum_
= 0u;
216
length_
= 0u;
217
elements_
=
nullptr
;
218
has_ownership_
=
true
;
219
}
220
221
std::vector<T*> data_;
222
};
223
224
}
// namespace dds
225
}
// namespace fastdds
226
}
// namespace eprosima
227
228
// Macro to easily declare a LoanableSequence for a data type
229
#define FASTDDS_SEQUENCE(FooSeq, Foo) using FooSeq = eprosima::fastdds::dds::LoanableSequence<Foo>
230
#define FASTDDS_CONST_SEQUENCE(FooSeq, Foo) using FooSeq = eprosima::fastdds::dds::LoanableSequence<Foo, \
231
std::false_type>
232
233
#endif
// FASTDDS_DDS_CORE__LOANABLESEQUENCE_HPP
eprosima::fastdds::dds::LoanableCollection::has_ownership_
bool has_ownership_
Definition
LoanableCollection.hpp:243
eprosima::fastdds::dds::LoanableCollection::maximum_
size_type maximum_
Definition
LoanableCollection.hpp:240
eprosima::fastdds::dds::LoanableCollection::maximum
size_type maximum() const
Get the maximum number of elements currently allocated.
Definition
LoanableCollection.hpp:68
eprosima::fastdds::dds::LoanableCollection::size_type
int32_t size_type
Definition
LoanableCollection.hpp:37
eprosima::fastdds::dds::LoanableCollection::length_
size_type length_
Definition
LoanableCollection.hpp:241
eprosima::fastdds::dds::LoanableCollection::elements_
element_type * elements_
Definition
LoanableCollection.hpp:242
eprosima::fastdds::dds::LoanableCollection::buffer
const element_type * buffer() const
Get the pointer to the elements buffer.
Definition
LoanableCollection.hpp:48
eprosima::fastdds::dds::LoanableCollection::length
size_type length() const
Get the number of elements currently accessible.
Definition
LoanableCollection.hpp:78
eprosima::fastdds::dds::LoanableCollection::element_type
void * element_type
Definition
LoanableCollection.hpp:38
eprosima::fastdds::dds::LoanableSequence::operator=
LoanableSequence & operator=(const LoanableSequence &other)
Copy the contents of another sequence into this one.
Definition
LoanableSequence.hpp:159
eprosima::fastdds::dds::LoanableSequence< SampleInfo >::has_ownership_
bool has_ownership_
eprosima::fastdds::dds::LoanableSequence::maximum_
size_type maximum_
Definition
LoanableCollection.hpp:240
eprosima::fastdds::dds::LoanableSequence::LoanableSequence
LoanableSequence()=default
Default constructor.
eprosima::fastdds::dds::LoanableSequence::LoanableSequence
LoanableSequence(const LoanableSequence &other)
Construct a sequence with the contents of another sequence.
Definition
LoanableSequence.hpp:137
eprosima::fastdds::dds::LoanableSequence< SampleInfo >::length_
size_type length_
eprosima::fastdds::dds::LoanableSequence::~LoanableSequence
~LoanableSequence()
Deallocate this sequence's buffer.
Definition
LoanableSequence.hpp:113
eprosima::fastdds::dds::LoanableSequence::element_type
LoanableCollection::element_type element_type
Definition
LoanableSequence.hpp:67
eprosima::fastdds::dds::LoanableSequence< SampleInfo >::elements_
element_type * elements_
eprosima::fastdds::dds::LoanableSequence::LoanableSequence
LoanableSequence(size_type max)
Pre-allocation constructor.
Definition
LoanableSequence.hpp:95
eprosima::fastdds::dds::LoanableSequence::size_type
LoanableCollection::size_type size_type
Definition
LoanableSequence.hpp:66
eprosima::fastdds::dds::LoanableTypedCollection
A type-safe accessible collection of generic opaque pointers that can receive the buffer from outside...
Definition
LoanableTypedCollection.hpp:40
eprosima::fastdds::dds
Definition
BuiltinTopicKey.hpp:26
eprosima::fastdds
eprosima
eProsima namespace.
include
fastdds
dds
core
LoanableSequence.hpp
Generated on
for