blob: 963e6c9e694544c9337131ceafa1bb9378530b1b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
///////////////////////////////////////////////////////////////////////////////
// Reference implementation of std::generator proposal P2168.
//
// See https://wg21.link/P2168 for details.
//
///////////////////////////////////////////////////////////////////////////////
// Copyright Lewis Baker, Corentin Jabot
//
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0.
// (See accompanying file LICENSE or http://www.boost.org/LICENSE_1_0.txt)
///////////////////////////////////////////////////////////////////////////////
module;
#include <concepts>
#include <memory>
export module nihil.generator:manual_lifetime;
namespace nihil {
template <typename T>
struct manual_lifetime {
manual_lifetime() noexcept {}
~manual_lifetime() {}
template <typename ...Args>
auto construct(this manual_lifetime &self, Args && ...args)
noexcept(std::is_nothrow_constructible_v<T, Args...>)
-> T &
{
return *::new (static_cast<void*>(std::addressof(self.m_value)))
T(static_cast<Args &&>(args)...);
}
void destruct(this manual_lifetime &self)
noexcept(std::is_nothrow_destructible_v<T>)
{
self.m_value.~T();
}
auto get(this manual_lifetime &self) noexcept -> T &
{
return self.m_value;
}
auto get(this manual_lifetime &&self) noexcept -> T &&
{
return static_cast<T&&>(self.m_value);
}
auto get(this manual_lifetime const &self) noexcept -> T const &
{
return self.m_value;
}
auto get(this manual_lifetime const &&self) noexcept -> T const &&
{
return static_cast<T const &&>(self.m_value);
}
private:
union {
std::remove_const_t<T> m_value;
};
};
template <typename T>
class manual_lifetime<T &> {
manual_lifetime() noexcept = default;
~manual_lifetime() = default;
auto construct(this manual_lifetime &self, T &value) noexcept -> T &
{
self.m_value = std::addressof(value);
return self.m_value;
}
auto destruct(this manual_lifetime &) noexcept -> void
{
}
auto get(this manual_lifetime const &self) noexcept -> T &
{
return *self.m_value;
}
private:
T *m_value = nullptr;
};
template <typename T>
class manual_lifetime<T &&> {
manual_lifetime() noexcept = default;
~manual_lifetime() = default;
auto construct(this manual_lifetime &self, T &&value) noexcept -> T &&
{
self.m_value = std::addressof(value);
return static_cast<T &&>(value);
}
void destruct(this manual_lifetime &) noexcept
{
}
auto get(this manual_lifetime const &self) noexcept -> T &&
{
return static_cast<T &&>(*self.m_value);
}
private:
T* m_value = nullptr;
};
}
|