forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcata_lazy.h
80 lines (63 loc) · 1.88 KB
/
cata_lazy.h
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
#pragma once
#ifndef CATA_SRC_CATA_LAZY_H
#define CATA_SRC_CATA_LAZY_H
#include <optional>
#include <type_traits>
/**
* A wrapper for a type that lazily initializes the underlying type as needed. Use to
* save unnecessary allocation costs for eg. std containers in transiently allocated types.
*
* T must be default initializable.
*/
template<typename T, typename = std::enable_if_t<std::is_default_constructible_v<T>>>
struct lazy {
lazy() = default;
// std::optional-like api for testing without allocating
bool has_value() const {
return actual.has_value();
}
// Transparent value operators that proxy to the actual value.
template<typename Arg, typename ...Args>
// NOLINTNEXTLINE(google-explicit-constructor)
lazy( Arg &&arg, Args &&...args ) : actual{std::in_place, std::forward<Arg>( arg ), std::forward<Args>( args )...} {}
template<typename U>
// NOLINTNEXTLINE(misc-unconventional-assign-operator)
T &operator=( U &&u ) {
get() = std::forward<U>( u );
return get();
}
// NOLINTNEXTLINE(google-explicit-constructor)
operator T &() {
return get();
}
// NOLINTNEXTLINE(google-explicit-constructor)
operator T const &() const {
return get();
}
T &operator*() {
return get();
}
const T &operator*() const {
return get();
}
T *operator->() {
return &get();
}
const T *operator->() const {
return &get();
}
template<typename Arg>
auto &&operator[]( Arg &&arg ) {
return get()[std::forward<Arg>( arg )];
}
private:
// The constness is a lie because actual is mutable, but that's why it's private.
T &get() const {
if( !actual.has_value() ) {
actual.emplace();
}
return actual.value();
}
mutable std::optional<T> actual;
};
#endif // CATA_SRC_CATA_LAZY_H