ALib C++ Framework
by
Library Version:
2605 R0
Documentation generated by
Loading...
Searching...
No Matches
ALib
src
alib
lang
lang/placeholder.hpp
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
/// Copyright 2013-2026 A-Worx GmbH, Germany.
6
/// Published under #"mainpage_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
/// the placeholder space.
22
char
bytes
[
sizeof
(T)];
23
24
/// Constructor. Initializes the occupied memory with 0-bytes.
25
Placeholder
() {}
26
27
/// Constructs the custom type \p{T} represented by this placeholder.
28
/// @tparam TArgs The argument types used for constructing \p{T}.
29
/// @param args The arguments to construct the instance of \p{T}.
30
template
<
typename
... TArgs>
31
void
Construct
( TArgs&&... args ) {
new
(
this
) T(std::forward<TArgs>(args)... ); }
32
33
/// Destructs the custom type \p{T} represented by this placeholder.
34
void
Destruct
() {
lang::Destruct
( *
Get
() ); }
35
36
/// Returns a non-constant pointer to the represented instance of type \p{T}.
37
/// @return A pointer to \p{T}.
38
T*
Get
() {
return
reinterpret_cast<
T*
>
(
this
); }
39
40
/// Returns a constant pointer to the represented instance of type \p{T}.
41
/// @return A pointer to \p{T}.
42
const
T*
Get
()
const
{
return
reinterpret_cast<
T*
>
(
this
); }
43
44
/// Overloaded operator to access the represented instance of type \p{T}.
45
/// @return A pointer to \p{T}.
46
T*
operator->
() {
return
Get
(); }
47
48
/// Overloaded operator to access the represented instance of type \p{T}.
49
/// @return A constant pointer to \p{T}.
50
const
T*
operator->
()
const
{
return
Get
(); }
51
52
/// Overloaded operator to access the represented instance of type \p{T}.
53
/// @return A pointer to \p{T}.
54
T&
operator*
() {
return
*
Get
(); }
55
56
/// Overloaded operator to access the represented instance of type \p{T}.
57
/// @return A constant pointer to \p{T}.
58
const
T&
operator*
()
const
{
return
*
Get
(); }
59
};
// struct Placeholder
60
61
}
// namespace alib[::lang]
ALIB_EXPORT
#define ALIB_EXPORT
Definition
alib.prepro.hpp:538
alib::lang
Definition
allocation.hpp:8
alib::lang::Destruct
void Destruct(T &object)
Definition
tmp.hpp:82
alib::lang::Placeholder::Destruct
void Destruct()
Destructs the custom type T represented by this placeholder.
Definition
lang/placeholder.hpp:34
alib::lang::Placeholder::Construct
void Construct(TArgs &&... args)
Definition
lang/placeholder.hpp:31
alib::lang::Placeholder::operator*
const T & operator*() const
Definition
lang/placeholder.hpp:58
alib::lang::Placeholder::Get
T * Get()
Definition
lang/placeholder.hpp:38
alib::lang::Placeholder::operator->
const T * operator->() const
Definition
lang/placeholder.hpp:50
alib::lang::Placeholder::operator*
T & operator*()
Definition
lang/placeholder.hpp:54
alib::lang::Placeholder::Placeholder
Placeholder()
Constructor. Initializes the occupied memory with 0-bytes.
Definition
lang/placeholder.hpp:25
alib::lang::Placeholder::operator->
T * operator->()
Definition
lang/placeholder.hpp:46
alib::lang::Placeholder::bytes
char bytes[sizeof(T)]
the placeholder space.
Definition
lang/placeholder.hpp:22
alib::lang::Placeholder::Get
const T * Get() const
Definition
lang/placeholder.hpp:42