stdlib: adapt to llvm's changed Optional API

add `value()` which replaces the deprecated `getValue()`
This commit is contained in:
Erik Eckstein
2022-10-31 10:58:03 +01:00
parent 67a98e9a37
commit 4960396f11

View File

@@ -72,7 +72,7 @@ template <typename T, bool = (llvm::is_trivially_copy_constructible<T>::value &&
class OptionalStorage {
union {
char empty;
T value;
T val;
};
bool hasVal;
@@ -83,22 +83,22 @@ public:
constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
if (other.hasValue()) {
emplace(other.value);
emplace(other.val);
}
}
constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
if (other.hasValue()) {
emplace(std::move(other.value));
emplace(std::move(other.val));
}
}
template <class... Args>
constexpr explicit OptionalStorage(in_place_t, Args &&... args)
: value(std::forward<Args>(args)...), hasVal(true) {}
: val(std::forward<Args>(args)...), hasVal(true) {}
void reset() noexcept {
if (hasVal) {
value.~T();
val.~T();
hasVal = false;
}
}
@@ -107,39 +107,39 @@ public:
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
return val;
}
constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
return val;
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&getValue() && noexcept {
assert(hasVal);
return std::move(value);
return std::move(val);
}
#endif
template <class... Args> void emplace(Args &&... args) {
reset();
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
::new ((void *)std::addressof(val)) T(std::forward<Args>(args)...);
hasVal = true;
}
OptionalStorage &operator=(T const &y) {
if (hasValue()) {
value = y;
val = y;
} else {
::new ((void *)std::addressof(value)) T(y);
::new ((void *)std::addressof(val)) T(y);
hasVal = true;
}
return *this;
}
OptionalStorage &operator=(T &&y) {
if (hasValue()) {
value = std::move(y);
val = std::move(y);
} else {
::new ((void *)std::addressof(value)) T(std::move(y));
::new ((void *)std::addressof(val)) T(std::move(y));
hasVal = true;
}
return *this;
@@ -148,9 +148,9 @@ public:
OptionalStorage &operator=(OptionalStorage const &other) {
if (other.hasValue()) {
if (hasValue()) {
value = other.value;
val = other.val;
} else {
::new ((void *)std::addressof(value)) T(other.value);
::new ((void *)std::addressof(val)) T(other.val);
hasVal = true;
}
} else {
@@ -162,9 +162,9 @@ public:
OptionalStorage &operator=(OptionalStorage &&other) {
if (other.hasValue()) {
if (hasValue()) {
value = std::move(other.value);
val = std::move(other.val);
} else {
::new ((void *)std::addressof(value)) T(std::move(other.value));
::new ((void *)std::addressof(val)) T(std::move(other.val));
hasVal = true;
}
} else {
@@ -177,7 +177,7 @@ public:
template <typename T> class OptionalStorage<T, true> {
union {
char empty;
T value;
T val;
};
bool hasVal = false;
@@ -194,11 +194,11 @@ public:
template <class... Args>
constexpr explicit OptionalStorage(in_place_t, Args &&... args)
: value(std::forward<Args>(args)...), hasVal(true) {}
: val(std::forward<Args>(args)...), hasVal(true) {}
void reset() noexcept {
if (hasVal) {
value.~T();
val.~T();
hasVal = false;
}
}
@@ -207,39 +207,39 @@ public:
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
return val;
}
constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
return val;
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&getValue() && noexcept {
assert(hasVal);
return std::move(value);
return std::move(val);
}
#endif
template <class... Args> void emplace(Args &&... args) {
reset();
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
::new ((void *)std::addressof(val)) T(std::forward<Args>(args)...);
hasVal = true;
}
OptionalStorage &operator=(T const &y) {
if (hasValue()) {
value = y;
val = y;
} else {
::new ((void *)std::addressof(value)) T(y);
::new ((void *)std::addressof(val)) T(y);
hasVal = true;
}
return *this;
}
OptionalStorage &operator=(T &&y) {
if (hasValue()) {
value = std::move(y);
val = std::move(y);
} else {
::new ((void *)std::addressof(value)) T(std::move(y));
::new ((void *)std::addressof(val)) T(std::move(y));
hasVal = true;
}
return *this;
@@ -292,9 +292,13 @@ public:
constexpr const T *getPointer() const { return &Storage.getValue(); }
T *getPointer() { return &Storage.getValue(); }
constexpr const T &value() const LLVM_LVALUE_FUNCTION {
return Storage.getValue();
}
constexpr const T &getValue() const LLVM_LVALUE_FUNCTION {
return Storage.getValue();
}
T &value() LLVM_LVALUE_FUNCTION { return Storage.getValue(); }
T &getValue() LLVM_LVALUE_FUNCTION { return Storage.getValue(); }
constexpr explicit operator bool() const { return hasValue(); }
@@ -307,8 +311,8 @@ public:
T &operator*() LLVM_LVALUE_FUNCTION { return getValue(); }
template <typename U>
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
return hasValue() ? getValue() : std::forward<U>(value);
constexpr T getValueOr(U &&alt) const LLVM_LVALUE_FUNCTION {
return hasValue() ? getValue() : std::forward<U>(alt);
}
/// Apply a function to the value if present; otherwise return None.
@@ -320,12 +324,13 @@ public:
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&value() && { return std::move(Storage.getValue()); }
T &&getValue() && { return std::move(Storage.getValue()); }
T &&operator*() && { return std::move(Storage.getValue()); }
template <typename U>
T getValueOr(U &&value) && {
return hasValue() ? std::move(getValue()) : std::forward<U>(value);
T getValueOr(U &&alt) && {
return hasValue() ? std::move(getValue()) : std::forward<U>(alt);
}
/// Apply a function to the value if present; otherwise return None.