Fast DDS  Version 3.1.0
Fast DDS
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
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
30namespace eprosima {
31namespace fastdds {
32namespace dds {
33
61template<typename T, typename _NonConstEnabler = std::true_type>
62class LoanableSequence : public LoanableTypedCollection<T, _NonConstEnabler>
63{
64public:
65
68
79 LoanableSequence() = default;
80
96 size_type max)
97 {
98 if (max <= 0)
99 {
100 return;
101 }
102
103 resize(max);
104 }
105
114 {
116 {
117 EPROSIMA_LOG_WARNING(SUBSCRIBER, "Sequence destroyed with active loan");
118 return;
119 }
120
121 release();
122 }
123
138 const LoanableSequence& other)
139 {
140 *this = other;
141 }
142
160 const LoanableSequence& other)
161 {
162 if (!has_ownership_)
163 {
164 release();
165 }
166
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
177protected:
178
183
184private:
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 {
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
bool has_ownership_
Definition LoanableCollection.hpp:243
size_type maximum_
Definition LoanableCollection.hpp:240
size_type maximum() const
Get the maximum number of elements currently allocated.
Definition LoanableCollection.hpp:68
int32_t size_type
Definition LoanableCollection.hpp:37
size_type length_
Definition LoanableCollection.hpp:241
element_type * elements_
Definition LoanableCollection.hpp:242
const element_type * buffer() const
Get the pointer to the elements buffer.
Definition LoanableCollection.hpp:48
size_type length() const
Get the number of elements currently accessible.
Definition LoanableCollection.hpp:78
void * element_type
Definition LoanableCollection.hpp:38
A type-safe, ordered collection of elements that can receive the buffer from outside (loan).
Definition LoanableSequence.hpp:63
LoanableSequence & operator=(const LoanableSequence &other)
Copy the contents of another sequence into this one.
Definition LoanableSequence.hpp:159
bool has_ownership_
Definition LoanableCollection.hpp:243
size_type maximum_
Definition LoanableCollection.hpp:240
LoanableSequence()=default
Default constructor.
LoanableSequence(const LoanableSequence &other)
Construct a sequence with the contents of another sequence.
Definition LoanableSequence.hpp:137
size_type length_
Definition LoanableCollection.hpp:241
~LoanableSequence()
Deallocate this sequence's buffer.
Definition LoanableSequence.hpp:113
LoanableCollection::element_type element_type
Definition LoanableSequence.hpp:67
element_type * elements_
Definition LoanableCollection.hpp:242
LoanableSequence(size_type max)
Pre-allocation constructor.
Definition LoanableSequence.hpp:95
LoanableCollection::size_type size_type
Definition LoanableSequence.hpp:66
A type-safe accessible collection of generic opaque pointers that can receive the buffer from outside...
Definition LoanableTypedCollection.hpp:40
Definition DomainParticipant.hpp:45
eProsima namespace.