clang 19.0.0git
CustomizableOptional.h
Go to the documentation of this file.
1//===- CustomizableOptional.h - Optional with custom storage ----*- 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 CLANG_BASIC_CUSTOMIZABLEOPTIONAL_H
10#define CLANG_BASIC_CUSTOMIZABLEOPTIONAL_H
11
12#include "llvm/ADT/Hashing.h"
13#include "llvm/Support/Compiler.h"
14#include "llvm/Support/type_traits.h"
15#include <cassert>
16#include <new>
17#include <optional>
18#include <utility>
19
20namespace clang {
21
22namespace optional_detail {
23template <typename> class OptionalStorage;
24} // namespace optional_detail
25
26// Optional type which internal storage can be specialized by providing
27// OptionalStorage. The interface follows std::optional.
28template <typename T> class CustomizableOptional {
30
31public:
32 using value_type = T;
33
34 constexpr CustomizableOptional() = default;
35 constexpr CustomizableOptional(std::nullopt_t) {}
36
37 constexpr CustomizableOptional(const T &y) : Storage(std::in_place, y) {}
38 constexpr CustomizableOptional(const CustomizableOptional &O) = default;
39
40 constexpr CustomizableOptional(T &&y)
41 : Storage(std::in_place, std::move(y)) {}
43
44 template <typename... ArgTypes>
45 constexpr CustomizableOptional(std::in_place_t, ArgTypes &&...Args)
46 : Storage(std::in_place, std::forward<ArgTypes>(Args)...) {}
47
48 // Allow conversion from std::optional<T>.
49 constexpr CustomizableOptional(const std::optional<T> &y)
51 constexpr CustomizableOptional(std::optional<T> &&y)
52 : CustomizableOptional(y ? std::move(*y) : CustomizableOptional()) {}
53
55 Storage = std::move(y);
56 return *this;
57 }
59
60 /// Create a new object by constructing it in place with the given arguments.
61 template <typename... ArgTypes> void emplace(ArgTypes &&...Args) {
62 Storage.emplace(std::forward<ArgTypes>(Args)...);
63 }
64
66 Storage = y;
67 return *this;
68 }
70
71 void reset() { Storage.reset(); }
72
73 LLVM_DEPRECATED("Use &*X instead.", "&*X")
74 constexpr const T *getPointer() const { return &Storage.value(); }
75 LLVM_DEPRECATED("Use &*X instead.", "&*X")
76 T *getPointer() { return &Storage.value(); }
77 LLVM_DEPRECATED("std::optional::value is throwing. Use *X instead", "*X")
78 constexpr const T &value() const & { return Storage.value(); }
79 LLVM_DEPRECATED("std::optional::value is throwing. Use *X instead", "*X")
80 T &value() & { return Storage.value(); }
81
82 constexpr explicit operator bool() const { return has_value(); }
83 constexpr bool has_value() const { return Storage.has_value(); }
84 constexpr const T *operator->() const { return &Storage.value(); }
85 T *operator->() { return &Storage.value(); }
86 constexpr const T &operator*() const & { return Storage.value(); }
87 T &operator*() & { return Storage.value(); }
88
89 template <typename U> constexpr T value_or(U &&alt) const & {
90 return has_value() ? operator*() : std::forward<U>(alt);
91 }
92
93 LLVM_DEPRECATED("std::optional::value is throwing. Use *X instead", "*X")
94 T &&value() && { return std::move(Storage.value()); }
95 T &&operator*() && { return std::move(Storage.value()); }
96
97 template <typename U> T value_or(U &&alt) && {
98 return has_value() ? std::move(operator*()) : std::forward<U>(alt);
99 }
100
101 // Allow conversion to std::optional<T>.
102 explicit operator std::optional<T> &() const & {
103 return *this ? **this : std::optional<T>();
104 }
105 explicit operator std::optional<T> &&() const && {
106 return *this ? std::move(**this) : std::optional<T>();
107 }
108};
109
110template <typename T>
112
113template <class T>
114llvm::hash_code hash_value(const CustomizableOptional<T> &O) {
115 return O ? llvm::hash_combine(true, *O) : llvm::hash_value(false);
116}
117
118template <typename T, typename U>
120 const CustomizableOptional<U> &Y) {
121 if (X && Y)
122 return *X == *Y;
123 return X.has_value() == Y.has_value();
124}
125
126template <typename T, typename U>
128 const CustomizableOptional<U> &Y) {
129 return !(X == Y);
130}
131
132template <typename T, typename U>
133constexpr bool operator<(const CustomizableOptional<T> &X,
134 const CustomizableOptional<U> &Y) {
135 if (X && Y)
136 return *X < *Y;
137 return X.has_value() < Y.has_value();
138}
139
140template <typename T, typename U>
142 const CustomizableOptional<U> &Y) {
143 return !(Y < X);
144}
145
146template <typename T, typename U>
147constexpr bool operator>(const CustomizableOptional<T> &X,
148 const CustomizableOptional<U> &Y) {
149 return Y < X;
150}
151
152template <typename T, typename U>
154 const CustomizableOptional<U> &Y) {
155 return !(X < Y);
156}
157
158template <typename T>
159constexpr bool operator==(const CustomizableOptional<T> &X, std::nullopt_t) {
160 return !X;
161}
162
163template <typename T>
164constexpr bool operator==(std::nullopt_t, const CustomizableOptional<T> &X) {
165 return X == std::nullopt;
166}
167
168template <typename T>
169constexpr bool operator!=(const CustomizableOptional<T> &X, std::nullopt_t) {
170 return !(X == std::nullopt);
171}
172
173template <typename T>
174constexpr bool operator!=(std::nullopt_t, const CustomizableOptional<T> &X) {
175 return X != std::nullopt;
176}
177
178template <typename T>
179constexpr bool operator<(const CustomizableOptional<T> &, std::nullopt_t) {
180 return false;
181}
182
183template <typename T>
184constexpr bool operator<(std::nullopt_t, const CustomizableOptional<T> &X) {
185 return X.has_value();
186}
187
188template <typename T>
189constexpr bool operator<=(const CustomizableOptional<T> &X, std::nullopt_t) {
190 return !(std::nullopt < X);
191}
192
193template <typename T>
194constexpr bool operator<=(std::nullopt_t, const CustomizableOptional<T> &X) {
195 return !(X < std::nullopt);
196}
197
198template <typename T>
199constexpr bool operator>(const CustomizableOptional<T> &X, std::nullopt_t) {
200 return std::nullopt < X;
201}
202
203template <typename T>
204constexpr bool operator>(std::nullopt_t, const CustomizableOptional<T> &X) {
205 return X < std::nullopt;
206}
207
208template <typename T>
209constexpr bool operator>=(const CustomizableOptional<T> &X, std::nullopt_t) {
210 return std::nullopt <= X;
211}
212
213template <typename T>
214constexpr bool operator>=(std::nullopt_t, const CustomizableOptional<T> &X) {
215 return X <= std::nullopt;
216}
217
218template <typename T>
219constexpr bool operator==(const CustomizableOptional<T> &X, const T &Y) {
220 return X && *X == Y;
221}
222
223template <typename T>
224constexpr bool operator==(const T &X, const CustomizableOptional<T> &Y) {
225 return Y && X == *Y;
226}
227
228template <typename T>
229constexpr bool operator!=(const CustomizableOptional<T> &X, const T &Y) {
230 return !(X == Y);
231}
232
233template <typename T>
234constexpr bool operator!=(const T &X, const CustomizableOptional<T> &Y) {
235 return !(X == Y);
236}
237
238template <typename T>
239constexpr bool operator<(const CustomizableOptional<T> &X, const T &Y) {
240 return !X || *X < Y;
241}
242
243template <typename T>
244constexpr bool operator<(const T &X, const CustomizableOptional<T> &Y) {
245 return Y && X < *Y;
246}
247
248template <typename T>
249constexpr bool operator<=(const CustomizableOptional<T> &X, const T &Y) {
250 return !(Y < X);
251}
252
253template <typename T>
254constexpr bool operator<=(const T &X, const CustomizableOptional<T> &Y) {
255 return !(Y < X);
256}
257
258template <typename T>
259constexpr bool operator>(const CustomizableOptional<T> &X, const T &Y) {
260 return Y < X;
261}
262
263template <typename T>
264constexpr bool operator>(const T &X, const CustomizableOptional<T> &Y) {
265 return Y < X;
266}
267
268template <typename T>
269constexpr bool operator>=(const CustomizableOptional<T> &X, const T &Y) {
270 return !(X < Y);
271}
272
273template <typename T>
274constexpr bool operator>=(const T &X, const CustomizableOptional<T> &Y) {
275 return !(X < Y);
276}
277
278} // namespace clang
279
280#endif // CLANG_BASIC_CUSTOMIZABLEOPTIONAL_H
#define X(type, name)
Definition: Value.h:142
constexpr CustomizableOptional(std::in_place_t, ArgTypes &&...Args)
constexpr T value_or(U &&alt) const &
CustomizableOptional & operator=(const CustomizableOptional &O)=default
void emplace(ArgTypes &&...Args)
Create a new object by constructing it in place with the given arguments.
constexpr bool has_value() const
constexpr CustomizableOptional(const CustomizableOptional &O)=default
constexpr CustomizableOptional(std::nullopt_t)
constexpr const T * operator->() const
CustomizableOptional & operator=(const T &y)
constexpr CustomizableOptional(std::optional< T > &&y)
constexpr const T & operator*() const &
CustomizableOptional & operator=(CustomizableOptional &&O)=default
CustomizableOptional & operator=(T &&y)
constexpr CustomizableOptional(const std::optional< T > &y)
constexpr const T & value() const &
constexpr CustomizableOptional()=default
constexpr CustomizableOptional(CustomizableOptional &&O)=default
constexpr const T * getPointer() const
constexpr CustomizableOptional(const T &y)
Code completion in a.
The JSON file list parser is used to communicate input to InstallAPI.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition: CallGraph.h:207
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
bool operator!=(CanQual< T > x, CanQual< U > y)
bool operator<=(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
llvm::hash_code hash_value(const CustomizableOptional< T > &O)
bool operator>(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
bool operator>=(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
hash_code hash_value(const clang::tooling::dependencies::ModuleID &ID)
Definition: Format.h:5304
#define bool
Definition: stdbool.h:20