2021-05-25 13:10:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkTOptional_DEFINED
|
|
|
|
#define SkTOptional_DEFINED
|
|
|
|
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace skstd {
|
|
|
|
|
2021-06-22 15:18:53 +00:00
|
|
|
/**
|
|
|
|
* An empty optional is represented with `nullopt`.
|
|
|
|
*/
|
|
|
|
struct nullopt_t {
|
|
|
|
struct tag {};
|
|
|
|
|
|
|
|
// nullopt_t must not be default-constructible.
|
|
|
|
explicit constexpr nullopt_t(tag) {}
|
|
|
|
};
|
|
|
|
|
2021-07-12 16:18:26 +00:00
|
|
|
static constexpr nullopt_t nullopt{nullopt_t::tag{}};
|
2021-06-22 15:18:53 +00:00
|
|
|
|
2021-05-25 13:10:14 +00:00
|
|
|
/**
|
|
|
|
* Simple drop-in replacement for std::optional until we move to C++17. This does not have all of
|
|
|
|
* std::optional's capabilities, but it covers our needs for the time being.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class optional {
|
|
|
|
public:
|
2021-06-01 17:25:22 +00:00
|
|
|
optional(const T& value)
|
|
|
|
: fHasValue(true) {
|
|
|
|
new(&fPayload.fValue) T(value);
|
|
|
|
}
|
2021-05-25 13:10:14 +00:00
|
|
|
|
|
|
|
optional(T&& value)
|
|
|
|
: fHasValue(true) {
|
|
|
|
new(&fPayload.fValue) T(std::move(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
optional() {}
|
|
|
|
|
2021-06-01 17:25:22 +00:00
|
|
|
optional(const optional& other) {
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
2021-06-22 15:18:53 +00:00
|
|
|
// Construction with nullopt is the same as default construction.
|
|
|
|
optional(nullopt_t) : optional() {}
|
|
|
|
|
2021-06-01 17:25:22 +00:00
|
|
|
// We need a non-const copy constructor because otherwise optional(nonConstSrc) isn't an exact
|
|
|
|
// match for the copy constructor, and we'd end up invoking the Args&&... template by mistake.
|
|
|
|
optional(optional& other) {
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
2021-05-25 13:10:14 +00:00
|
|
|
optional(optional&& other) {
|
|
|
|
*this = std::move(other);
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:25:22 +00:00
|
|
|
template<typename... Args>
|
|
|
|
optional(Args&&... args) {
|
|
|
|
fHasValue = true;
|
2021-08-26 17:58:05 +00:00
|
|
|
new(&fPayload.fValue) T(std::forward<Args>(args)...);
|
2021-05-25 13:10:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 17:25:22 +00:00
|
|
|
~optional() {
|
|
|
|
this->reset();
|
|
|
|
}
|
2021-05-25 13:10:14 +00:00
|
|
|
|
2021-06-01 17:25:22 +00:00
|
|
|
optional& operator=(const optional& other) {
|
2021-05-25 13:10:14 +00:00
|
|
|
if (this != &other) {
|
|
|
|
if (fHasValue) {
|
2021-06-01 17:25:22 +00:00
|
|
|
if (other.fHasValue) {
|
|
|
|
fPayload.fValue = other.fPayload.fValue;
|
|
|
|
} else {
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (other.fHasValue) {
|
|
|
|
fHasValue = true;
|
|
|
|
new (&fPayload.fValue) T(other.fPayload.fValue);
|
|
|
|
} else {
|
|
|
|
// do nothing, no value on either side
|
|
|
|
}
|
2021-05-25 13:10:14 +00:00
|
|
|
}
|
2021-06-01 17:25:22 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional& operator=(optional&& other) {
|
|
|
|
if (this != &other) {
|
2021-05-25 13:10:14 +00:00
|
|
|
if (fHasValue) {
|
2021-06-01 17:25:22 +00:00
|
|
|
if (other.fHasValue) {
|
|
|
|
fPayload.fValue = std::move(other.fPayload.fValue);
|
|
|
|
} else {
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (other.fHasValue) {
|
|
|
|
fHasValue = true;
|
|
|
|
new (&fPayload.fValue) T(std::move(other.fPayload.fValue));
|
|
|
|
} else {
|
|
|
|
// do nothing, no value on either side
|
|
|
|
}
|
2021-05-25 13:10:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
2021-08-26 17:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
optional& emplace(Args&&... args) {
|
|
|
|
this->reset();
|
|
|
|
fHasValue = true;
|
|
|
|
new(&fPayload.fValue) T(std::forward<Args>(args)...);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename... Args>
|
|
|
|
optional& emplace(std::initializer_list<U> il, Args&&... args) {
|
|
|
|
this->reset();
|
|
|
|
fHasValue = true;
|
|
|
|
new(&fPayload.fValue) T(il, std::forward<Args>(args)...);
|
|
|
|
return *this;
|
2021-05-25 13:10:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 15:18:53 +00:00
|
|
|
// Assignment to nullopt is the same as reset().
|
|
|
|
optional& operator=(nullopt_t) {
|
|
|
|
this->reset();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-07-07 21:43:57 +00:00
|
|
|
T& operator*() & {
|
2021-05-25 13:10:14 +00:00
|
|
|
SkASSERT(fHasValue);
|
|
|
|
return fPayload.fValue;
|
|
|
|
}
|
|
|
|
|
2021-07-07 21:43:57 +00:00
|
|
|
const T& operator*() const& {
|
|
|
|
SkASSERT(fHasValue);
|
2021-06-01 17:25:22 +00:00
|
|
|
return fPayload.fValue;
|
|
|
|
}
|
|
|
|
|
2021-07-07 21:43:57 +00:00
|
|
|
T&& operator*() && {
|
2021-06-01 17:25:22 +00:00
|
|
|
SkASSERT(fHasValue);
|
2021-07-07 21:43:57 +00:00
|
|
|
return std::move(fPayload.fValue);
|
2021-06-01 17:25:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 21:43:57 +00:00
|
|
|
const T&& operator*() const&& {
|
2021-05-25 13:10:14 +00:00
|
|
|
SkASSERT(fHasValue);
|
2021-07-07 21:43:57 +00:00
|
|
|
return std::move(fPayload.fValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& value() const& {
|
|
|
|
SkASSERT_RELEASE(fHasValue);
|
|
|
|
return **this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& value() & {
|
|
|
|
SkASSERT_RELEASE(fHasValue);
|
|
|
|
return **this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T&& value() const&& {
|
|
|
|
SkASSERT_RELEASE(fHasValue);
|
|
|
|
return std::move(**this);
|
2021-05-25 13:10:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 21:43:57 +00:00
|
|
|
T&& value() && {
|
|
|
|
SkASSERT_RELEASE(fHasValue);
|
|
|
|
return std::move(**this);
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() {
|
|
|
|
return &**this;
|
2021-05-25 13:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const T* operator->() const {
|
2021-07-07 21:43:57 +00:00
|
|
|
return &**this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
T value_or(U&& value) const& {
|
|
|
|
return this->has_value() ? **this : static_cast<T>(std::forward<U>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
T value_or(U&& value) && {
|
|
|
|
return this->has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(value));
|
2021-05-25 13:10:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 17:25:22 +00:00
|
|
|
bool has_value() const {
|
2021-05-25 13:10:14 +00:00
|
|
|
return fHasValue;
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:25:22 +00:00
|
|
|
explicit operator bool() const {
|
|
|
|
return this->has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
if (fHasValue) {
|
|
|
|
fPayload.fValue.~T();
|
|
|
|
fHasValue = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 13:10:14 +00:00
|
|
|
private:
|
|
|
|
union Payload {
|
|
|
|
T fValue;
|
|
|
|
|
|
|
|
Payload() {}
|
|
|
|
|
|
|
|
~Payload() {}
|
|
|
|
} fPayload;
|
|
|
|
|
|
|
|
bool fHasValue = false;
|
|
|
|
};
|
|
|
|
|
2021-06-22 19:43:05 +00:00
|
|
|
// Comparison operators for optional x optional
|
|
|
|
template <typename T, typename U> bool operator==(const optional<T>& a, const optional<U>& b) {
|
|
|
|
return (a.has_value() != b.has_value()) ? false :
|
|
|
|
!a.has_value() ? true :
|
|
|
|
(*a == *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator!=(const optional<T>& a, const optional<U>& b) {
|
|
|
|
return (a.has_value() != b.has_value()) ? true :
|
|
|
|
!a.has_value() ? false :
|
|
|
|
(*a != *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator<(const optional<T>& a, const optional<U>& b) {
|
|
|
|
return !b.has_value() ? false :
|
|
|
|
!a.has_value() ? true :
|
|
|
|
(*a < *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator<=(const optional<T>& a, const optional<U>& b) {
|
|
|
|
return !a.has_value() ? true :
|
|
|
|
!b.has_value() ? false :
|
|
|
|
(*a <= *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator>(const optional<T>& a, const optional<U>& b) {
|
|
|
|
return !a.has_value() ? false :
|
|
|
|
!b.has_value() ? true :
|
|
|
|
(*a > *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator>=(const optional<T>& a, const optional<U>& b) {
|
|
|
|
return !b.has_value() ? true :
|
|
|
|
!a.has_value() ? false :
|
|
|
|
(*a >= *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison operators for optional x nullopt
|
|
|
|
template <typename T> bool operator==(const optional<T>& a, nullopt_t) {
|
|
|
|
return !a.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> bool operator!=(const optional<T>& a, nullopt_t) {
|
|
|
|
return a.has_value();
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:39:26 +00:00
|
|
|
template <typename T> bool operator<(const optional<T>&, nullopt_t) {
|
2021-06-22 19:43:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> bool operator<=(const optional<T>& a, nullopt_t) {
|
|
|
|
return !a.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> bool operator>(const optional<T>& a, nullopt_t) {
|
|
|
|
return a.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-06-28 18:39:26 +00:00
|
|
|
bool operator>=(const optional<T>&, nullopt_t) {
|
2021-06-22 19:43:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison operators for nullopt x optional
|
|
|
|
template <typename U> bool operator==(nullopt_t, const optional<U>& b) {
|
|
|
|
return !b.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U> bool operator!=(nullopt_t, const optional<U>& b) {
|
|
|
|
return b.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U> bool operator<(nullopt_t, const optional<U>& b) {
|
|
|
|
return b.has_value();
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:39:26 +00:00
|
|
|
template <typename U> bool operator<=(nullopt_t, const optional<U>&) {
|
2021-06-22 19:43:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:39:26 +00:00
|
|
|
template <typename U> bool operator>(nullopt_t, const optional<U>&) {
|
2021-06-22 19:43:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U> bool operator>=(nullopt_t, const optional<U>& b) {
|
|
|
|
return !b.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison operators for optional x value
|
|
|
|
template <typename T, typename U> bool operator==(const optional<T>& a, const U& b) {
|
|
|
|
return a.has_value() && (*a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator!=(const optional<T>& a, const U& b) {
|
|
|
|
return !a.has_value() || (*a != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator<(const optional<T>& a, const U& b) {
|
|
|
|
return !a.has_value() || (*a < b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator<=(const optional<T>& a, const U& b) {
|
|
|
|
return !a.has_value() || (*a <= b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator>(const optional<T>& a, const U& b) {
|
|
|
|
return a.has_value() && (*a > b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator>=(const optional<T>& a, const U& b) {
|
|
|
|
return a.has_value() && (*a >= b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison operators for value x optional
|
|
|
|
template <typename T, typename U> bool operator==(const T& a, const optional<U>& b) {
|
|
|
|
return b.has_value() && (a == *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator!=(const T& a, const optional<U>& b) {
|
|
|
|
return !b.has_value() || (a != *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator<(const T& a, const optional<U>& b) {
|
|
|
|
return b.has_value() && (a < *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator<=(const T& a, const optional<U>& b) {
|
|
|
|
return b.has_value() && (a <= *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator>(const T& a, const optional<U>& b) {
|
|
|
|
return !b.has_value() || (a > *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U> bool operator>=(const T& a, const optional<U>& b) {
|
|
|
|
return !b.has_value() || (a >= *b);
|
|
|
|
}
|
|
|
|
|
2021-05-25 13:10:14 +00:00
|
|
|
} // namespace skstd
|
|
|
|
|
|
|
|
#endif
|