2017-06-14 23:42:24 +02:00
|
|
|
//===- Optional.h - Simple variant for passing optional values --*- C++ -*-===//
|
2010-04-09 22:25:54 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2010-04-09 22:25:54 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides Optional, a template class modeled in the spirit of
|
|
|
|
// OCaml's 'opt' variant. The idea is to strongly type whether or not
|
|
|
|
// a value can be optional.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_ADT_OPTIONAL_H
|
|
|
|
#define LLVM_ADT_OPTIONAL_H
|
2010-04-09 22:25:54 +02:00
|
|
|
|
2020-12-04 04:26:56 +01:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2013-02-21 01:27:28 +01:00
|
|
|
#include "llvm/ADT/None.h"
|
2021-04-06 00:59:09 +02:00
|
|
|
#include "llvm/ADT/STLForwardCompat.h"
|
2020-01-22 18:36:48 +01:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2020-12-02 22:48:05 +01:00
|
|
|
#include "llvm/Support/type_traits.h"
|
2010-04-09 22:25:54 +02:00
|
|
|
#include <cassert>
|
2019-02-20 15:56:31 +01:00
|
|
|
#include <memory>
|
2014-10-01 04:12:35 +02:00
|
|
|
#include <new>
|
2012-10-19 00:22:55 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2010-04-09 22:25:54 +02:00
|
|
|
namespace llvm {
|
|
|
|
|
2019-01-18 13:52:03 +01:00
|
|
|
class raw_ostream;
|
|
|
|
|
2018-01-18 12:26:24 +01:00
|
|
|
namespace optional_detail {
|
2019-02-18 09:46:32 +01:00
|
|
|
|
2019-02-13 23:11:09 +01:00
|
|
|
/// Storage for any type.
|
Fix llvm::Optional build breaks in MSVC using std::is_trivially_copyable
Current code breaks this version of MSVC due to a mismatch between `std::is_trivially_copyable` and `llvm::is_trivially_copyable` for `std::pair` instantiations. Hence I was attempting to use `std::is_trivially_copyable` to set `llvm::is_trivially_copyable<T>::value`.
I spent some time root causing an `llvm::Optional` build error on MSVC 16.8.3 related to the change described above:
```
62>C:\src\ocg_llvm\llvm-project\llvm\include\llvm/ADT/BreadthFirstIterator.h(96,12): error C2280: 'llvm::Optional<std::pair<std::pair<unsigned int,llvm::Graph<4>::NodeSubset> *,llvm::Optional<llvm::Graph<4>::ChildIterator>>> &llvm::Optional<std::pair<std::pair<unsigned int,llvm::Graph<4>::NodeSubset> *,llvm::Optional<llvm::Graph<4>::ChildIterator>>>::operator =(const llvm::Optional<std::pair<std::pair<unsigned int,llvm::Graph<4>::NodeSubset> *,llvm::Optional<llvm::Graph<4>::ChildIterator>>> &)': attempting to reference a deleted function (compiling source file C:\src\ocg_llvm\llvm-project\llvm\unittests\ADT\BreadthFirstIteratorTest.cpp)
...
```
The "trivial" specialization of `optional_detail::OptionalStorage` assumes that the value type is trivially copy constructible and trivially copy assignable. The specialization is invoked based on a check of `is_trivially_copyable` alone, which does not imply both `is_trivially_copy_assignable` and `is_trivially_copy_constructible` are true.
[[ https://en.cppreference.com/w/cpp/named_req/TriviallyCopyable | According to the spec ]], a deleted assignment operator does not make `is_trivially_copyable` false. So I think all these properties need to be checked explicitly in order to specialize `OptionalStorage` to the "trivial" version:
```
/// Storage for any type.
template <typename T, bool = std::is_trivially_copy_constructible<T>::value
&& std::is_trivially_copy_assignable<T>::value>
class OptionalStorage {
```
Above fixed my build break in MSVC, but I think we need to explicitly check `is_trivially_copy_constructible` too since it might be possible the copy constructor is deleted. Also would be ideal to move over to `std::is_trivially_copyable` instead of the `llvm` namespace verson.
Reviewed By: dblaikie
Differential Revision: https://reviews.llvm.org/D93510
2021-01-16 15:34:20 +01:00
|
|
|
//
|
|
|
|
// The specialization condition intentionally uses
|
|
|
|
// llvm::is_trivially_copy_constructible instead of
|
|
|
|
// std::is_trivially_copy_constructible. GCC versions prior to 7.4 may
|
|
|
|
// instantiate the copy constructor of `T` when
|
|
|
|
// std::is_trivially_copy_constructible is instantiated. This causes
|
|
|
|
// compilation to fail if we query the trivially copy constructible property of
|
|
|
|
// a class which is not copy constructible.
|
|
|
|
//
|
|
|
|
// The current implementation of OptionalStorage insists that in order to use
|
|
|
|
// the trivial specialization, the value_type must be trivially copy
|
|
|
|
// constructible and trivially copy assignable due to =default implementations
|
|
|
|
// of the copy/move constructor/assignment. It does not follow that this is
|
|
|
|
// necessarily the case std::is_trivially_copyable is true (hence the expanded
|
|
|
|
// specialization condition).
|
|
|
|
//
|
|
|
|
// The move constructible / assignable conditions emulate the remaining behavior
|
|
|
|
// of std::is_trivially_copyable.
|
|
|
|
template <typename T, bool = (llvm::is_trivially_copy_constructible<T>::value &&
|
|
|
|
std::is_trivially_copy_assignable<T>::value &&
|
|
|
|
(std::is_trivially_move_constructible<T>::value ||
|
|
|
|
!std::is_move_constructible<T>::value) &&
|
|
|
|
(std::is_trivially_move_assignable<T>::value ||
|
|
|
|
!std::is_move_assignable<T>::value))>
|
2019-02-18 09:46:32 +01:00
|
|
|
class OptionalStorage {
|
|
|
|
union {
|
|
|
|
char empty;
|
|
|
|
T value;
|
|
|
|
};
|
|
|
|
bool hasVal;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~OptionalStorage() { reset(); }
|
2019-02-13 10:31:22 +01:00
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr OptionalStorage() noexcept : empty(), hasVal(false) {}
|
2019-02-13 19:12:04 +01:00
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
|
2019-02-18 09:46:32 +01:00
|
|
|
if (other.hasValue()) {
|
|
|
|
emplace(other.value);
|
|
|
|
}
|
2019-02-18 00:01:41 +01:00
|
|
|
}
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
|
2019-02-18 09:46:32 +01:00
|
|
|
if (other.hasValue()) {
|
|
|
|
emplace(std::move(other.value));
|
2019-02-18 00:01:41 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 23:42:24 +02:00
|
|
|
|
2019-02-18 09:46:32 +01:00
|
|
|
template <class... Args>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr explicit OptionalStorage(in_place_t, Args &&... args)
|
2019-02-18 09:46:32 +01:00
|
|
|
: value(std::forward<Args>(args)...), hasVal(true) {}
|
|
|
|
|
|
|
|
void reset() noexcept {
|
|
|
|
if (hasVal) {
|
|
|
|
value.~T();
|
|
|
|
hasVal = false;
|
2013-02-21 08:55:39 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 09:46:32 +01:00
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool hasValue() const noexcept { return hasVal; }
|
2019-02-18 09:46:32 +01:00
|
|
|
|
2020-01-22 18:36:48 +01:00
|
|
|
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
|
2019-02-18 09:46:32 +01:00
|
|
|
assert(hasVal);
|
|
|
|
return value;
|
|
|
|
}
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
|
2019-02-18 09:46:32 +01:00
|
|
|
assert(hasVal);
|
|
|
|
return value;
|
|
|
|
}
|
2020-01-22 18:36:48 +01:00
|
|
|
#if LLVM_HAS_RVALUE_REFERENCE_THIS
|
2019-02-18 09:46:32 +01:00
|
|
|
T &&getValue() && noexcept {
|
|
|
|
assert(hasVal);
|
|
|
|
return std::move(value);
|
|
|
|
}
|
2020-01-22 18:36:48 +01:00
|
|
|
#endif
|
2019-02-18 09:46:32 +01:00
|
|
|
|
|
|
|
template <class... Args> void emplace(Args &&... args) {
|
|
|
|
reset();
|
|
|
|
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
|
|
|
|
hasVal = true;
|
2013-02-21 08:55:39 +01:00
|
|
|
}
|
2012-10-19 00:22:55 +02:00
|
|
|
|
2019-02-18 09:46:32 +01:00
|
|
|
OptionalStorage &operator=(T const &y) {
|
|
|
|
if (hasValue()) {
|
|
|
|
value = y;
|
|
|
|
} else {
|
|
|
|
::new ((void *)std::addressof(value)) T(y);
|
2019-02-18 00:01:41 +01:00
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2019-02-18 09:46:32 +01:00
|
|
|
OptionalStorage &operator=(T &&y) {
|
|
|
|
if (hasValue()) {
|
|
|
|
value = std::move(y);
|
|
|
|
} else {
|
|
|
|
::new ((void *)std::addressof(value)) T(std::move(y));
|
|
|
|
hasVal = true;
|
|
|
|
}
|
2019-02-18 00:01:41 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-02-18 09:46:32 +01:00
|
|
|
OptionalStorage &operator=(OptionalStorage const &other) {
|
|
|
|
if (other.hasValue()) {
|
|
|
|
if (hasValue()) {
|
|
|
|
value = other.value;
|
|
|
|
} else {
|
|
|
|
::new ((void *)std::addressof(value)) T(other.value);
|
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reset();
|
2013-02-20 01:26:04 +01:00
|
|
|
}
|
2019-02-18 09:46:32 +01:00
|
|
|
return *this;
|
2013-02-20 01:26:04 +01:00
|
|
|
}
|
2019-02-13 12:35:45 +01:00
|
|
|
|
2019-02-18 09:46:32 +01:00
|
|
|
OptionalStorage &operator=(OptionalStorage &&other) {
|
|
|
|
if (other.hasValue()) {
|
|
|
|
if (hasValue()) {
|
|
|
|
value = std::move(other.value);
|
|
|
|
} else {
|
|
|
|
::new ((void *)std::addressof(value)) T(std::move(other.value));
|
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
return *this;
|
2019-02-13 12:35:45 +01:00
|
|
|
}
|
2018-01-18 12:26:24 +01:00
|
|
|
};
|
|
|
|
|
2019-02-18 13:07:12 +01:00
|
|
|
template <typename T> class OptionalStorage<T, true> {
|
|
|
|
union {
|
|
|
|
char empty;
|
|
|
|
T value;
|
|
|
|
};
|
|
|
|
bool hasVal = false;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~OptionalStorage() = default;
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr OptionalStorage() noexcept : empty{} {}
|
2019-02-18 13:07:12 +01:00
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr OptionalStorage(OptionalStorage const &other) = default;
|
|
|
|
constexpr OptionalStorage(OptionalStorage &&other) = default;
|
2019-02-18 13:07:12 +01:00
|
|
|
|
|
|
|
OptionalStorage &operator=(OptionalStorage const &other) = default;
|
|
|
|
OptionalStorage &operator=(OptionalStorage &&other) = default;
|
|
|
|
|
|
|
|
template <class... Args>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr explicit OptionalStorage(in_place_t, Args &&... args)
|
2019-02-18 13:07:12 +01:00
|
|
|
: value(std::forward<Args>(args)...), hasVal(true) {}
|
|
|
|
|
|
|
|
void reset() noexcept {
|
|
|
|
if (hasVal) {
|
|
|
|
value.~T();
|
|
|
|
hasVal = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool hasValue() const noexcept { return hasVal; }
|
2019-02-18 13:07:12 +01:00
|
|
|
|
2020-01-22 18:36:48 +01:00
|
|
|
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
|
2019-02-18 13:07:12 +01:00
|
|
|
assert(hasVal);
|
|
|
|
return value;
|
|
|
|
}
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
|
2019-02-18 13:07:12 +01:00
|
|
|
assert(hasVal);
|
|
|
|
return value;
|
|
|
|
}
|
2020-01-22 18:36:48 +01:00
|
|
|
#if LLVM_HAS_RVALUE_REFERENCE_THIS
|
2019-02-18 13:07:12 +01:00
|
|
|
T &&getValue() && noexcept {
|
|
|
|
assert(hasVal);
|
|
|
|
return std::move(value);
|
|
|
|
}
|
2020-01-22 18:36:48 +01:00
|
|
|
#endif
|
2019-02-18 13:07:12 +01:00
|
|
|
|
|
|
|
template <class... Args> void emplace(Args &&... args) {
|
|
|
|
reset();
|
|
|
|
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
|
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStorage &operator=(T const &y) {
|
|
|
|
if (hasValue()) {
|
|
|
|
value = y;
|
|
|
|
} else {
|
|
|
|
::new ((void *)std::addressof(value)) T(y);
|
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
OptionalStorage &operator=(T &&y) {
|
|
|
|
if (hasValue()) {
|
|
|
|
value = std::move(y);
|
|
|
|
} else {
|
|
|
|
::new ((void *)std::addressof(value)) T(std::move(y));
|
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-18 12:26:24 +01:00
|
|
|
} // namespace optional_detail
|
|
|
|
|
|
|
|
template <typename T> class Optional {
|
2019-02-13 12:35:45 +01:00
|
|
|
optional_detail::OptionalStorage<T> Storage;
|
2018-01-18 12:26:24 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
constexpr Optional() {}
|
|
|
|
constexpr Optional(NoneType) {}
|
|
|
|
|
2021-04-06 00:59:09 +02:00
|
|
|
constexpr Optional(const T &y) : Storage(in_place, y) {}
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr Optional(const Optional &O) = default;
|
2018-01-18 12:26:24 +01:00
|
|
|
|
2021-04-06 00:59:09 +02:00
|
|
|
constexpr Optional(T &&y) : Storage(in_place, std::move(y)) {}
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr Optional(Optional &&O) = default;
|
2018-01-18 12:26:24 +01:00
|
|
|
|
2021-04-06 00:59:09 +02:00
|
|
|
template <typename... ArgTypes>
|
|
|
|
constexpr Optional(in_place_t, ArgTypes &&...Args)
|
|
|
|
: Storage(in_place, std::forward<ArgTypes>(Args)...) {}
|
|
|
|
|
2018-01-18 12:26:24 +01:00
|
|
|
Optional &operator=(T &&y) {
|
|
|
|
Storage = std::move(y);
|
|
|
|
return *this;
|
2017-11-01 06:14:35 +01:00
|
|
|
}
|
2018-01-18 12:26:24 +01:00
|
|
|
Optional &operator=(Optional &&O) = default;
|
|
|
|
|
|
|
|
/// Create a new object by constructing it in place with the given arguments.
|
|
|
|
template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
|
2019-02-18 09:46:32 +01:00
|
|
|
Storage.emplace(std::forward<ArgTypes>(Args)...);
|
2018-01-18 12:26:24 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
static constexpr Optional create(const T *y) {
|
2018-01-18 12:26:24 +01:00
|
|
|
return y ? Optional(*y) : Optional();
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional &operator=(const T &y) {
|
|
|
|
Storage = y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Optional &operator=(const Optional &O) = default;
|
|
|
|
|
|
|
|
void reset() { Storage.reset(); }
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr const T *getPointer() const { return &Storage.getValue(); }
|
2019-02-18 09:46:32 +01:00
|
|
|
T *getPointer() { return &Storage.getValue(); }
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr const T &getValue() const LLVM_LVALUE_FUNCTION {
|
|
|
|
return Storage.getValue();
|
|
|
|
}
|
2020-01-22 18:36:48 +01:00
|
|
|
T &getValue() LLVM_LVALUE_FUNCTION { return Storage.getValue(); }
|
2018-01-18 12:26:24 +01:00
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr explicit operator bool() const { return hasValue(); }
|
|
|
|
constexpr bool hasValue() const { return Storage.hasValue(); }
|
|
|
|
constexpr const T *operator->() const { return getPointer(); }
|
2018-01-18 12:26:24 +01:00
|
|
|
T *operator->() { return getPointer(); }
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr const T &operator*() const LLVM_LVALUE_FUNCTION {
|
|
|
|
return getValue();
|
|
|
|
}
|
2020-01-22 18:36:48 +01:00
|
|
|
T &operator*() LLVM_LVALUE_FUNCTION { return getValue(); }
|
2012-11-30 01:38:53 +01:00
|
|
|
|
2014-09-29 20:56:08 +02:00
|
|
|
template <typename U>
|
2020-01-22 18:36:48 +01:00
|
|
|
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
|
2014-09-29 20:56:08 +02:00
|
|
|
return hasValue() ? getValue() : std::forward<U>(value);
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:57:03 +01:00
|
|
|
/// Apply a function to the value if present; otherwise return None.
|
|
|
|
template <class Function>
|
2020-01-22 20:20:15 +01:00
|
|
|
auto map(const Function &F) const LLVM_LVALUE_FUNCTION
|
2019-12-16 17:57:03 +01:00
|
|
|
-> Optional<decltype(F(getValue()))> {
|
|
|
|
if (*this) return F(getValue());
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:36:48 +01:00
|
|
|
#if LLVM_HAS_RVALUE_REFERENCE_THIS
|
2019-02-18 09:46:32 +01:00
|
|
|
T &&getValue() && { return std::move(Storage.getValue()); }
|
|
|
|
T &&operator*() && { return std::move(Storage.getValue()); }
|
2014-09-29 20:56:08 +02:00
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
T getValueOr(U &&value) && {
|
|
|
|
return hasValue() ? std::move(getValue()) : std::forward<U>(value);
|
|
|
|
}
|
2019-12-16 17:57:03 +01:00
|
|
|
|
|
|
|
/// Apply a function to the value if present; otherwise return None.
|
|
|
|
template <class Function>
|
|
|
|
auto map(const Function &F) &&
|
|
|
|
-> Optional<decltype(F(std::move(*this).getValue()))> {
|
|
|
|
if (*this) return F(std::move(*this).getValue());
|
|
|
|
return None;
|
|
|
|
}
|
2020-01-22 18:36:48 +01:00
|
|
|
#endif
|
2010-04-09 22:25:54 +02:00
|
|
|
};
|
|
|
|
|
2020-12-04 04:26:56 +01:00
|
|
|
template <class T> llvm::hash_code hash_value(const Optional<T> &O) {
|
|
|
|
return O ? hash_combine(true, *O) : hash_value(false);
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:10:15 +02:00
|
|
|
template <typename T, typename U>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool operator==(const Optional<T> &X, const Optional<U> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
if (X && Y)
|
|
|
|
return *X == *Y;
|
|
|
|
return X.hasValue() == Y.hasValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(X == Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool operator<(const Optional<T> &X, const Optional<U> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
if (X && Y)
|
|
|
|
return *X < *Y;
|
|
|
|
return X.hasValue() < Y.hasValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(Y < X);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool operator>(const Optional<T> &X, const Optional<U> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return Y < X;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2020-08-21 10:45:44 +02:00
|
|
|
constexpr bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(X < Y);
|
|
|
|
}
|
2011-02-11 19:13:20 +01:00
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator==(const Optional<T> &X, NoneType) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !X;
|
2015-08-20 01:07:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator==(NoneType, const Optional<T> &X) {
|
2015-08-20 01:07:27 +02:00
|
|
|
return X == None;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator!=(const Optional<T> &X, NoneType) {
|
2015-08-20 01:07:27 +02:00
|
|
|
return !(X == None);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator!=(NoneType, const Optional<T> &X) {
|
2015-08-20 01:07:27 +02:00
|
|
|
return X != None;
|
|
|
|
}
|
2016-08-11 22:10:15 +02:00
|
|
|
|
2021-03-12 19:23:19 +01:00
|
|
|
template <typename T> constexpr bool operator<(const Optional<T> &, NoneType) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T> constexpr bool operator<(NoneType, const Optional<T> &X) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return X.hasValue();
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<=(const Optional<T> &X, NoneType) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(None < X);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<=(NoneType, const Optional<T> &X) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(X < None);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T> constexpr bool operator>(const Optional<T> &X, NoneType) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return None < X;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T> constexpr bool operator>(NoneType, const Optional<T> &X) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return X < None;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>=(const Optional<T> &X, NoneType) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return None <= X;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>=(NoneType, const Optional<T> &X) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return X <= None;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator==(const Optional<T> &X, const T &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return X && *X == Y;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator==(const T &X, const Optional<T> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return Y && X == *Y;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator!=(const Optional<T> &X, const T &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(X == Y);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator!=(const T &X, const Optional<T> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(X == Y);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<(const Optional<T> &X, const T &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !X || *X < Y;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<(const T &X, const Optional<T> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return Y && X < *Y;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<=(const Optional<T> &X, const T &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(Y < X);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<=(const T &X, const Optional<T> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(Y < X);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>(const Optional<T> &X, const T &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return Y < X;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>(const T &X, const Optional<T> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return Y < X;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>=(const Optional<T> &X, const T &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(X < Y);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:45:44 +02:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>=(const T &X, const Optional<T> &Y) {
|
2016-08-11 22:10:15 +02:00
|
|
|
return !(X < Y);
|
|
|
|
}
|
2011-02-11 19:13:20 +01:00
|
|
|
|
2019-01-18 13:52:03 +01:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, NoneType);
|
|
|
|
|
|
|
|
template <typename T, typename = decltype(std::declval<raw_ostream &>()
|
|
|
|
<< std::declval<const T &>())>
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
|
|
|
|
if (O)
|
|
|
|
OS << *O;
|
|
|
|
else
|
|
|
|
OS << None;
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2017-06-14 23:42:24 +02:00
|
|
|
} // end namespace llvm
|
2010-04-09 22:25:54 +02:00
|
|
|
|
2017-06-14 23:42:24 +02:00
|
|
|
#endif // LLVM_ADT_OPTIONAL_H
|