iceoryx_doc  1.0.1
poor_mans_heap.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
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 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_UTILS_CXX_POOR_MANS_HEAP_HPP
17 #define IOX_UTILS_CXX_POOR_MANS_HEAP_HPP
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <utility>
22 
23 namespace iox
24 {
25 namespace cxx
26 {
28 template <typename Type>
30 {
31 };
32 
112 template <typename Interface, size_t TypeSize, size_t TypeAlignment = 8>
114 {
115  public:
116  PoorMansHeap() = default;
117  ~PoorMansHeap() noexcept;
118 
122  template <typename Type, typename... CTorArgs>
123  PoorMansHeap(PoorMansHeapType<Type>, CTorArgs&&... ctorArgs) noexcept;
124 
125  PoorMansHeap(PoorMansHeap&& other) = delete;
126  PoorMansHeap& operator=(PoorMansHeap&& rhs) = delete;
127 
128  PoorMansHeap(const PoorMansHeap&) = delete;
129  PoorMansHeap& operator=(const PoorMansHeap&) = delete;
130 
134  template <typename Type, typename... CTorArgs>
135  void newInstance(CTorArgs&&... ctorArgs) noexcept;
136 
138  void deleteInstance() noexcept;
139 
142  bool hasInstance() const noexcept;
143 
146  Interface* operator->() const noexcept;
147 
150  Interface& operator*() const noexcept;
151 
152  private:
153  Interface* m_instance{nullptr};
154  alignas(TypeAlignment) uint8_t m_heap[TypeSize];
155 };
156 
157 } // namespace cxx
158 } // namespace iox
159 
160 #include "iceoryx_utils/internal/cxx/poor_mans_heap.inl"
161 
162 #endif // IOX_UTILS_CXX_POOR_MANS_HEAP_HPP
This is a proxy which must be used for the non default PoorMansHeap ctor.
Definition: poor_mans_heap.hpp:30
Reserves space on stack for placement new instatiation.
Definition: poor_mans_heap.hpp:114
Interface & operator*() const noexcept
Definition: poor_mans_heap.inl:77
void deleteInstance() noexcept
Calls the destructor if there is a valid instance, otherwise nothing happens.
Definition: poor_mans_heap.inl:55
Interface * operator->() const noexcept
Definition: poor_mans_heap.inl:71
bool hasInstance() const noexcept
Definition: poor_mans_heap.inl:65
void newInstance(CTorArgs &&... ctorArgs) noexcept
Definition: poor_mans_heap.inl:44
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28