ADTF  3.18.2
stack_ptr_decl.h
Go to the documentation of this file.
1 
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 
20 namespace a_util {
21 namespace memory {
29 template <typename T,
30  std::size_t StackSize = sizeof(T),
31  std::size_t Alignment = alignof(std::size_t)>
32 class alignas(Alignment) StackPtr {
33 public:
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 
68  StackPtr& operator=(StackPtr other);
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 
129 private: // types and constants
133  NumFlagBytes = 1,
135  OverheadSize = (Alignment - (StackSize % Alignment)),
137  StorageSize = StackSize + OverheadSize,
139  FirstFlagPosition = StackSize
140  };
141 
144  InitializeStatus = FirstFlagPosition
145  };
146 
148  enum class StackPtrFlags : char {
149  IsDestroyed = 0x00,
150  IsConstructed = 0x01
151  };
152 
153 private:
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 
191 private:
193  std::array<char, StorageSize> _storage;
194 };
195 
206 template <typename T, std::size_t StackSize, std::size_t Alignment>
207 void swap(StackPtr<T, StackSize, Alignment>& lhs, StackPtr<T, StackSize, Alignment>& rhs);
208 
218 template <typename T, std::size_t StackSize, std::size_t Alignment>
219 bool operator!=(const StackPtr<T, StackSize, Alignment>& lhs,
220  const StackPtr<T, StackSize, Alignment>& rhs);
221 
232 template <typename T,
233  std::size_t StackSize = sizeof(T),
234  std::size_t Alignment = alignof(std::size_t),
235  typename... Args>
236 constexpr 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
A smart pointer allocating its memory only on the stack.
StackPtrFlags
Flags for StackPtrFlagPositions.
StackPtrIntrospection
Calculate entire size of the _storage - adds padding bytes to comply to the alignment.
StackPtrFlagPositions
Absolute bit positions of the flags inside the overhead area of the storage.
void swap(StackPtr< T, StackSize, Alignment > &lhs, StackPtr< T, StackSize, Alignment > &rhs)
Swap storages of two objects of type StackPtr.
constexpr auto makeStackPtr(Args &&... args) -> StackPtr< T, StackSize, Alignment >
Create a new StackPtr.
Serves as the root component, with common functionality documented in core functionality.
Definition: base.h:24
A(8) B(8) G(8) R(8)") ADTF_DEFINE_STREAM_IMAGE_FORMAT(ARGB_32
Format definition this description means always first in lowest bit of lowest byte.
tResult reset(T &oCodec, const A_UTILS_NS::cString &strElementName)
Set the value of the requested element to zero.
Alignment
Alignment defintion.