From 2260f4dd6cea8f8278a5cfa83f4ff4bc6998a66f Mon Sep 17 00:00:00 2001 From: mental Date: Sun, 4 Mar 2007 23:16:18 +0000 Subject: [PATCH] clean up reference version of maybe, and add == and != operators --- src/libnr/nr-maybe.h | 111 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 91 insertions(+), 20 deletions(-) diff --git a/src/libnr/nr-maybe.h b/src/libnr/nr-maybe.h index 917323d3f..00aec2d43 100644 --- a/src/libnr/nr-maybe.h +++ b/src/libnr/nr-maybe.h @@ -28,15 +28,15 @@ struct Nothing {}; template class Maybe { public: - Maybe(Nothing) : _is_nothing(true), _t() {} - Maybe(T const &t) : _is_nothing(false), _t(t) {} - Maybe(Maybe const &m) : _is_nothing(m._is_nothing), _t(m._t) {} + Maybe(Nothing) : _t(), _is_nothing(true) {} + Maybe(T const &t) : _t(t), _is_nothing(false) {} + Maybe(Maybe const &m) : _t(m._t), _is_nothing(m._is_nothing) {} template Maybe(Maybe const &m) - : _is_nothing(m._is_nothing), _t(m._t) {} + : _t(m._t), _is_nothing(m._is_nothing) {} template Maybe(T2 const &t) - : _is_nothing(false), _t(t) {}; + : _t(t), _is_nothing(false) {} operator bool() const { return !_is_nothing; } @@ -70,23 +70,40 @@ public: } } + template + bool operator==(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing == other._is_nothing; + } else { + return _t == other._t; + } + } + template + bool operator!=(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing != other._is_nothing; + } else { + return _t != other._t; + } + } + private: - bool _is_nothing; T _t; + bool _is_nothing; }; template class Maybe { public: - Maybe(Nothing) : _is_nothing(true), _t() {} - Maybe(T const &t) : _is_nothing(false), _t(t) {} - Maybe(Maybe const &m) : _is_nothing(m._is_nothing), _t(m._t) {} + Maybe(Nothing) : _t(), _is_nothing(true) {} + Maybe(T const &t) : _t(t), _is_nothing(false) {} + Maybe(Maybe const &m) : _t(m._t), _is_nothing(m._is_nothing) {} template Maybe(Maybe const &m) - : _is_nothing(m._is_nothing), _t(m._t) {} + : _t(m._t), _is_nothing(m._is_nothing) {} template Maybe(T2 const &t) - : _is_nothing(false), _t(t) {}; + : _t(t), _is_nothing(false) {} operator bool() const { return !_is_nothing; } @@ -106,23 +123,43 @@ public: } } + template + bool operator==(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing == other._is_nothing; + } else { + return _t == other._t; + } + } + template + bool operator!=(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing != other._is_nothing; + } else { + return _t != other._t; + } + } + private: - bool _is_nothing; T const _t; + bool _is_nothing; }; template class Maybe { public: - Maybe(Nothing) : _is_nothing(true), _t() {} - Maybe(T &t) : _is_nothing(false), _t(t) {} - Maybe(Maybe const &m) : _is_nothing(m._is_nothing), _t(m._t) {} + Maybe(Nothing) : _t(), _is_nothing(true) {} + Maybe(T &t) : _t(&t), _is_nothing(false) {} + Maybe(Maybe const &m) : _t(m._t), _is_nothing(m._is_nothing) {} template Maybe(Maybe const &m) - : _is_nothing(m._is_nothing), _t(m._t) {} + : _t(&m._t), _is_nothing(m._is_nothing) {} + + template Maybe(Maybe const &m) + : _t(m._t), _is_nothing(m._is_nothing) {} template Maybe(T2 &t) - : _is_nothing(false), _t(t) {}; + : _t(&t), _is_nothing(false) {}; operator bool() const { return !_is_nothing; } @@ -130,7 +167,7 @@ public: if (_is_nothing) { throw IsNothing(); } else { - return _t; + return *_t; } } @@ -138,13 +175,47 @@ public: if (_is_nothing) { throw IsNothing(); } else { - return &_t; + return _t; + } + } + + template + bool operator==(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing == other._is_nothing; + } else { + return *_t == other._t; + } + } + template + bool operator!=(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing != other._is_nothing; + } else { + return *_t != other._t; + } + } + + template + bool operator==(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing == other._is_nothing; + } else { + return *_t == *other._t; + } + } + template + bool operator!=(NR::Maybe const &other) const { + if ( _is_nothing || other._is_nothing ) { + return _is_nothing != other._is_nothing; + } else { + return *_t != *other._t; } } private: + T *_t; bool _is_nothing; - T &_t; }; } /* namespace NR */ -- 2.30.2