ALib C++ Library
by
Library Version:
2510 R0
Documentation generated by
Loading...
Searching...
No Matches
home
dev
A-Worx
ALib
src
alib
lang
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
//==================================================================================================
8
ALIB_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
//==================================================================================================
19
template
<
typename
T>
20
struct
alignas
(alignof(T))
Placeholder
21
{
22
/// the placeholder space.
23
char
bytes
[
sizeof
(T)];
24
25
/// Constructs the custom type \p{T} represented by this placeholder.
26
/// @tparam TArgs The argument types used for constructing \p{T}.
27
/// @param args The arguments to construct the instance of \p{T}.
28
template
<
typename
... TArgs>
29
void
Construct
( TArgs&&... args ) {
new
(
this
) T(std::forward<TArgs>(args)... ); }
30
31
/// Destructs the custom type \p{T} represented by this placeholder.
32
void
Destruct
() {
lang::Destruct
( *
Get
() ); }
33
34
/// Returns a non-constant pointer to the represented instance of type \p{T}.
35
/// @return A pointer to \p{T}.
36
T*
Get
() {
return
reinterpret_cast<
T*
>
(
this
); }
37
38
/// Returns a constant pointer to the represented instance of type \p{T}.
39
/// @return A pointer to \p{T}.
40
const
T*
Get
()
const
{
return
reinterpret_cast<
T*
>
(
this
); }
41
42
/// Overloaded operator to access the represented instance of type \p{T}.
43
/// @return A pointer to \p{T}.
44
T*
operator->
() {
return
Get
(); }
45
46
/// Overloaded operator to access the represented instance of type \p{T}.
47
/// @return A constant pointer to \p{T}.
48
const
T*
operator->
()
const
{
return
Get
(); }
49
50
/// Overloaded operator to access the represented instance of type \p{T}.
51
/// @return A pointer to \p{T}.
52
T&
operator*
() {
return
*
Get
(); }
53
54
/// Overloaded operator to access the represented instance of type \p{T}.
55
/// @return A constant pointer to \p{T}.
56
const
T&
operator*
()
const
{
return
*
Get
(); }
57
};
// struct Placeholder
58
59
}
// namespace alib[::lang]
60
61
62
63
ALIB_EXPORT
#define ALIB_EXPORT
Definition
alib.inl:488
alib::lang
Definition
allocation.inl:8
alib::lang::Destruct
void Destruct(T &object)
Definition
tmp.inl:83
alib::lang::Placeholder
Definition
lang/placeholder.inl:21
alib::lang::Placeholder::Destruct
void Destruct()
Destructs the custom type T represented by this placeholder.
Definition
lang/placeholder.inl:32
alib::lang::Placeholder::Construct
void Construct(TArgs &&... args)
Definition
lang/placeholder.inl:29
alib::lang::Placeholder::operator*
const T & operator*() const
Definition
lang/placeholder.inl:56
alib::lang::Placeholder::Get
T * Get()
Definition
lang/placeholder.inl:36
alib::lang::Placeholder::operator->
const T * operator->() const
Definition
lang/placeholder.inl:48
alib::lang::Placeholder::operator*
T & operator*()
Definition
lang/placeholder.inl:52
alib::lang::Placeholder::operator->
T * operator->()
Definition
lang/placeholder.inl:44
alib::lang::Placeholder::bytes
char bytes[sizeof(T)]
the placeholder space.
Definition
lang/placeholder.inl:23
alib::lang::Placeholder::Get
const T * Get() const
Definition
lang/placeholder.inl:40