ADTF
Loading...
Searching...
No Matches
stack_ptr_decl.h
Go to the documentation of this file.
1
14
15#ifndef A_UTIL_UTIL_MEMORY_DETAIL_STACK_PTR_DECL_HEADER_INCLUDED
16#define A_UTIL_UTIL_MEMORY_DETAIL_STACK_PTR_DECL_HEADER_INCLUDED
17
18#include <array>
19
20namespace a_util {
21namespace memory {
29template <typename T,
30 std::size_t StackSize = sizeof(T),
31 std::size_t Alignment = alignof(std::size_t)>
32class alignas(Alignment) StackPtr {
33public:
35 StackPtr();
36
38 StackPtr(std::nullptr_t);
39
44 StackPtr(const T& data);
45
50 StackPtr(T&& data);
51
53 ~StackPtr();
54
60 StackPtr(const StackPtr& other);
61
69
74 StackPtr(StackPtr&& other);
75
81 T* operator->();
82
88 const T* operator->() const;
89
95 T& operator*();
96
102 const T& operator*() const;
103
108 explicit operator bool() const noexcept;
109
111 void reset();
112
118 template <typename... Args>
119 void reset(Args&&... args);
120
127 void swap(StackPtr& other);
128
129private: // types and constants
131 enum StackPtrIntrospection {
133 NumFlagBytes = 1,
135 OverheadSize = (Alignment - (StackSize % Alignment)),
137 StorageSize = StackSize + OverheadSize,
139 FirstFlagPosition = StackSize
140 };
141
143 enum StackPtrFlagPositions {
144 InitializeStatus = FirstFlagPosition
145 };
146
148 enum class StackPtrFlags : char {
149 IsDestroyed = 0x00,
150 IsConstructed = 0x01
151 };
152
153private:
158 void* address();
159
164 const void* address() const;
165
171 void setFlag(StackPtrFlagPositions position, StackPtrFlags flag);
172
177 bool isConstructed() const noexcept;
178
188 template <typename U, std::size_t SSize, std::size_t A>
189 friend bool operator==(const StackPtr<U, SSize, A>& lhs, const StackPtr<U, SSize, A>& rhs);
190
191private:
193 std::array<char, StorageSize> _storage;
194};
195
206template <typename T, std::size_t StackSize, std::size_t Alignment>
208
218template <typename T, std::size_t StackSize, std::size_t Alignment>
221
232template <typename T,
233 std::size_t StackSize = sizeof(T),
234 std::size_t Alignment = alignof(std::size_t),
235 typename... Args>
236constexpr auto makeStackPtr(Args&&... args) -> StackPtr<T, StackSize, Alignment>;
237
238} // namespace memory
239} // namespace a_util
240
241#endif // A_UTIL_UTIL_MEMORY_DETAIL_STACK_PTR_DECL_HEADER_INCLUDED
Definition stack_ptr_decl.h:32
void reset()
Unitializes the storage and, if constructed, destructs the storaged object.
Definition stack_ptr_impl.h:111
StackPtr & operator=(StackPtr other)
Definition stack_ptr_impl.h:65
StackPtr()
Initializes the storage and constructs object ot type T by calling its default ctor.
Definition stack_ptr_impl.h:25
T * operator->()
Definition stack_ptr_impl.h:81
void swap(StackPtr &other)
Definition stack_ptr_impl.h:129
friend bool operator==(const StackPtr< U, SSize, A > &lhs, const StackPtr< U, SSize, A > &rhs)
T & operator*()
Definition stack_ptr_impl.h:93
Serves as component for memory access and management.
Definition memory.h:20
bool operator!=(const StackPtr< T, StackSize, Alignment > &lhs, const StackPtr< T, StackSize, Alignment > &rhs)
Definition stack_ptr_impl.h:223
void swap(StackPtr< T, StackSize, Alignment > &lhs, StackPtr< T, StackSize, Alignment > &rhs)
Definition stack_ptr_impl.h:191
constexpr auto makeStackPtr(Args &&... args) -> StackPtr< T, StackSize, Alignment >
Definition stack_ptr_impl.h:230
Serves as the root component, with common functionality documented in core functionality.
Definition base.h:24