Debian Codegraph @

Utilities/std/cm/optional

raw
1 // -*-c++-*-
2 // vim: set ft=cpp:
3
4 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
5 file LICENSE.rst or https://cmake.org/licensing for details. */
6 #pragma once
7
8 #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
9 # define CMake_HAVE_CXX_OPTIONAL
10 #endif
11
12 #if defined(CMake_HAVE_CXX_OPTIONAL)
13 # include <optional> // IWYU pragma: export
14 #else
15 # if defined(__GNUC__) && !defined(__clang__)
16 # include <cstring>
17 # endif
18 # include <memory>
19
20 # include <cm/utility>
21 #endif
22
23 namespace cm {
24
25 #if defined(CMake_HAVE_CXX_OPTIONAL)
26
27 using std::nullopt_t;
28 using std::nullopt;
29 using std::optional;
30 using std::bad_optional_access;
31 using std::make_optional;
32
33 #else
34
35 class bad_optional_access : public std::exception
36 {
37 using std::exception::exception;
38 };
39
40 struct nullopt_t
41 {
42 explicit constexpr nullopt_t(int) {}
43 };
44
45 constexpr nullopt_t nullopt{ 0 };
46
47 template <typename T>
48 class optional
49 {
50 public:
51 using value_type = T;
52
53 optional() noexcept = default;
54 optional(nullopt_t) noexcept;
55 optional(optional const& other);
56 optional(optional&& other) noexcept;
57
58 template <typename... Args>
59 explicit optional(cm::in_place_t, Args&&... args);
60
61 template <
62 typename U = T,
63 typename = typename std::enable_if<
64 std::is_constructible<T, U&&>::value &&
65 !std::is_same<typename std::decay<U>::type, cm::in_place_t>::value &&
66 !std::is_same<typename std::decay<U>::type,
67 cm::optional<T>>::value>::type>
68 optional(U&& v);
69
70 ~optional();
71
72 optional& operator=(nullopt_t) noexcept;
73 optional& operator=(optional const& other);
74
75 template <typename U = T>
76 typename std::enable_if<std::is_constructible<T, U&&>::value &&
77 std::is_assignable<T&, U&&>::value,
78 optional&>::type
79 operator=(optional<U>&& other) noexcept;
80
81 template <typename U = T>
82 typename std::enable_if<
83 !std::is_same<typename std::decay<U>::type, cm::optional<T>>::value &&
84 std::is_constructible<T, U&&>::value &&
85 std::is_assignable<T&, U&&>::value &&
86 (!std::is_scalar<T>::value ||
87 !std::is_same<typename std::decay<U>::type, T>::value),
88 optional&>::type
89 operator=(U&& v);
90
91 T const* operator->() const;
92 T* operator->();
93 T const& operator*() const&;
94 T& operator*() &;
95 T const&& operator*() const&&;
96 T&& operator*() &&;
97
98 explicit operator bool() const noexcept;
99 bool has_value() const noexcept;
100
101 T& value() &;
102 T const& value() const&;
103
104 T&& value() &&;
105 T const&& value() const&&;
106
107 template <typename U>
108 T value_or(U&& default_value) const&;
109
110 template <typename U>
111 T value_or(U&& default_value) &&;
112
113 void swap(optional& other) noexcept;
114 void reset() noexcept;
115
116 template <typename... Args>
117 T& emplace(Args&&... args);
118
119 private:
120 bool _has_value = false;
121 std::allocator<T> _allocator;
122 union _mem_union
123 {
124 T value;
125
126 // Explicit constructor and destructor is required to make this work
127 _mem_union() noexcept
128 {
129 # if defined(__GNUC__) && !defined(__clang__)
130 // Some versions of GCC complain about uninitialized memory
131 std::memset(this, 0, sizeof(*this));
132 # endif
133 }
134 ~_mem_union() noexcept {}
135 } _mem;
136 };
137
138 template <typename T>
139 optional<typename std::decay<T>::type> make_optional(T&& value)
140 {
141 return optional<typename std::decay<T>::type>(std::forward<T>(value));
142 }
143
144 template <typename T, class... Args>
145 optional<T> make_optional(Args&&... args)
146 {
147 return optional<T>(in_place, std::forward<Args>(args)...);
148 }
149
150 template <typename T>
151 optional<T>::optional(nullopt_t) noexcept
152 : optional()
153 {
154 }
155
156 template <typename T>
157 optional<T>::optional(optional const& other)
158 {
159 if (other.has_value()) {
160 this->emplace(*other);
161 }
162 }
163
164 template <typename T>
165 optional<T>::optional(optional&& other) noexcept
166 {
167 if (other.has_value()) {
168 this->emplace(std::move(*other));
169 }
170 }
171
172 template <typename T>
173 template <typename... Args>
174 optional<T>::optional(cm::in_place_t, Args&&... args)
175 {
176 this->emplace(std::forward<Args>(args)...);
177 }
178
179 template <typename T>
180 template <typename U, typename>
181 optional<T>::optional(U&& v)
182 {
183 this->emplace(std::forward<U>(v));
184 }
185
186 template <typename T>
187 optional<T>::~optional()
188 {
189 this->reset();
190 }
191
192 template <typename T>
193 optional<T>& optional<T>::operator=(nullopt_t) noexcept
194 {
195 this->reset();
196 return *this;
197 }
198
199 template <typename T>
200 optional<T>& optional<T>::operator=(optional const& other)
201 {
202 if (other.has_value()) {
203 if (this->has_value()) {
204 this->value() = *other;
205 } else {
206 this->emplace(*other);
207 }
208 } else {
209 this->reset();
210 }
211 return *this;
212 }
213
214 template <typename T>
215 template <typename U>
216 typename std::enable_if<std::is_constructible<T, U&&>::value &&
217 std::is_assignable<T&, U&&>::value,
218 optional<T>&>::type
219 optional<T>::operator=(optional<U>&& other) noexcept
220 {
221 if (other.has_value()) {
222 if (this->has_value()) {
223 this->value() = std::move(*other);
224 } else {
225 this->emplace(std::move(*other));
226 }
227 } else {
228 this->reset();
229 }
230 return *this;
231 }
232
233 template <typename T>
234 template <typename U>
235 typename std::enable_if<
236 !std::is_same<typename std::decay<U>::type, cm::optional<T>>::value &&
237 std::is_constructible<T, U&&>::value &&
238 std::is_assignable<T&, U&&>::value &&
239 (!std::is_scalar<T>::value ||
240 !std::is_same<typename std::decay<U>::type, T>::value),
241 optional<T>&>::type
242 optional<T>::operator=(U&& v)
243 {
244 if (this->has_value()) {
245 this->value() = v;
246 } else {
247 this->emplace(std::forward<U>(v));
248 }
249 return *this;
250 }
251
252 template <typename T, typename U>
253 bool operator==(optional<T> const& lhs, optional<U> const& rhs)
254 {
255 if (lhs.has_value()) {
256 return rhs.has_value() && *lhs == *rhs;
257 }
258 return !rhs.has_value();
259 }
260
261 template <typename T, typename U>
262 bool operator!=(optional<T> const& lhs, optional<U> const& rhs)
263 {
264 if (lhs.has_value()) {
265 return !rhs.has_value() || *lhs != *rhs;
266 }
267 return rhs.has_value();
268 }
269
270 template <typename T, typename U>
271 bool operator<(optional<T> const& lhs, optional<U> const& rhs)
272 {
273 if (rhs.has_value()) {
274 return !lhs.has_value() || *lhs < *rhs;
275 }
276 return false;
277 }
278
279 template <typename T, typename U>
280 bool operator<=(optional<T> const& lhs, optional<U> const& rhs)
281 {
282 if (!lhs.has_value()) {
283 return true;
284 }
285 if (rhs.has_value()) {
286 return *lhs <= *rhs;
287 }
288 return false;
289 }
290
291 template <typename T, typename U>
292 bool operator>(optional<T> const& lhs, optional<U> const& rhs)
293 {
294 if (lhs.has_value()) {
295 return !rhs.has_value() || *lhs > *rhs;
296 }
297 return false;
298 }
299
300 template <typename T, typename U>
301 bool operator>=(optional<T> const& lhs, optional<U> const& rhs)
302 {
303 if (!rhs.has_value()) {
304 return true;
305 }
306 if (lhs.has_value()) {
307 return *lhs >= *rhs;
308 }
309 return false;
310 }
311
312 template <typename T>
313 bool operator==(optional<T> const& opt, nullopt_t) noexcept
314 {
315 return !opt.has_value();
316 }
317
318 template <typename T>
319 bool operator!=(optional<T> const& opt, nullopt_t) noexcept
320 {
321 return opt.has_value();
322 }
323
324 template <typename T>
325 bool operator<(optional<T> const& /*opt*/, nullopt_t) noexcept
326 {
327 return false;
328 }
329
330 template <typename T>
331 bool operator<=(optional<T> const& opt, nullopt_t) noexcept
332 {
333 return !opt.has_value();
334 }
335
336 template <typename T>
337 bool operator>(optional<T> const& opt, nullopt_t) noexcept
338 {
339 return opt.has_value();
340 }
341
342 template <typename T>
343 bool operator>=(optional<T> const& /*opt*/, nullopt_t) noexcept
344 {
345 return true;
346 }
347
348 template <typename T>
349 bool operator==(nullopt_t, optional<T> const& opt) noexcept
350 {
351 return !opt.has_value();
352 }
353
354 template <typename T>
355 bool operator!=(nullopt_t, optional<T> const& opt) noexcept
356 {
357 return opt.has_value();
358 }
359
360 template <typename T>
361 bool operator<(nullopt_t, optional<T> const& opt) noexcept
362 {
363 return opt.has_value();
364 }
365
366 template <typename T>
367 bool operator<=(nullopt_t, optional<T> const& /*opt*/) noexcept
368 {
369 return true;
370 }
371
372 template <typename T>
373 bool operator>(nullopt_t, optional<T> const& /*opt*/) noexcept
374 {
375 return false;
376 }
377
378 template <typename T>
379 bool operator>=(nullopt_t, optional<T> const& opt) noexcept
380 {
381 return !opt.has_value();
382 }
383
384 template <typename T, typename U>
385 bool operator==(optional<T> const& opt, U const& value)
386 {
387 return opt.has_value() && *opt == value;
388 }
389
390 template <typename T, typename U>
391 bool operator!=(optional<T> const& opt, U const& value)
392 {
393 return !opt.has_value() || *opt != value;
394 }
395
396 template <typename T, typename U>
397 bool operator<(optional<T> const& opt, U const& value)
398 {
399 return !opt.has_value() || *opt < value;
400 }
401
402 template <typename T, typename U>
403 bool operator<=(optional<T> const& opt, U const& value)
404 {
405 return !opt.has_value() || *opt <= value;
406 }
407
408 template <typename T, typename U>
409 bool operator>(optional<T> const& opt, U const& value)
410 {
411 return opt.has_value() && *opt > value;
412 }
413
414 template <typename T, typename U>
415 bool operator>=(optional<T> const& opt, U const& value)
416 {
417 return opt.has_value() && *opt >= value;
418 }
419
420 template <typename T, typename U>
421 bool operator==(T const& value, optional<U> const& opt)
422 {
423 return opt.has_value() && value == *opt;
424 }
425
426 template <typename T, typename U>
427 bool operator!=(T const& value, optional<U> const& opt)
428 {
429 return !opt.has_value() || value != *opt;
430 }
431
432 template <typename T, typename U>
433 bool operator<(T const& value, optional<U> const& opt)
434 {
435 return opt.has_value() && value < *opt;
436 }
437
438 template <typename T, typename U>
439 bool operator<=(T const& value, optional<U> const& opt)
440 {
441 return opt.has_value() && value <= *opt;
442 }
443
444 template <typename T, typename U>
445 bool operator>(T const& value, optional<U> const& opt)
446 {
447 return !opt.has_value() || value > *opt;
448 }
449
450 template <typename T, typename U>
451 bool operator>=(T const& value, optional<U> const& opt)
452 {
453 return !opt.has_value() || value >= *opt;
454 }
455
456 template <typename T>
457 T const* optional<T>::operator->() const
458 {
459 return &**this;
460 }
461
462 template <typename T>
463 T* optional<T>::operator->()
464 {
465 return &**this;
466 }
467
468 template <typename T>
469 T const& optional<T>::operator*() const&
470 {
471 return this->_mem.value;
472 }
473
474 template <typename T>
475 T& optional<T>::operator*() &
476 {
477 return this->_mem.value;
478 }
479
480 template <typename T>
481 T const&& optional<T>::operator*() const&&
482 {
483 return std::move(**this);
484 }
485
486 template <typename T>
487 T&& optional<T>::operator*() &&
488 {
489 return std::move(**this);
490 }
491
492 template <typename T>
493 bool optional<T>::has_value() const noexcept
494 {
495 return this->_has_value;
496 }
497
498 template <typename T>
499 optional<T>::operator bool() const noexcept
500 {
501 return this->has_value();
502 }
503
504 template <typename T>
505 T& optional<T>::value() &
506 {
507 if (!this->has_value()) {
508 throw cm::bad_optional_access{};
509 }
510 return **this;
511 }
512
513 template <typename T>
514 T const& optional<T>::value() const&
515 {
516 if (!this->has_value()) {
517 throw cm::bad_optional_access{};
518 }
519 return **this;
520 }
521
522 template <typename T>
523 template <typename U>
524 T optional<T>::value_or(U&& default_value) const&
525 {
526 return bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value));
527 }
528
529 template <typename T>
530 template <typename U>
531 T optional<T>::value_or(U&& default_value) &&
532 {
533 return bool(*this) ? std::move(**this)
534 : static_cast<T>(std::forward<U>(default_value));
535 }
536
537 template <typename T>
538 void optional<T>::swap(optional& other) noexcept
539 {
540 if (this->has_value()) {
541 if (other.has_value()) {
542 using std::swap;
543 swap(**this, *other);
544 } else {
545 other.emplace(std::move(**this));
546 this->reset();
547 }
548 } else if (other.has_value()) {
549 this->emplace(std::move(*other));
550 other.reset();
551 }
552 }
553
554 template <typename T>
555 void optional<T>::reset() noexcept
556 {
557 if (this->has_value()) {
558 this->_has_value = false;
559 std::allocator_traits<std::allocator<T>>::destroy(this->_allocator,
560 &**this);
561 }
562 }
563
564 template <typename T>
565 template <typename... Args>
566 T& optional<T>::emplace(Args&&... args)
567 {
568 this->reset();
569 std::allocator_traits<std::allocator<T>>::construct(
570 this->_allocator, &**this, std::forward<Args>(args)...);
571 this->_has_value = true;
572 return this->value();
573 }
574
575 #endif
576 }