Fast DDS  Version 3.6.1.0
Fast DDS
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 
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 
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 
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(
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 
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 
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 
243  bool has_ownership_ = true;
244 };
245 
246 } // namespace dds
247 } // namespace fastdds
248 } // namespace eprosima
249 
250 #endif // FASTDDS_DDS_CORE__LOANABLECOLLECTION_HPP
A collection of generic opaque pointers that can receive the buffer from outside (loan).
Definition: LoanableCollection.hpp:34
virtual void resize(size_type new_length)=0
bool has_ownership_
Definition: LoanableCollection.hpp:243
LoanableCollection()=default
Default constructor.
size_type maximum_
Definition: LoanableCollection.hpp:240
element_type * unloan(size_type &maximum, size_type &length)
Remove the loan from the collection.
Definition: LoanableCollection.hpp:183
const element_type * buffer() const
Get the pointer to the elements buffer.
Definition: LoanableCollection.hpp:48
size_type maximum() const
Get the maximum number of elements currently allocated.
Definition: LoanableCollection.hpp:68
bool length(size_type new_length)
Set the number of elements currently accessible.
Definition: LoanableCollection.hpp:101
int32_t size_type
Definition: LoanableCollection.hpp:37
size_type length_
Definition: LoanableCollection.hpp:241
bool loan(element_type *buffer, size_type new_maximum, size_type new_length)
Loan a buffer to the collection.
Definition: LoanableCollection.hpp:145
element_type * elements_
Definition: LoanableCollection.hpp:242
size_type length() const
Get the number of elements currently accessible.
Definition: LoanableCollection.hpp:78
void * element_type
Definition: LoanableCollection.hpp:38
element_type * unloan()
Remove the loan from the collection.
Definition: LoanableCollection.hpp:217
bool has_ownership() const
Get the ownership flag.
Definition: LoanableCollection.hpp:58
Definition: DomainParticipant.hpp:46