From 0e5d575feceea4feac4b33e85626719e14f762a1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Feb 2022 15:49:51 +0200 Subject: Add missing optional<> comparison operators --- libbutl/optional.hxx | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/libbutl/optional.hxx b/libbutl/optional.hxx index 28aa95a..f569f8d 100644 --- a/libbutl/optional.hxx +++ b/libbutl/optional.hxx @@ -291,6 +291,8 @@ namespace butl explicit operator bool () const {return this->v_;} }; + // optional ? optional + // template inline auto operator== (const optional& x, const optional& y) @@ -320,6 +322,131 @@ namespace butl { return y < x; } + + // optional ? nullopt + // nullopt ? optional + // + template + inline auto + operator== (const optional& x, nullopt_t) + { + bool px (x); + return !px; + } + + template + inline auto + operator== (nullopt_t, const optional& y) + { + bool py (y); + return !py; + } + + template + inline auto + operator!= (const optional& x, nullopt_t y) + { + return !(x == y); + } + + template + inline auto + operator!= (nullopt_t x, const optional& y) + { + return !(x == y); + } + + template + inline auto + operator< (const optional&, nullopt_t) + { + return false; + } + + template + inline auto + operator< (nullopt_t, const optional& y) + { + bool py (y); + return py; + } + + template + inline auto + operator> (const optional& x, nullopt_t y) + { + return y < x; + } + + template + inline auto + operator> (nullopt_t x, const optional& y) + { + return y < x; + } + + // optional ? T + // T ? optional + // + template + inline auto + operator== (const optional& x, const T& y) + { + bool px (x); + return px && *x == y; + } + + template + inline auto + operator== (const T& x, const optional& y) + { + bool py (y); + return py && x == *y; + } + + template + inline auto + operator!= (const optional& x, const T& y) + { + return !(x == y); + } + + template + inline auto + operator!= (const T& x, const optional& y) + { + return !(x == y); + } + + template + inline auto + operator< (const optional& x, const T& y) + { + bool px (x); + return !px || *x < y; + } + + template + inline auto + operator< (const T& x, const optional& y) + { + bool py (y); + return py && x < *y; + } + + template + inline auto + operator> (const optional& x, const T& y) + { + return y < x; + } + + template + inline auto + operator> (const T& x, const optional& y) + { + return y < x; + } } namespace std -- cgit v1.1