ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
optional.h
Go to the documentation of this file.
1#pragma once
2//
3// Copyright (c) 2017 Martin Moene
4//
5// https://github.com/martinmoene/optional-bare
6//
7// This code is licensed under the MIT License (MIT).
8//
9// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
12// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
15// THE SOFTWARE.
16//
17// Modified by Otto Winter on 18.05.18
18
19#include <algorithm>
20
21namespace esphome {
22
23// type for nullopt
24
25struct nullopt_t { // NOLINT
26 struct init {}; // NOLINT
27 nullopt_t(init /*unused*/) {}
28};
29
30// extra parenthesis to prevent the most vexing parse:
31
32const nullopt_t nullopt((nullopt_t::init())); // NOLINT
33
34// Simplistic optional: requires T to be default constructible, copyable.
35
36template<typename T> class optional { // NOLINT
37 private:
38 using safe_bool = void (optional::*)() const;
39
40 public:
41 using value_type = T;
42
44
45 optional(nullopt_t /*unused*/) {}
46
47 optional(T const &arg) : has_value_(true), value_(arg) {} // NOLINT
48
49 template<class U> optional(optional<U> const &other) : has_value_(other.has_value()), value_(other.value()) {}
50
52 reset();
53 return *this;
54 }
55 bool operator==(optional<T> const &rhs) const {
56 if (has_value() && rhs.has_value())
57 return value() == rhs.value();
58 return !has_value() && !rhs.has_value();
59 }
60
61 template<class U> optional &operator=(optional<U> const &other) {
62 has_value_ = other.has_value();
63 value_ = other.value();
64 return *this;
65 }
66
67 void swap(optional &rhs) noexcept {
68 using std::swap;
69 if (has_value() && rhs.has_value()) {
70 swap(**this, *rhs);
71 } else if (!has_value() && rhs.has_value()) {
72 initialize(*rhs);
73 rhs.reset();
74 } else if (has_value() && !rhs.has_value()) {
75 rhs.initialize(**this);
76 reset();
77 }
78 }
79
80 // observers
81
82 value_type const *operator->() const { return &value_; }
83
84 value_type *operator->() { return &value_; }
85
86 value_type const &operator*() const { return value_; }
87
88 value_type &operator*() { return value_; }
89
90 operator safe_bool() const { return has_value() ? &optional::this_type_does_not_support_comparisons : nullptr; }
91
92 bool has_value() const { return has_value_; }
93
94 value_type const &value() const { return value_; }
95
96 value_type &value() { return value_; }
97
98 template<class U> value_type value_or(U const &v) const { return has_value() ? value() : static_cast<value_type>(v); }
99
100 // modifiers
101
102 void reset() { has_value_ = false; }
103
104 private:
105 void this_type_does_not_support_comparisons() const {} // NOLINT
106
107 template<typename V> void initialize(V const &value) { // NOLINT
108 value_ = value;
109 has_value_ = true;
110 }
111
112 bool has_value_{false}; // NOLINT
113 value_type value_; // NOLINT
114};
115
116// Relational operators
117
118template<typename T, typename U> inline bool operator==(optional<T> const &x, optional<U> const &y) {
119 return bool(x) != bool(y) ? false : !bool(x) ? true : *x == *y;
120}
121
122template<typename T, typename U> inline bool operator!=(optional<T> const &x, optional<U> const &y) {
123 return !(x == y);
124}
125
126template<typename T, typename U> inline bool operator<(optional<T> const &x, optional<U> const &y) {
127 return (!y) ? false : (!x) ? true : *x < *y;
128}
129
130template<typename T, typename U> inline bool operator>(optional<T> const &x, optional<U> const &y) { return (y < x); }
131
132template<typename T, typename U> inline bool operator<=(optional<T> const &x, optional<U> const &y) { return !(y < x); }
133
134template<typename T, typename U> inline bool operator>=(optional<T> const &x, optional<U> const &y) { return !(x < y); }
135
136// Comparison with nullopt
137
138template<typename T> inline bool operator==(optional<T> const &x, nullopt_t /*unused*/) { return (!x); }
139
140template<typename T> inline bool operator==(nullopt_t /*unused*/, optional<T> const &x) { return (!x); }
141
142template<typename T> inline bool operator!=(optional<T> const &x, nullopt_t /*unused*/) { return bool(x); }
143
144template<typename T> inline bool operator!=(nullopt_t /*unused*/, optional<T> const &x) { return bool(x); }
145
146template<typename T> inline bool operator<(optional<T> const & /*unused*/, nullopt_t /*unused*/) { return false; }
147
148template<typename T> inline bool operator<(nullopt_t /*unused*/, optional<T> const &x) { return bool(x); }
149
150template<typename T> inline bool operator<=(optional<T> const &x, nullopt_t /*unused*/) { return (!x); }
151
152template<typename T> inline bool operator<=(nullopt_t /*unused*/, optional<T> const & /*unused*/) { return true; }
153
154template<typename T> inline bool operator>(optional<T> const &x, nullopt_t /*unused*/) { return bool(x); }
155
156template<typename T> inline bool operator>(nullopt_t /*unused*/, optional<T> const & /*unused*/) { return false; }
157
158template<typename T> inline bool operator>=(optional<T> const & /*unused*/, nullopt_t /*unused*/) { return true; }
159
160template<typename T> inline bool operator>=(nullopt_t /*unused*/, optional<T> const &x) { return (!x); }
161
162// Comparison with T
163
164template<typename T, typename U> inline bool operator==(optional<T> const &x, U const &v) {
165 return bool(x) ? *x == v : false;
166}
167
168template<typename T, typename U> inline bool operator==(U const &v, optional<T> const &x) {
169 return bool(x) ? v == *x : false;
170}
171
172template<typename T, typename U> inline bool operator!=(optional<T> const &x, U const &v) {
173 return bool(x) ? *x != v : true;
174}
175
176template<typename T, typename U> inline bool operator!=(U const &v, optional<T> const &x) {
177 return bool(x) ? v != *x : true;
178}
179
180template<typename T, typename U> inline bool operator<(optional<T> const &x, U const &v) {
181 return bool(x) ? *x < v : true;
182}
183
184template<typename T, typename U> inline bool operator<(U const &v, optional<T> const &x) {
185 return bool(x) ? v < *x : false;
186}
187
188template<typename T, typename U> inline bool operator<=(optional<T> const &x, U const &v) {
189 return bool(x) ? *x <= v : true;
190}
191
192template<typename T, typename U> inline bool operator<=(U const &v, optional<T> const &x) {
193 return bool(x) ? v <= *x : false;
194}
195
196template<typename T, typename U> inline bool operator>(optional<T> const &x, U const &v) {
197 return bool(x) ? *x > v : false;
198}
199
200template<typename T, typename U> inline bool operator>(U const &v, optional<T> const &x) {
201 return bool(x) ? v > *x : true;
202}
203
204template<typename T, typename U> inline bool operator>=(optional<T> const &x, U const &v) {
205 return bool(x) ? *x >= v : false;
206}
207
208template<typename T, typename U> inline bool operator>=(U const &v, optional<T> const &x) {
209 return bool(x) ? v >= *x : true;
210}
211
212// Specialized algorithms
213
214template<typename T> void swap(optional<T> &x, optional<T> &y) noexcept { x.swap(y); }
215
216// Convenience function to create an optional.
217
218template<typename T> inline optional<T> make_optional(T const &v) { return optional<T>(v); }
219
220} // namespace esphome
bool has_value() const
Definition optional.h:92
value_type value_or(U const &v) const
Definition optional.h:98
void swap(optional &rhs) noexcept
Definition optional.h:67
optional & operator=(optional< U > const &other)
Definition optional.h:61
value_type & operator*()
Definition optional.h:88
optional(optional< U > const &other)
Definition optional.h:49
bool operator==(optional< T > const &rhs) const
Definition optional.h:55
optional(T const &arg)
Definition optional.h:47
value_type & value()
Definition optional.h:96
optional & operator=(nullopt_t)
Definition optional.h:51
value_type const & value() const
Definition optional.h:94
optional(nullopt_t)
Definition optional.h:45
value_type * operator->()
Definition optional.h:84
value_type const & operator*() const
Definition optional.h:86
value_type const * operator->() const
Definition optional.h:82
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
bool operator>=(optional< T > const &x, optional< U > const &y)
Definition optional.h:134
bool operator==(optional< T > const &x, optional< U > const &y)
Definition optional.h:118
void swap(optional< T > &x, optional< T > &y) noexcept
Definition optional.h:214
bool operator!=(optional< T > const &x, optional< U > const &y)
Definition optional.h:122
bool operator>(optional< T > const &x, optional< U > const &y)
Definition optional.h:130
bool operator<(optional< T > const &x, optional< U > const &y)
Definition optional.h:126
bool operator<=(optional< T > const &x, optional< U > const &y)
Definition optional.h:132
optional< T > make_optional(T const &v)
Definition optional.h:218
const nullopt_t nullopt((nullopt_t::init()))
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6