ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
lang/placeholder.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_lang of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib::lang {
9
10//==================================================================================================
11/// Templated helper that reserves memory of size and alignment corresponding to type \p{T}.
12/// Along with that, construction, destruction and reinterpretation casts are given in
13/// various ways.
14///
15/// The type is used for class members or (less frequently for local variables), whose construction
16/// has to be deferred.
17/// @tparam T The type to replace.
18//==================================================================================================
19template<typename T>
20struct alignas(alignof(T)) Placeholder
21{
22 /// the placeholder space.
23 char bytes[sizeof(T)];
24
25 /// Constructor. Initializes the occupied memory with 0-bytes.
27
28 /// Constructs the custom type \p{T} represented by this placeholder.
29 /// @tparam TArgs The argument types used for constructing \p{T}.
30 /// @param args The arguments to construct the instance of \p{T}.
31 template<typename... TArgs>
32 void Construct( TArgs&&... args ) { new (this) T(std::forward<TArgs>(args)... ); }
33
34 /// Destructs the custom type \p{T} represented by this placeholder.
35 void Destruct() { lang::Destruct( *Get() ); }
36
37 /// Returns a non-constant pointer to the represented instance of type \p{T}.
38 /// @return A pointer to \p{T}.
39 T* Get() { return reinterpret_cast<T*>(this); }
40
41 /// Returns a constant pointer to the represented instance of type \p{T}.
42 /// @return A pointer to \p{T}.
43 const T* Get() const { return reinterpret_cast<T*>(this); }
44
45 /// Overloaded operator to access the represented instance of type \p{T}.
46 /// @return A pointer to \p{T}.
47 T* operator->() { return Get(); }
48
49 /// Overloaded operator to access the represented instance of type \p{T}.
50 /// @return A constant pointer to \p{T}.
51 const T* operator->() const { return Get(); }
52
53 /// Overloaded operator to access the represented instance of type \p{T}.
54 /// @return A pointer to \p{T}.
55 T& operator*() { return *Get(); }
56
57 /// Overloaded operator to access the represented instance of type \p{T}.
58 /// @return A constant pointer to \p{T}.
59 const T& operator*() const { return *Get(); }
60}; // struct Placeholder
61
62} // namespace alib[::lang]
#define ALIB_EXPORT
Definition alib.inl:497
void Destruct(T &object)
Definition tmp.inl:82
void Destruct()
Destructs the custom type T represented by this placeholder.
void Construct(TArgs &&... args)
const T & operator*() const
const T * operator->() const
Placeholder()
Constructor. Initializes the occupied memory with 0-bytes.
char bytes[sizeof(T)]
the placeholder space.