Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Uninitialized.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_UNINITIALIZED_HXX_
35#define _UTILS_UNINITIALIZED_HXX_
36
37#include <type_traits>
38
48template <class T>
50{
51public:
53 const T *operator->() const
54 {
55 return tptr();
56 }
59 {
60 return tptrm();
61 }
64 {
65 return *tptrm();
66 }
68 const T &operator*() const
69 {
70 return *tptr();
71 }
73 T &value()
74 {
75 return *tptrm();
76 }
78 const T &value() const
79 {
80 return *tptr();
81 };
82
88 constexpr T* get_mutable() const
89 {
90 return tptrm();
91 }
92
98 constexpr const T* get() const
99 {
100 return tptr();
101 }
102
104 template <class... Args> T &emplace(Args &&... args)
105 {
106 new (this) T(std::forward<Args>(args)...);
107 return *tptrm();
108 }
109
111 void reset()
112 {
113 tptr()->~T();
114 }
115
117 static constexpr T *cast_data(uninitialized<T> *parent)
118 {
119 return static_cast<T *>((void*)&parent->data);
120 }
121
122private:
123 typename std::aligned_storage<sizeof(T), alignof(T)>::type data;
124
126 constexpr T *tptrm() const
127 {
128 return static_cast<T *>((void*)&data);
129 }
131 constexpr const T *tptr() const
132 {
133 return static_cast<const T *>((void*)&data);
134 }
135};
136
137#endif // _UTILS_UNINITIALIZED_HXX_
Template class that allows allocating storage for an object but not calling its constructor.
constexpr const T * tptr() const
const T & operator*() const
const T & value() const
const T * operator->() const
void reset()
Destructs the embedded object.
constexpr T * get_mutable() const
Gets the embedded object pointer in a way that is friendly to linker-initialization.
static constexpr T * cast_data(uninitialized< T > *parent)
Public API to convert the pointer in a linker-initialized way.
constexpr T * tptrm() const
constexpr const T * get() const
Gets the embedded object pointer in a way that is friendly to linker-initialization.
T & emplace(Args &&... args)
Constructs the embedded object.