| Fast DDS Version 3.3.0 Fast DDS

Loading...
Searching...
No Matches
LoanableCollection.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__LOANABLECOLLECTION_HPP
20#define FASTDDS_DDS_CORE__LOANABLECOLLECTION_HPP
21
22#include <cstdint>
23
24 namespace eprosima {
25 namespace fastdds {
26 namespace dds {
27
33 class LoanableCollection
34{
35 public:
36
37 using size_type = int32_t;
38 using element_type = void*;
39
48 const element_type* buffer() const
49 {
50 return elements_;
51 }
52
58 bool has_ownership() const
59 {
60 return has_ownership_;
61 }
62
68 size_type maximum() const
69 {
70 return maximum_;
71 }
72
78 size_type length() const
79 {
80 return length_;
81 }
82
101 bool length(
102 size_type new_length)
103 {
104 if (new_length < 0)
105 {
106 return false;
107 }
108
109 if (new_length <= maximum_)
110 {
111 length_ = new_length;
112 return true;
113 }
114
115 if (!has_ownership_)
116 {
117 return false;
118 }
119
120 resize(new_length);
121 length_ = new_length;
122 return true;
123 }
124
145 bool loan(
146 element_type* buffer,
147 size_type new_maximum,
148 size_type new_length)
149 {
150 if (has_ownership_ && maximum_ > 0)
151 {
152 return false;
153 }
154
155 if ((nullptr == buffer) || (new_maximum < new_length) || (new_maximum < 1))
156 {
157 return false;
158 }
159
160 maximum_ = new_maximum;
161 length_ = new_length;
162 elements_ = buffer;
163 has_ownership_ = false;
164 return true;
165 }
166
183 element_type* unloan(
184 size_type& maximum,
185 size_type& length)
186 {
187 if (has_ownership_)
188 {
189 return nullptr;
190 }
191
192 element_type* ret_val = elements_;
193 maximum = maximum_;
194 length = length_;
195
196 maximum_ = 0;
197 length_ = 0;
198 elements_ = nullptr;
199 has_ownership_ = true;
200
201 return ret_val;
202 }
203
217 element_type* unloan()
218 {
219 size_type max, len;
220 return unloan(max, len);
221 }
222
223 protected:
224
235 LoanableCollection() = default;
236
237 virtual void resize(
238 size_type new_length) = 0;
239
240 size_type maximum_ = 0;
241 size_type length_ = 0;
242 element_type* elements_ = nullptr;
243 bool has_ownership_ = true;
244};
245
246} // namespace dds
247} // namespace fastdds
248} // namespace eprosima
249
250#endif // FASTDDS_DDS_CORE__LOANABLECOLLECTION_HPP
eprosima::fastdds::dds::LoanableCollection::resize
virtual void resize(size_type new_length)=0
eprosima::fastdds::dds::LoanableCollection::has_ownership_
bool has_ownership_
Definition LoanableCollection.hpp:243
eprosima::fastdds::dds::LoanableCollection::LoanableCollection
LoanableCollection()=default
Default constructor.
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::length
bool length(size_type new_length)
Set the number of elements currently accessible.
Definition LoanableCollection.hpp:101
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::unloan
element_type * unloan()
Remove the loan from the collection.
Definition LoanableCollection.hpp:217
eprosima::fastdds::dds::LoanableCollection::loan
bool loan(element_type *buffer, size_type new_maximum, size_type new_length)
Loan a buffer to the collection.
Definition LoanableCollection.hpp:145
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::LoanableCollection::has_ownership
bool has_ownership() const
Get the ownership flag.
Definition LoanableCollection.hpp:58
eprosima::fastdds::dds::LoanableCollection::unloan
element_type * unloan(size_type &maximum, size_type &length)
Remove the loan from the collection.
Definition LoanableCollection.hpp:183
eprosima::fastdds::dds
Definition BuiltinTopicKey.hpp:26
eprosima::fastdds
eprosima
eProsima namespace.