From 7e1304c6e61a25aac6fcf1bee47a719ac39075e5 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Sat, 29 Aug 2020 14:05:53 -0400 Subject: cm::optional: Add comparison operators --- Utilities/std/cm/optional | 204 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) (limited to 'Utilities/std') diff --git a/Utilities/std/cm/optional b/Utilities/std/cm/optional index e691e8e1e9..9a5d840766 100644 --- a/Utilities/std/cm/optional +++ b/Utilities/std/cm/optional @@ -218,6 +218,210 @@ optional& optional::operator=(U&& v) return *this; } +template +bool operator==(const optional& lhs, const optional& rhs) +{ + if (lhs.has_value()) { + return rhs.has_value() && *lhs == *rhs; + } + return !rhs.has_value(); +} + +template +bool operator!=(const optional& lhs, const optional& rhs) +{ + if (lhs.has_value()) { + return !rhs.has_value() || *lhs != *rhs; + } + return rhs.has_value(); +} + +template +bool operator<(const optional& lhs, const optional& rhs) +{ + if (rhs.has_value()) { + return !lhs.has_value() || *lhs < *rhs; + } + return false; +} + +template +bool operator<=(const optional& lhs, const optional& rhs) +{ + if (!lhs.has_value()) { + return true; + } + if (rhs.has_value()) { + return *lhs <= *rhs; + } + return false; +} + +template +bool operator>(const optional& lhs, const optional& rhs) +{ + if (lhs.has_value()) { + return !rhs.has_value() || *lhs > *rhs; + } + return false; +} + +template +bool operator>=(const optional& lhs, const optional& rhs) +{ + if (!rhs.has_value()) { + return true; + } + if (lhs.has_value()) { + return *lhs >= *rhs; + } + return false; +} + +template +bool operator==(const optional& opt, nullopt_t) noexcept +{ + return !opt.has_value(); +} + +template +bool operator!=(const optional& opt, nullopt_t) noexcept +{ + return opt.has_value(); +} + +template +bool operator<(const optional& opt, nullopt_t) noexcept +{ + return false; +} + +template +bool operator<=(const optional& opt, nullopt_t) noexcept +{ + return !opt.has_value(); +} + +template +bool operator>(const optional& opt, nullopt_t) noexcept +{ + return opt.has_value(); +} + +template +bool operator>=(const optional& opt, nullopt_t) noexcept +{ + return true; +} + +template +bool operator==(nullopt_t, const optional& opt) noexcept +{ + return !opt.has_value(); +} + +template +bool operator!=(nullopt_t, const optional& opt) noexcept +{ + return opt.has_value(); +} + +template +bool operator<(nullopt_t, const optional& opt) noexcept +{ + return opt.has_value(); +} + +template +bool operator<=(nullopt_t, const optional& opt) noexcept +{ + return true; +} + +template +bool operator>(nullopt_t, const optional& opt) noexcept +{ + return false; +} + +template +bool operator>=(nullopt_t, const optional& opt) noexcept +{ + return !opt.has_value(); +} + +template +bool operator==(const optional& opt, const U& value) +{ + return opt.has_value() && *opt == value; +} + +template +bool operator!=(const optional& opt, const U& value) +{ + return !opt.has_value() || *opt != value; +} + +template +bool operator<(const optional& opt, const U& value) +{ + return !opt.has_value() || *opt < value; +} + +template +bool operator<=(const optional& opt, const U& value) +{ + return !opt.has_value() || *opt <= value; +} + +template +bool operator>(const optional& opt, const U& value) +{ + return opt.has_value() && *opt > value; +} + +template +bool operator>=(const optional& opt, const U& value) +{ + return opt.has_value() && *opt >= value; +} + +template +bool operator==(const T& value, const optional& opt) +{ + return opt.has_value() && value == *opt; +} + +template +bool operator!=(const T& value, const optional& opt) +{ + return !opt.has_value() || value != *opt; +} + +template +bool operator<(const T& value, const optional& opt) +{ + return opt.has_value() && value < *opt; +} + +template +bool operator<=(const T& value, const optional& opt) +{ + return opt.has_value() && value <= *opt; +} + +template +bool operator>(const T& value, const optional& opt) +{ + return !opt.has_value() || value > *opt; +} + +template +bool operator>=(const T& value, const optional& opt) +{ + return !opt.has_value() || value >= *opt; +} + template const T* optional::operator->() const { -- cgit v1.2.1