//===- CustomizableOptional.h - Optional with custom storage ----*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef CLANG_BASIC_CUSTOMIZABLEOPTIONAL_H #define CLANG_BASIC_CUSTOMIZABLEOPTIONAL_H #include "llvm/ADT/Hashing.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/type_traits.h" #include #include #include #include namespace clang { namespace optional_detail { template class OptionalStorage; } // namespace optional_detail // Optional type which internal storage can be specialized by providing // OptionalStorage. The interface follows std::optional. template class CustomizableOptional { optional_detail::OptionalStorage Storage; public: using value_type = T; constexpr CustomizableOptional() = default; constexpr CustomizableOptional(std::nullopt_t) {} constexpr CustomizableOptional(const T &y) : Storage(std::in_place, y) {} constexpr CustomizableOptional(const CustomizableOptional &O) = default; constexpr CustomizableOptional(T &&y) : Storage(std::in_place, std::move(y)) {} constexpr CustomizableOptional(CustomizableOptional &&O) = default; template constexpr CustomizableOptional(std::in_place_t, ArgTypes &&...Args) : Storage(std::in_place, std::forward(Args)...) {} // Allow conversion from std::optional. constexpr CustomizableOptional(const std::optional &y) : CustomizableOptional(y ? *y : CustomizableOptional()) {} constexpr CustomizableOptional(std::optional &&y) : CustomizableOptional(y ? std::move(*y) : CustomizableOptional()) {} CustomizableOptional &operator=(T &&y) { Storage = std::move(y); return *this; } CustomizableOptional &operator=(CustomizableOptional &&O) = default; /// Create a new object by constructing it in place with the given arguments. template void emplace(ArgTypes &&...Args) { Storage.emplace(std::forward(Args)...); } CustomizableOptional &operator=(const T &y) { Storage = y; return *this; } CustomizableOptional &operator=(const CustomizableOptional &O) = default; void reset() { Storage.reset(); } LLVM_DEPRECATED("Use &*X instead.", "&*X") constexpr const T *getPointer() const { return &Storage.value(); } LLVM_DEPRECATED("Use &*X instead.", "&*X") T *getPointer() { return &Storage.value(); } LLVM_DEPRECATED("std::optional::value is throwing. Use *X instead", "*X") constexpr const T &value() const & { return Storage.value(); } LLVM_DEPRECATED("std::optional::value is throwing. Use *X instead", "*X") T &value() & { return Storage.value(); } constexpr explicit operator bool() const { return has_value(); } constexpr bool has_value() const { return Storage.has_value(); } constexpr const T *operator->() const { return &Storage.value(); } T *operator->() { return &Storage.value(); } constexpr const T &operator*() const & { return Storage.value(); } T &operator*() & { return Storage.value(); } template constexpr T value_or(U &&alt) const & { return has_value() ? operator*() : std::forward(alt); } LLVM_DEPRECATED("std::optional::value is throwing. Use *X instead", "*X") T &&value() && { return std::move(Storage.value()); } T &&operator*() && { return std::move(Storage.value()); } template T value_or(U &&alt) && { return has_value() ? std::move(operator*()) : std::forward(alt); } // Allow conversion to std::optional. explicit operator std::optional &() const & { return *this ? **this : std::optional(); } explicit operator std::optional &&() const && { return *this ? std::move(**this) : std::optional(); } }; template CustomizableOptional(const T &) -> CustomizableOptional; template llvm::hash_code hash_value(const CustomizableOptional &O) { return O ? llvm::hash_combine(true, *O) : llvm::hash_value(false); } template constexpr bool operator==(const CustomizableOptional &X, const CustomizableOptional &Y) { if (X && Y) return *X == *Y; return X.has_value() == Y.has_value(); } template constexpr bool operator!=(const CustomizableOptional &X, const CustomizableOptional &Y) { return !(X == Y); } template constexpr bool operator<(const CustomizableOptional &X, const CustomizableOptional &Y) { if (X && Y) return *X < *Y; return X.has_value() < Y.has_value(); } template constexpr bool operator<=(const CustomizableOptional &X, const CustomizableOptional &Y) { return !(Y < X); } template constexpr bool operator>(const CustomizableOptional &X, const CustomizableOptional &Y) { return Y < X; } template constexpr bool operator>=(const CustomizableOptional &X, const CustomizableOptional &Y) { return !(X < Y); } template constexpr bool operator==(const CustomizableOptional &X, std::nullopt_t) { return !X; } template constexpr bool operator==(std::nullopt_t, const CustomizableOptional &X) { return X == std::nullopt; } template constexpr bool operator!=(const CustomizableOptional &X, std::nullopt_t) { return !(X == std::nullopt); } template constexpr bool operator!=(std::nullopt_t, const CustomizableOptional &X) { return X != std::nullopt; } template constexpr bool operator<(const CustomizableOptional &, std::nullopt_t) { return false; } template constexpr bool operator<(std::nullopt_t, const CustomizableOptional &X) { return X.has_value(); } template constexpr bool operator<=(const CustomizableOptional &X, std::nullopt_t) { return !(std::nullopt < X); } template constexpr bool operator<=(std::nullopt_t, const CustomizableOptional &X) { return !(X < std::nullopt); } template constexpr bool operator>(const CustomizableOptional &X, std::nullopt_t) { return std::nullopt < X; } template constexpr bool operator>(std::nullopt_t, const CustomizableOptional &X) { return X < std::nullopt; } template constexpr bool operator>=(const CustomizableOptional &X, std::nullopt_t) { return std::nullopt <= X; } template constexpr bool operator>=(std::nullopt_t, const CustomizableOptional &X) { return X <= std::nullopt; } template constexpr bool operator==(const CustomizableOptional &X, const T &Y) { return X && *X == Y; } template constexpr bool operator==(const T &X, const CustomizableOptional &Y) { return Y && X == *Y; } template constexpr bool operator!=(const CustomizableOptional &X, const T &Y) { return !(X == Y); } template constexpr bool operator!=(const T &X, const CustomizableOptional &Y) { return !(X == Y); } template constexpr bool operator<(const CustomizableOptional &X, const T &Y) { return !X || *X < Y; } template constexpr bool operator<(const T &X, const CustomizableOptional &Y) { return Y && X < *Y; } template constexpr bool operator<=(const CustomizableOptional &X, const T &Y) { return !(Y < X); } template constexpr bool operator<=(const T &X, const CustomizableOptional &Y) { return !(Y < X); } template constexpr bool operator>(const CustomizableOptional &X, const T &Y) { return Y < X; } template constexpr bool operator>(const T &X, const CustomizableOptional &Y) { return Y < X; } template constexpr bool operator>=(const CustomizableOptional &X, const T &Y) { return !(X < Y); } template constexpr bool operator>=(const T &X, const CustomizableOptional &Y) { return !(X < Y); } } // namespace clang #endif // CLANG_BASIC_CUSTOMIZABLEOPTIONAL_H