Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Optional.h - Simple variant for passing optional values --*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file provides Optional, a template class modeled in the spirit of |
| 10 | // OCaml's 'opt' variant. The idea is to strongly type whether or not |
| 11 | // a value can be optional. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_ADT_OPTIONAL_H |
| 16 | #define LLVM_ADT_OPTIONAL_H |
| 17 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 18 | #include "llvm/ADT/Hashing.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/ADT/None.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | #include "llvm/Support/type_traits.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | #include <cassert> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 23 | #include <memory> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | #include <new> |
| 25 | #include <utility> |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 29 | class raw_ostream; |
| 30 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 31 | namespace optional_detail { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 32 | |
| 33 | struct in_place_t {}; |
| 34 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 35 | /// Storage for any type. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 36 | template <typename T, bool = is_trivially_copyable<T>::value> |
| 37 | class OptionalStorage { |
| 38 | union { |
| 39 | char empty; |
| 40 | T value; |
| 41 | }; |
| 42 | bool hasVal; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 43 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 44 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 45 | ~OptionalStorage() { reset(); } |
| 46 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 47 | constexpr OptionalStorage() noexcept : empty(), hasVal(false) {} |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 48 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 49 | constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 50 | if (other.hasValue()) { |
| 51 | emplace(other.value); |
| 52 | } |
| 53 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 54 | constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 55 | if (other.hasValue()) { |
| 56 | emplace(std::move(other.value)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | template <class... Args> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 61 | constexpr explicit OptionalStorage(in_place_t, Args &&... args) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 62 | : value(std::forward<Args>(args)...), hasVal(true) {} |
| 63 | |
| 64 | void reset() noexcept { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | if (hasVal) { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 66 | value.~T(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | hasVal = false; |
| 68 | } |
| 69 | } |
| 70 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 71 | constexpr bool hasValue() const noexcept { return hasVal; } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 72 | |
| 73 | T &getValue() LLVM_LVALUE_FUNCTION noexcept { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | assert(hasVal); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 75 | return value; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 77 | constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | assert(hasVal); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 79 | return value; |
| 80 | } |
| 81 | #if LLVM_HAS_RVALUE_REFERENCE_THIS |
| 82 | T &&getValue() && noexcept { |
| 83 | assert(hasVal); |
| 84 | return std::move(value); |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | template <class... Args> void emplace(Args &&... args) { |
| 89 | reset(); |
| 90 | ::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...); |
| 91 | hasVal = true; |
| 92 | } |
| 93 | |
| 94 | OptionalStorage &operator=(T const &y) { |
| 95 | if (hasValue()) { |
| 96 | value = y; |
| 97 | } else { |
| 98 | ::new ((void *)std::addressof(value)) T(y); |
| 99 | hasVal = true; |
| 100 | } |
| 101 | return *this; |
| 102 | } |
| 103 | OptionalStorage &operator=(T &&y) { |
| 104 | if (hasValue()) { |
| 105 | value = std::move(y); |
| 106 | } else { |
| 107 | ::new ((void *)std::addressof(value)) T(std::move(y)); |
| 108 | hasVal = true; |
| 109 | } |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | OptionalStorage &operator=(OptionalStorage const &other) { |
| 114 | if (other.hasValue()) { |
| 115 | if (hasValue()) { |
| 116 | value = other.value; |
| 117 | } else { |
| 118 | ::new ((void *)std::addressof(value)) T(other.value); |
| 119 | hasVal = true; |
| 120 | } |
| 121 | } else { |
| 122 | reset(); |
| 123 | } |
| 124 | return *this; |
| 125 | } |
| 126 | |
| 127 | OptionalStorage &operator=(OptionalStorage &&other) { |
| 128 | if (other.hasValue()) { |
| 129 | if (hasValue()) { |
| 130 | value = std::move(other.value); |
| 131 | } else { |
| 132 | ::new ((void *)std::addressof(value)) T(std::move(other.value)); |
| 133 | hasVal = true; |
| 134 | } |
| 135 | } else { |
| 136 | reset(); |
| 137 | } |
| 138 | return *this; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | template <typename T> class OptionalStorage<T, true> { |
| 143 | union { |
| 144 | char empty; |
| 145 | T value; |
| 146 | }; |
| 147 | bool hasVal = false; |
| 148 | |
| 149 | public: |
| 150 | ~OptionalStorage() = default; |
| 151 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 152 | constexpr OptionalStorage() noexcept : empty{} {} |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 153 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 154 | constexpr OptionalStorage(OptionalStorage const &other) = default; |
| 155 | constexpr OptionalStorage(OptionalStorage &&other) = default; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 156 | |
| 157 | OptionalStorage &operator=(OptionalStorage const &other) = default; |
| 158 | OptionalStorage &operator=(OptionalStorage &&other) = default; |
| 159 | |
| 160 | template <class... Args> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 161 | constexpr explicit OptionalStorage(in_place_t, Args &&... args) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 162 | : value(std::forward<Args>(args)...), hasVal(true) {} |
| 163 | |
| 164 | void reset() noexcept { |
| 165 | if (hasVal) { |
| 166 | value.~T(); |
| 167 | hasVal = false; |
| 168 | } |
| 169 | } |
| 170 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 171 | constexpr bool hasValue() const noexcept { return hasVal; } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 172 | |
| 173 | T &getValue() LLVM_LVALUE_FUNCTION noexcept { |
| 174 | assert(hasVal); |
| 175 | return value; |
| 176 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 177 | constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 178 | assert(hasVal); |
| 179 | return value; |
| 180 | } |
| 181 | #if LLVM_HAS_RVALUE_REFERENCE_THIS |
| 182 | T &&getValue() && noexcept { |
| 183 | assert(hasVal); |
| 184 | return std::move(value); |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | template <class... Args> void emplace(Args &&... args) { |
| 189 | reset(); |
| 190 | ::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...); |
| 191 | hasVal = true; |
| 192 | } |
| 193 | |
| 194 | OptionalStorage &operator=(T const &y) { |
| 195 | if (hasValue()) { |
| 196 | value = y; |
| 197 | } else { |
| 198 | ::new ((void *)std::addressof(value)) T(y); |
| 199 | hasVal = true; |
| 200 | } |
| 201 | return *this; |
| 202 | } |
| 203 | OptionalStorage &operator=(T &&y) { |
| 204 | if (hasValue()) { |
| 205 | value = std::move(y); |
| 206 | } else { |
| 207 | ::new ((void *)std::addressof(value)) T(std::move(y)); |
| 208 | hasVal = true; |
| 209 | } |
| 210 | return *this; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 214 | } // namespace optional_detail |
| 215 | |
| 216 | template <typename T> class Optional { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 217 | optional_detail::OptionalStorage<T> Storage; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 218 | |
| 219 | public: |
| 220 | using value_type = T; |
| 221 | |
| 222 | constexpr Optional() {} |
| 223 | constexpr Optional(NoneType) {} |
| 224 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 225 | constexpr Optional(const T &y) : Storage(optional_detail::in_place_t{}, y) {} |
| 226 | constexpr Optional(const Optional &O) = default; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 227 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 228 | constexpr Optional(T &&y) |
| 229 | : Storage(optional_detail::in_place_t{}, std::move(y)) {} |
| 230 | constexpr Optional(Optional &&O) = default; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 231 | |
| 232 | Optional &operator=(T &&y) { |
| 233 | Storage = std::move(y); |
| 234 | return *this; |
| 235 | } |
| 236 | Optional &operator=(Optional &&O) = default; |
| 237 | |
| 238 | /// Create a new object by constructing it in place with the given arguments. |
| 239 | template <typename... ArgTypes> void emplace(ArgTypes &&... Args) { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 240 | Storage.emplace(std::forward<ArgTypes>(Args)...); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 243 | static constexpr Optional create(const T *y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 244 | return y ? Optional(*y) : Optional(); |
| 245 | } |
| 246 | |
| 247 | Optional &operator=(const T &y) { |
| 248 | Storage = y; |
| 249 | return *this; |
| 250 | } |
| 251 | Optional &operator=(const Optional &O) = default; |
| 252 | |
| 253 | void reset() { Storage.reset(); } |
| 254 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 255 | constexpr const T *getPointer() const { return &Storage.getValue(); } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 256 | T *getPointer() { return &Storage.getValue(); } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 257 | constexpr const T &getValue() const LLVM_LVALUE_FUNCTION { |
| 258 | return Storage.getValue(); |
| 259 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 260 | T &getValue() LLVM_LVALUE_FUNCTION { return Storage.getValue(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 261 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 262 | constexpr explicit operator bool() const { return hasValue(); } |
| 263 | constexpr bool hasValue() const { return Storage.hasValue(); } |
| 264 | constexpr const T *operator->() const { return getPointer(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 265 | T *operator->() { return getPointer(); } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 266 | constexpr const T &operator*() const LLVM_LVALUE_FUNCTION { |
| 267 | return getValue(); |
| 268 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 269 | T &operator*() LLVM_LVALUE_FUNCTION { return getValue(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 270 | |
| 271 | template <typename U> |
| 272 | constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION { |
| 273 | return hasValue() ? getValue() : std::forward<U>(value); |
| 274 | } |
| 275 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 276 | /// Apply a function to the value if present; otherwise return None. |
| 277 | template <class Function> |
| 278 | auto map(const Function &F) const LLVM_LVALUE_FUNCTION |
| 279 | -> Optional<decltype(F(getValue()))> { |
| 280 | if (*this) return F(getValue()); |
| 281 | return None; |
| 282 | } |
| 283 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 284 | #if LLVM_HAS_RVALUE_REFERENCE_THIS |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 285 | T &&getValue() && { return std::move(Storage.getValue()); } |
| 286 | T &&operator*() && { return std::move(Storage.getValue()); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 287 | |
| 288 | template <typename U> |
| 289 | T getValueOr(U &&value) && { |
| 290 | return hasValue() ? std::move(getValue()) : std::forward<U>(value); |
| 291 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 292 | |
| 293 | /// Apply a function to the value if present; otherwise return None. |
| 294 | template <class Function> |
| 295 | auto map(const Function &F) && |
| 296 | -> Optional<decltype(F(std::move(*this).getValue()))> { |
| 297 | if (*this) return F(std::move(*this).getValue()); |
| 298 | return None; |
| 299 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 300 | #endif |
| 301 | }; |
| 302 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 303 | template <class T> llvm::hash_code hash_value(const Optional<T> &O) { |
| 304 | return O ? hash_combine(true, *O) : hash_value(false); |
| 305 | } |
| 306 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 307 | template <typename T, typename U> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 308 | constexpr bool operator==(const Optional<T> &X, const Optional<U> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 309 | if (X && Y) |
| 310 | return *X == *Y; |
| 311 | return X.hasValue() == Y.hasValue(); |
| 312 | } |
| 313 | |
| 314 | template <typename T, typename U> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 315 | constexpr bool operator!=(const Optional<T> &X, const Optional<U> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 316 | return !(X == Y); |
| 317 | } |
| 318 | |
| 319 | template <typename T, typename U> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 320 | constexpr bool operator<(const Optional<T> &X, const Optional<U> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 321 | if (X && Y) |
| 322 | return *X < *Y; |
| 323 | return X.hasValue() < Y.hasValue(); |
| 324 | } |
| 325 | |
| 326 | template <typename T, typename U> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 327 | constexpr bool operator<=(const Optional<T> &X, const Optional<U> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 328 | return !(Y < X); |
| 329 | } |
| 330 | |
| 331 | template <typename T, typename U> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 332 | constexpr bool operator>(const Optional<T> &X, const Optional<U> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 333 | return Y < X; |
| 334 | } |
| 335 | |
| 336 | template <typename T, typename U> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 337 | constexpr bool operator>=(const Optional<T> &X, const Optional<U> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 338 | return !(X < Y); |
| 339 | } |
| 340 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 341 | template <typename T> |
| 342 | constexpr bool operator==(const Optional<T> &X, NoneType) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 343 | return !X; |
| 344 | } |
| 345 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 346 | template <typename T> |
| 347 | constexpr bool operator==(NoneType, const Optional<T> &X) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 348 | return X == None; |
| 349 | } |
| 350 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 351 | template <typename T> |
| 352 | constexpr bool operator!=(const Optional<T> &X, NoneType) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 353 | return !(X == None); |
| 354 | } |
| 355 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 356 | template <typename T> |
| 357 | constexpr bool operator!=(NoneType, const Optional<T> &X) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 358 | return X != None; |
| 359 | } |
| 360 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 361 | template <typename T> constexpr bool operator<(const Optional<T> &X, NoneType) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 365 | template <typename T> constexpr bool operator<(NoneType, const Optional<T> &X) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 366 | return X.hasValue(); |
| 367 | } |
| 368 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 369 | template <typename T> |
| 370 | constexpr bool operator<=(const Optional<T> &X, NoneType) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 371 | return !(None < X); |
| 372 | } |
| 373 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 374 | template <typename T> |
| 375 | constexpr bool operator<=(NoneType, const Optional<T> &X) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 376 | return !(X < None); |
| 377 | } |
| 378 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 379 | template <typename T> constexpr bool operator>(const Optional<T> &X, NoneType) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 380 | return None < X; |
| 381 | } |
| 382 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 383 | template <typename T> constexpr bool operator>(NoneType, const Optional<T> &X) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 384 | return X < None; |
| 385 | } |
| 386 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 387 | template <typename T> |
| 388 | constexpr bool operator>=(const Optional<T> &X, NoneType) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 389 | return None <= X; |
| 390 | } |
| 391 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 392 | template <typename T> |
| 393 | constexpr bool operator>=(NoneType, const Optional<T> &X) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 394 | return X <= None; |
| 395 | } |
| 396 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 397 | template <typename T> |
| 398 | constexpr bool operator==(const Optional<T> &X, const T &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 399 | return X && *X == Y; |
| 400 | } |
| 401 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 402 | template <typename T> |
| 403 | constexpr bool operator==(const T &X, const Optional<T> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 404 | return Y && X == *Y; |
| 405 | } |
| 406 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 407 | template <typename T> |
| 408 | constexpr bool operator!=(const Optional<T> &X, const T &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 409 | return !(X == Y); |
| 410 | } |
| 411 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 412 | template <typename T> |
| 413 | constexpr bool operator!=(const T &X, const Optional<T> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 414 | return !(X == Y); |
| 415 | } |
| 416 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 417 | template <typename T> |
| 418 | constexpr bool operator<(const Optional<T> &X, const T &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 419 | return !X || *X < Y; |
| 420 | } |
| 421 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 422 | template <typename T> |
| 423 | constexpr bool operator<(const T &X, const Optional<T> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 424 | return Y && X < *Y; |
| 425 | } |
| 426 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 427 | template <typename T> |
| 428 | constexpr bool operator<=(const Optional<T> &X, const T &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 429 | return !(Y < X); |
| 430 | } |
| 431 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 432 | template <typename T> |
| 433 | constexpr bool operator<=(const T &X, const Optional<T> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 434 | return !(Y < X); |
| 435 | } |
| 436 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 437 | template <typename T> |
| 438 | constexpr bool operator>(const Optional<T> &X, const T &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 439 | return Y < X; |
| 440 | } |
| 441 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 442 | template <typename T> |
| 443 | constexpr bool operator>(const T &X, const Optional<T> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 444 | return Y < X; |
| 445 | } |
| 446 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 447 | template <typename T> |
| 448 | constexpr bool operator>=(const Optional<T> &X, const T &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 449 | return !(X < Y); |
| 450 | } |
| 451 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 452 | template <typename T> |
| 453 | constexpr bool operator>=(const T &X, const Optional<T> &Y) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 454 | return !(X < Y); |
| 455 | } |
| 456 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 457 | raw_ostream &operator<<(raw_ostream &OS, NoneType); |
| 458 | |
| 459 | template <typename T, typename = decltype(std::declval<raw_ostream &>() |
| 460 | << std::declval<const T &>())> |
| 461 | raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) { |
| 462 | if (O) |
| 463 | OS << *O; |
| 464 | else |
| 465 | OS << None; |
| 466 | return OS; |
| 467 | } |
| 468 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 469 | } // end namespace llvm |
| 470 | |
| 471 | #endif // LLVM_ADT_OPTIONAL_H |