Fast RTPS  Version 2.14.3
Fast RTPS
Loading...
Searching...
No Matches
DynamicLoanableSequence.hpp
1// Copyright 2024 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
15#ifndef _TYPES_DYNAMIC_LOANABLE_SEQUENCE_HPP_
16#define _TYPES_DYNAMIC_LOANABLE_SEQUENCE_HPP_
17
18#include <cassert>
19#include <memory>
20
21#include <fastdds/dds/core/LoanableSequence.hpp>
22#include <fastdds/dds/log/Log.hpp>
23#include <fastrtps/types/DynamicData.h>
24#include <fastrtps/types/DynamicPubSubType.h>
25#include <fastrtps/types/DynamicTypePtr.h>
26
27namespace eprosima {
28namespace fastdds {
29namespace dds {
30
39template<typename _NonConstEnabler>
40class LoanableSequence<fastrtps::types::DynamicData, _NonConstEnabler>
41 : public LoanableTypedCollection<fastrtps::types::DynamicData, _NonConstEnabler>
42{
43public:
44
46 using size_type = LoanableCollection::size_type;
47
49 using element_type = LoanableCollection::element_type;
50
58 : dynamic_type_support_(new fastrtps::types::DynamicPubSubType(dyn_type))
59 {
60 }
61
68 size_type max)
69 {
70 if (max <= 0)
71 {
72 return;
73 }
74
75 resize(max);
76 }
77
82 {
83 if (elements_ && !has_ownership_)
84 {
85 EPROSIMA_LOG_WARNING(SUBSCRIBER, "Sequence destroyed with active loan");
86 return;
87 }
88
89 release();
90 }
91
94 const LoanableSequence&) = delete;
95
97 LoanableSequence& operator =(
98 const LoanableSequence&) = delete;
99
102 LoanableSequence&&) = default;
103
105 LoanableSequence& operator =(
106 LoanableSequence&&) = default;
107
108protected:
109
110 using LoanableCollection::maximum_;
111 using LoanableCollection::length_;
112 using LoanableCollection::elements_;
113 using LoanableCollection::has_ownership_;
114
115private:
116
122 void resize(
123 size_type maximum) override
124 {
125 assert(has_ownership_);
126
127 // Resize collection and get new pointer
128 data_.reserve(maximum);
129 data_.resize(maximum);
130 elements_ = reinterpret_cast<element_type*>(data_.data());
131
132 // Allocate individual elements
133 while (maximum_ < maximum)
134 {
135 data_[maximum_++] = static_cast<fastrtps::types::DynamicData*>(dynamic_type_support_->createData());
136 }
137 }
138
142 void release()
143 {
144 if (has_ownership_ && elements_)
145 {
146 for (size_type n = 0; n < maximum_; ++n)
147 {
148 fastrtps::types::DynamicData* elem = data_[n];
149 dynamic_type_support_->deleteData(elem);
150 }
151 std::vector<fastrtps::types::DynamicData*>().swap(data_);
152 }
153
154 maximum_ = 0u;
155 length_ = 0u;
156 elements_ = nullptr;
157 has_ownership_ = true;
158 }
159
161 std::vector<fastrtps::types::DynamicData*> data_;
162
164 std::unique_ptr<fastrtps::types::DynamicPubSubType> dynamic_type_support_;
165};
166
168using DynamicLoanableSequence = LoanableSequence<fastrtps::types::DynamicData, std::true_type>;
169
170} // namespace dds
171} // namespace fastdds
172} // namespace eprosima
173
174#endif // _TYPES_DYNAMIC_LOANABLE_SEQUENCE_HPP_
LoanableSequence(fastrtps::types::DynamicType_ptr dyn_type)
Construct a LoanableSequence with a specified dynamic type.
Definition DynamicLoanableSequence.hpp:56
LoanableSequence(LoanableSequence &&)=default
Move constructor for LoanableSequence.
~LoanableSequence()
Destructor for LoanableSequence.
Definition DynamicLoanableSequence.hpp:81
LoanableCollection::element_type element_type
Type for the elements in the sequence.
Definition DynamicLoanableSequence.hpp:49
LoanableSequence(size_type max)
Construct a LoanableSequence with a specified maximum size.
Definition DynamicLoanableSequence.hpp:67
LoanableCollection::size_type size_type
Type for the size of the sequence.
Definition DynamicLoanableSequence.hpp:46
LoanableSequence(const LoanableSequence &)=delete
Deleted copy constructor for LoanableSequence.
Definition DynamicData.h:44
Definition DynamicTypePtr.h:27
LoanableSequence< fastrtps::types::DynamicData, std::true_type > DynamicLoanableSequence
Alias for LoanableSequence with DynamicData and true_type.
Definition DynamicLoanableSequence.hpp:168
eProsima namespace.
Definition LibrarySettingsAttributes.h:23