Fast DDS  Version 3.6.1.0
Fast DDS
StackAllocatedSequence.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__STACKALLOCATEDSEQUENCE_HPP
20 #define FASTDDS_DDS_CORE__STACKALLOCATEDSEQUENCE_HPP
21 
22 #include <array>
23 #include <cassert>
24 #include <cstdint>
25 #include <stdexcept>
26 
27 #include <fastdds/dds/core/LoanableArray.hpp>
28 #include <fastdds/dds/core/LoanableTypedCollection.hpp>
29 
30 namespace eprosima {
31 namespace fastdds {
32 namespace dds {
33 
37 template<typename T, LoanableCollection::size_type num_items>
39 {
42 
44  {
45  has_ownership_ = true;
46  maximum_ = num_items;
47  length_ = 0;
48  elements_ = data_.buffer_for_loans();
49  }
50 
52 
53  // Non-copyable
55  const StackAllocatedSequence&) = delete;
57  const StackAllocatedSequence&) = delete;
58 
59  // Non-moveable
61  StackAllocatedSequence&&) = delete;
63  StackAllocatedSequence&&) = delete;
64 
65 protected:
66 
71 
72  void resize(
73  LoanableCollection::size_type new_length) override
74  {
75  // This kind of collection cannot grow above its stack-allocated size
76  if (new_length > num_items)
77  {
78  throw std::bad_alloc();
79  }
80  }
81 
82 private:
83 
85 };
86 
87 } // namespace dds
88 } // namespace fastdds
89 } // namespace eprosima
90 
91 #endif // FASTDDS_DDS_CORE__STACKALLOCATEDSEQUENCE_HPP
bool has_ownership_
Definition: LoanableCollection.hpp:243
size_type maximum_
Definition: LoanableCollection.hpp:240
int32_t size_type
Definition: LoanableCollection.hpp:37
size_type length_
Definition: LoanableCollection.hpp:241
element_type * elements_
Definition: LoanableCollection.hpp:242
void * element_type
Definition: LoanableCollection.hpp:38
A type-safe accessible collection of generic opaque pointers that can receive the buffer from outside...
Definition: LoanableTypedCollection.hpp:40
Definition: DomainParticipant.hpp:46
A type-safe, ordered collection of elements allocated on the stack, which can be loaned to a Loanable...
Definition: LoanableArray.hpp:35
A type-safe, ordered collection of elements allocated on the stack.
Definition: StackAllocatedSequence.hpp:39
bool has_ownership_
Definition: LoanableCollection.hpp:243
StackAllocatedSequence & operator=(const StackAllocatedSequence &)=delete
size_type maximum_
Definition: LoanableCollection.hpp:240
size_type length_
Definition: LoanableCollection.hpp:241
StackAllocatedSequence(const StackAllocatedSequence &)=delete
StackAllocatedSequence()
Definition: StackAllocatedSequence.hpp:43
StackAllocatedSequence(StackAllocatedSequence &&)=delete
element_type * elements_
Definition: LoanableCollection.hpp:242
void resize(LoanableCollection::size_type new_length) override
Definition: StackAllocatedSequence.hpp:72