1//===-- Standalone implementation of std::optional --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_OPTIONAL_H
10#define LLVM_LIBC_SRC___SUPPORT_CPP_OPTIONAL_H
11
12#include "src/__support/CPP/type_traits.h"
13#include "src/__support/CPP/utility.h"
14#include "src/__support/macros/attributes.h"
15#include "src/__support/macros/config.h"
16#include "src/__support/macros/optimization.h"
17
18namespace LIBC_NAMESPACE_DECL {
19namespace cpp {
20
21// Trivial nullopt_t struct.
22struct nullopt_t {
23 LIBC_INLINE constexpr explicit nullopt_t() = default;
24};
25
26// nullopt that can be used and returned.
27LIBC_INLINE_VAR constexpr nullopt_t nullopt{};
28
29// This is very simple implementation of the std::optional class. It makes
30// several assumptions that the underlying type is trivially constructible,
31// copyable, or movable.
32template <typename T> class optional {
33 template <typename U, bool = !is_trivially_destructible<U>::value>
34 struct OptionalStorage {
35 union {
36 char empty;
37 U stored_value;
38 };
39
40 bool in_use = false;
41
42 LIBC_INLINE ~OptionalStorage() { reset(); }
43
44 LIBC_INLINE constexpr OptionalStorage() : empty() {}
45
46 template <typename... Args>
47 LIBC_INLINE constexpr explicit OptionalStorage(in_place_t, Args &&...args)
48 : stored_value(forward<Args>(args)...) {}
49
50 LIBC_INLINE constexpr void reset() {
51 if (in_use)
52 stored_value.~U();
53 in_use = false;
54 }
55 };
56
57 // The only difference is that this type U doesn't have a nontrivial
58 // destructor.
59 template <typename U> struct OptionalStorage<U, false> {
60 union {
61 char empty;
62 U stored_value;
63 };
64
65 bool in_use = false;
66
67 LIBC_INLINE constexpr OptionalStorage() : empty() {}
68
69 template <typename... Args>
70 LIBC_INLINE constexpr explicit OptionalStorage(in_place_t, Args &&...args)
71 : stored_value(forward<Args>(args)...) {}
72
73 LIBC_INLINE constexpr void reset() { in_use = false; }
74 };
75
76 OptionalStorage<T> storage;
77
78public:
79 LIBC_INLINE constexpr optional() = default;
80 LIBC_INLINE constexpr optional(nullopt_t) {}
81
82 LIBC_INLINE constexpr optional(const T &t) : storage(in_place, t) {
83 storage.in_use = true;
84 }
85 LIBC_INLINE constexpr optional(const optional &) = default;
86
87 LIBC_INLINE constexpr optional(T &&t) : storage(in_place, move(t)) {
88 storage.in_use = true;
89 }
90 LIBC_INLINE constexpr optional(optional &&O) = default;
91
92 template <typename... ArgTypes>
93 LIBC_INLINE constexpr optional(in_place_t, ArgTypes &&...Args)
94 : storage(in_place, forward<ArgTypes>(Args)...) {
95 storage.in_use = true;
96 }
97
98 LIBC_INLINE constexpr optional &operator=(T &&t) {
99 storage = move(t);
100 return *this;
101 }
102 LIBC_INLINE constexpr optional &operator=(optional &&) = default;
103
104 LIBC_INLINE constexpr optional &operator=(const T &t) {
105 storage = t;
106 return *this;
107 }
108 LIBC_INLINE constexpr optional &operator=(const optional &) = default;
109
110 LIBC_INLINE constexpr void reset() { storage.reset(); }
111
112 LIBC_INLINE constexpr const T &value() const & {
113 if (LIBC_UNLIKELY(!storage.in_use))
114 __builtin_trap();
115 return storage.stored_value;
116 }
117
118 LIBC_INLINE constexpr T &value() & {
119 if (LIBC_UNLIKELY(!storage.in_use))
120 __builtin_trap();
121 return storage.stored_value;
122 }
123
124 LIBC_INLINE constexpr explicit operator bool() const {
125 return storage.in_use;
126 }
127 LIBC_INLINE constexpr bool has_value() const { return storage.in_use; }
128 LIBC_INLINE constexpr const T *operator->() const {
129#ifndef NDEBUG
130 if (LIBC_UNLIKELY(!storage.in_use))
131 __builtin_trap();
132#endif
133 return &storage.stored_value;
134 }
135 LIBC_INLINE constexpr T *operator->() {
136#ifndef NDEBUG
137 if (LIBC_UNLIKELY(!storage.in_use))
138 __builtin_trap();
139#endif
140 return &storage.stored_value;
141 }
142 LIBC_INLINE constexpr const T &operator*() const & {
143#ifndef NDEBUG
144 if (LIBC_UNLIKELY(!storage.in_use))
145 __builtin_trap();
146#endif
147 return storage.stored_value;
148 }
149 LIBC_INLINE constexpr T &operator*() & {
150#ifndef NDEBUG
151 if (LIBC_UNLIKELY(!storage.in_use))
152 __builtin_trap();
153#endif
154 return storage.stored_value;
155 }
156
157 LIBC_INLINE constexpr T &&value() && {
158 if (LIBC_UNLIKELY(!storage.in_use))
159 __builtin_trap();
160 return move(storage.stored_value);
161 }
162 LIBC_INLINE constexpr T &&operator*() && {
163#ifndef NDEBUG
164 if (LIBC_UNLIKELY(!storage.in_use))
165 __builtin_trap();
166#endif
167 return move(storage.stored_value);
168 }
169
170 template <typename U>
171 LIBC_INLINE constexpr T value_or(U &&default_value) const & {
172 return has_value() ? storage.stored_value
173 : static_cast<T>(forward<U>(default_value));
174 }
175
176 template <typename U> LIBC_INLINE constexpr T value_or(U &&default_value) && {
177 return has_value() ? move(storage.stored_value)
178 : static_cast<T>(forward<U>(default_value));
179 }
180};
181
182} // namespace cpp
183} // namespace LIBC_NAMESPACE_DECL
184
185#endif // LLVM_LIBC_SRC___SUPPORT_CPP_OPTIONAL_H
186