From 85fed18d9f78934228e553c240b149748a116a7f Mon Sep 17 00:00:00 2001 From: mental Date: Fri, 9 Mar 2007 00:25:36 +0000 Subject: [PATCH] fix reference maybes by separate specialization --- src/libnr/nr-maybe.h | 100 +++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 32 deletions(-) diff --git a/src/libnr/nr-maybe.h b/src/libnr/nr-maybe.h index 59e064f34..94078c211 100644 --- a/src/libnr/nr-maybe.h +++ b/src/libnr/nr-maybe.h @@ -41,38 +41,14 @@ private: bool _is_nothing; }; -template -class MaybeStorage { -public: - MaybeStorage() : _ref(NULL) {} - MaybeStorage(T &value) : _ref(&value) {} - - bool is_nothing() const { return !_ref; } - T &value() const { return *_ref; } - -private: - T *_ref; -}; - template class Maybe { public: Maybe() {} - Maybe(Nothing) {} - - Maybe(T &t) : _storage(t) {} Maybe(T const &t) : _storage(t) {} - - Maybe(Maybe &m) : _storage(m._storage) {} Maybe(Maybe const &m) : _storage(m._storage) {} - template - Maybe(Maybe &m) { - if (m) { - _storage = *m; - } - } template Maybe(Maybe const &m) { if (m) { @@ -80,12 +56,6 @@ public: } } - template - Maybe(Maybe m) { - if (m) { - _storage = *m; - } - } template Maybe(Maybe m) { if (m) { @@ -126,7 +96,7 @@ public: } template - bool operator==(NR::Maybe const &other) const { + bool operator==(Maybe const &other) const { bool is_nothing = _storage.is_nothing(); if ( is_nothing || !other ) { return is_nothing && !other; @@ -135,7 +105,7 @@ public: } } template - bool operator!=(NR::Maybe const &other) const { + bool operator!=(Maybe const &other) const { bool is_nothing = _storage.is_nothing(); if ( is_nothing || !other ) { return !is_nothing || other; @@ -148,6 +118,72 @@ private: MaybeStorage _storage; }; +template +class Maybe { +public: + Maybe() : _ref(NULL) {} + Maybe(Nothing) : _ref(NULL) {} + Maybe(T &t) : _ref(&t) {} + + template + Maybe(Maybe const &m) { + if (m) { + _ref = &*m; + } + } + + template + Maybe(Maybe m) { + if (m) { + _ref = *m; + } + } + + template + Maybe(Maybe m) { + if (m) { + _ref = *m; + } + } + + operator bool() const { return _ref; } + + T &operator*() const throw(IsNothing) { + if (!_ref) { + throw IsNothing(); + } else { + return *_ref; + } + } + T *operator->() const throw(IsNothing) { + if (!_ref) { + throw IsNothing(); + } else { + return _ref; + } + } + + template + bool operator==(Maybe const &other) const { + if ( !_ref || !other ) { + return !_ref && !other; + } else { + return *_ref == *other; + } + } + template + bool operator!=(Maybe const &other) const { + if ( !_ref || !other ) { + return _ref || other; + } else { + return *_ref != *other; + } + } + +private: + T *_ref; +}; + } /* namespace NR */ #endif -- 2.30.2