ALib C++ Library
by
Library Version:
2412 R0
Documentation generated by
Loading...
Searching...
No Matches
home
dev
A-Worx
ALib
src
alib
lang
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
12
#include "
alib/lang/allocation.hpp
"
13
14
namespace
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
//==================================================================================================
25
template
<
typename
T>
26
struct
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
allocation.hpp
alib::lang
Definition
alib.cpp:313
alib::lang::Destruct
static ALIB_FORCE_INLINE void Destruct(T &object)
Definition
allocation.hpp:729
alib::lang::Placeholder
Definition
placeholder.hpp:27
alib::lang::Placeholder::Get
const T * Get() const
Definition
placeholder.hpp:46
alib::lang::Placeholder::operator*
const T & operator*() const
Definition
placeholder.hpp:62
alib::lang::Placeholder::operator*
T & operator*()
Definition
placeholder.hpp:58
alib::lang::Placeholder::operator->
T * operator->()
Definition
placeholder.hpp:50
alib::lang::Placeholder::bytes
char bytes[sizeof(T)]
the placeholder space.
Definition
placeholder.hpp:29
alib::lang::Placeholder::Construct
void Construct(TArgs &&... args)
Definition
placeholder.hpp:35
alib::lang::Placeholder::operator->
const T * operator->() const
Definition
placeholder.hpp:54
alib::lang::Placeholder::Destruct
void Destruct()
Destructs the custom type T represented by this placeholder.
Definition
placeholder.hpp:38
alib::lang::Placeholder::Get
T * Get()
Definition
placeholder.hpp:42