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