iceoryx_hoofs  2.0.2
poor_mans_heap.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_HOOFS_CXX_POOR_MANS_HEAP_HPP
18 #define IOX_HOOFS_CXX_POOR_MANS_HEAP_HPP
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <utility>
23 
24 namespace iox
25 {
26 namespace cxx
27 {
29 template <typename Type>
31 {
32 };
33 
113 template <typename Interface, size_t TypeSize, size_t TypeAlignment = 8>
115 {
116  public:
117  PoorMansHeap() = default;
118  ~PoorMansHeap() noexcept;
119 
123  template <typename Type, typename... CTorArgs>
124  PoorMansHeap(PoorMansHeapType<Type>, CTorArgs&&... ctorArgs) noexcept;
125 
126  PoorMansHeap(PoorMansHeap&& other) = delete;
127  PoorMansHeap& operator=(PoorMansHeap&& rhs) = delete;
128 
129  PoorMansHeap(const PoorMansHeap&) = delete;
130  PoorMansHeap& operator=(const PoorMansHeap&) = delete;
131 
135  template <typename Type, typename... CTorArgs>
136  void newInstance(CTorArgs&&... ctorArgs) noexcept;
137 
139  void deleteInstance() noexcept;
140 
143  bool hasInstance() const noexcept;
144 
147  Interface* operator->() const noexcept;
148 
151  Interface& operator*() const noexcept;
152 
153  private:
154  Interface* m_instance{nullptr};
155  alignas(TypeAlignment) uint8_t m_heap[TypeSize];
156 };
157 
158 } // namespace cxx
159 } // namespace iox
160 
161 #include "iceoryx_hoofs/internal/cxx/poor_mans_heap.inl"
162 
163 #endif // IOX_HOOFS_CXX_POOR_MANS_HEAP_HPP
This is a proxy which must be used for the non default PoorMansHeap ctor.
Definition: poor_mans_heap.hpp:31
Reserves space on stack for placement new instatiation.
Definition: poor_mans_heap.hpp:115
PoorMansHeap(PoorMansHeapType< Type >, CTorArgs &&... ctorArgs) noexcept
void deleteInstance() noexcept
Calls the destructor if there is a valid instance, otherwise nothing happens.
bool hasInstance() const noexcept
void newInstance(CTorArgs &&... ctorArgs) noexcept
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29