-
Notifications
You must be signed in to change notification settings - Fork 182
/
manual_lifetime.hpp
184 lines (152 loc) · 4.96 KB
/
manual_lifetime.hpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <unifex/scope_guard.hpp>
#include <unifex/type_traits.hpp>
#include <functional>
#include <memory>
#include <new>
#include <type_traits>
#include <unifex/detail/prologue.hpp>
namespace unifex {
template <typename T>
class manual_lifetime {
static_assert(std::is_nothrow_destructible_v<T>);
public:
manual_lifetime() noexcept {}
~manual_lifetime() {}
template <typename... Args>
[[maybe_unused]] T& construct(Args&&... args) noexcept(
std::is_nothrow_constructible_v<T, Args...>) {
return *::new (static_cast<void*>(std::addressof(value_)))
T((Args &&) args...);
}
template <typename Func>
[[maybe_unused]] T&
construct_with(Func&& func) noexcept(std::is_nothrow_invocable_v<Func>) {
static_assert(
std::is_same_v<std::invoke_result_t<Func>, T>,
"Return type of func() must be exactly T to permit copy-elision.");
return *::new (static_cast<void*>(std::addressof(value_)))
T(((Func &&) func)());
}
void destruct() noexcept { value_.~T(); }
T& get() & noexcept { return value_; }
T&& get() && noexcept { return (T &&) value_; }
const T& get() const& noexcept { return value_; }
const T&& get() const&& noexcept { return (const T&&)value_; }
private:
union {
T value_;
};
};
template <typename T>
class manual_lifetime<T&> {
public:
manual_lifetime() noexcept : value_(nullptr) {}
~manual_lifetime() {}
[[maybe_unused]] T& construct(T& value) noexcept {
value_ = std::addressof(value);
return value;
}
template <typename Func>
[[maybe_unused]] T&
construct_with(Func&& func) noexcept(std::is_nothrow_invocable_v<Func>) {
static_assert(std::is_same_v<std::invoke_result_t<Func>, T&>);
value_ = std::addressof(((Func &&) func)());
return get();
}
void destruct() noexcept {}
T& get() const noexcept { return *value_; }
private:
T* value_;
};
template <typename T>
class manual_lifetime<T&&> {
public:
manual_lifetime() noexcept : value_(nullptr) {}
~manual_lifetime() {}
[[maybe_unused]] T&& construct(T&& value) noexcept {
value_ = std::addressof(value);
return (T &&) value;
}
template <typename Func>
[[maybe_unused]] T&&
construct_with(Func&& func) noexcept(std::is_nothrow_invocable_v<Func>) {
static_assert(std::is_same_v<std::invoke_result_t<Func>, T&&>);
value_ = std::addressof(((Func &&) func)());
return get();
}
void destruct() noexcept {}
T&& get() const noexcept { return (T &&) * value_; }
private:
T* value_;
};
template <>
class manual_lifetime<void> {
public:
manual_lifetime() noexcept = default;
~manual_lifetime() = default;
void construct() noexcept {}
template <typename Func>
void construct_with(Func&& func) noexcept(std::is_nothrow_invocable_v<Func>) {
static_assert(std::is_void_v<std::invoke_result_t<Func>>);
((Func &&) func)();
}
void destruct() noexcept {}
void get() const noexcept {}
};
template <>
class manual_lifetime<void const> : public manual_lifetime<void> {};
// For activating a manual_lifetime when it is in a union and initializing
// its value from arguments to its constructor.
template <typename T, typename... Args>
[[maybe_unused]] //
T& activate_union_member(manual_lifetime<T>& box, Args&&... args) noexcept(
std::is_nothrow_constructible_v<T, Args...>) {
auto* p = ::new (&box) manual_lifetime<T>{};
scope_guard guard = [=]() noexcept {
p->~manual_lifetime();
};
auto& t = box.construct(static_cast<Args&&>(args)...);
guard.release();
return t;
}
inline void activate_union_member(manual_lifetime<void>& box) noexcept {
(::new (&box) manual_lifetime<void>{})->construct();
}
// For activating a manual_lifetime when it is in a union and initializing
// its value from the result of calling a function.
template <typename T, typename Func>
[[maybe_unused]] //
T& activate_union_member_with(manual_lifetime<T>& box, Func&& func) noexcept(
std::is_nothrow_invocable_v<Func>) {
auto* p = ::new (&box) manual_lifetime<T>{};
scope_guard guard = [=]() noexcept {
p->~manual_lifetime();
};
auto& t = p->construct_with(static_cast<Func&&>(func));
guard.release();
return t;
}
// For deactivating a manual_lifetime when it is in a union
template <typename T>
void deactivate_union_member(manual_lifetime<T>& box) noexcept {
box.destruct();
box.~manual_lifetime();
}
} // namespace unifex
#include <unifex/detail/epilogue.hpp>