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
|
/*
Minetest
Copyright (C) 2021 rubenwardy
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <utility>
#include "debug.h"
struct nullopt_t
{
};
constexpr nullopt_t nullopt{};
/**
* An implementation of optional for C++11, which aims to be
* compatible with a subset of std::optional features.
*
* Unfortunately, Minetest doesn't use C++17 yet.
*
* @tparam T The type to be stored
*/
template <typename T>
class Optional
{
bool m_has_value = false;
T m_value;
public:
Optional() noexcept {}
Optional(nullopt_t) noexcept {}
Optional(const T &value) noexcept : m_has_value(true), m_value(value) {}
Optional(T &&value) noexcept : m_has_value(true), m_value(std::move(value)) {}
Optional(const Optional<T> &other) noexcept :
m_has_value(other.m_has_value), m_value(other.m_value)
{}
Optional(Optional<T> &&other) noexcept :
m_has_value(other.m_has_value), m_value(std::move(other.m_value))
{
other.m_has_value = false;
}
Optional<T> &operator=(nullopt_t) noexcept { m_has_value = false; return *this; }
Optional<T> &operator=(const Optional<T> &other) noexcept
{
if (&other == this)
return *this;
m_has_value = other.m_has_value;
m_value = other.m_value;
return *this;
}
Optional<T> &operator=(Optional<T> &&other) noexcept
{
if (&other == this)
return *this;
m_has_value = other.m_has_value;
m_value = std::move(other.m_value);
other.m_has_value = false;
return *this;
}
T &value()
{
FATAL_ERROR_IF(!m_has_value, "optional doesn't have value");
return m_value;
}
const T &value() const
{
FATAL_ERROR_IF(!m_has_value, "optional doesn't have value");
return m_value;
}
const T &value_or(const T &def) const { return m_has_value ? m_value : def; }
// Unchecked access consistent with std::optional
T* operator->() { return &m_value; }
const T* operator->() const { return &m_value; }
T& operator*() { return m_value; }
const T& operator*() const { return m_value; }
bool has_value() const noexcept { return m_has_value; }
explicit operator bool() const { return m_has_value; }
};
template <typename T>
constexpr bool operator==(const Optional<T> &opt, nullopt_t)
{
return !opt.has_value();
}
template <typename T>
constexpr bool operator==(nullopt_t, const Optional<T> &opt)
{
return !opt.has_value();
}
template <typename T>
constexpr bool operator!=(const Optional<T> &opt, nullopt_t)
{
return opt.has_value();
}
template <typename T>
constexpr bool operator!=(nullopt_t, const Optional<T> &opt)
{
return opt.has_value();
}
template <typename T, typename U>
constexpr bool operator==(const Optional<T> &opt, const U &value)
{
return opt.has_value() && *opt == value;
}
template <typename T, typename U>
constexpr bool operator==(const T &value, const Optional<U> &opt)
{
return opt.has_value() && value == *opt;
}
template <typename T, typename U>
constexpr bool operator!=(const Optional<T> &opt, const U &value)
{
return !opt.has_value() || *opt != value;
}
template <typename T, typename U>
constexpr bool operator!=(const T &value, const Optional<U> &opt)
{
return !opt.has_value() || value != *opt;
}
template <typename T, typename U>
constexpr bool operator==(const Optional<T> &lhs, const Optional<U> &rhs)
{
return lhs.has_value() ? *lhs == rhs : nullopt == rhs;
}
template <typename T, typename U>
constexpr bool operator!=(const Optional<T> &lhs, const Optional<U> &rhs)
{
return lhs.has_value() ? *lhs != rhs : nullopt != rhs;
}
|