diff options
| author | MenTaLguY <mental@rydia.net> | 2007-03-09 00:25:36 +0000 |
|---|---|---|
| committer | mental <mental@users.sourceforge.net> | 2007-03-09 00:25:36 +0000 |
| commit | 38d2a672753e8147fa97250051f8d01e2e373d4a (patch) | |
| tree | 71d302bb2ab9e343affb09760670608c911bc2f4 | |
| parent | rework maybe so storage is customizable and can be optimized on a (diff) | |
| download | inkscape-38d2a672753e8147fa97250051f8d01e2e373d4a.tar.gz inkscape-38d2a672753e8147fa97250051f8d01e2e373d4a.zip | |
fix reference maybes by separate specialization
(bzr r2581)
| -rw-r--r-- | src/libnr/nr-maybe.h | 100 |
1 files 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 @@ -42,38 +42,14 @@ private: }; template <typename T> -class MaybeStorage<T &> { -public: - MaybeStorage() : _ref(NULL) {} - MaybeStorage(T &value) : _ref(&value) {} - - bool is_nothing() const { return !_ref; } - T &value() const { return *_ref; } - -private: - T *_ref; -}; - -template <typename T> 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 <typename T2> - Maybe(Maybe<T2> &m) { - if (m) { - _storage = *m; - } - } - template <typename T2> Maybe(Maybe<T2> const &m) { if (m) { _storage = *m; @@ -81,12 +57,6 @@ public: } template <typename T2> - Maybe(Maybe<T2 &> m) { - if (m) { - _storage = *m; - } - } - template <typename T2> Maybe(Maybe<T2 const &> m) { if (m) { _storage = *m; @@ -126,7 +96,7 @@ public: } template <typename T2> - bool operator==(NR::Maybe<T2> const &other) const { + bool operator==(Maybe<T2> const &other) const { bool is_nothing = _storage.is_nothing(); if ( is_nothing || !other ) { return is_nothing && !other; @@ -135,7 +105,7 @@ public: } } template <typename T2> - bool operator!=(NR::Maybe<T2> const &other) const { + bool operator!=(Maybe<T2> const &other) const { bool is_nothing = _storage.is_nothing(); if ( is_nothing || !other ) { return !is_nothing || other; @@ -148,6 +118,72 @@ private: MaybeStorage<T> _storage; }; +template <typename T> +class Maybe<T &> { +public: + Maybe() : _ref(NULL) {} + Maybe(Nothing) : _ref(NULL) {} + Maybe(T &t) : _ref(&t) {} + + template <typename T2> + Maybe(Maybe<T2> const &m) { + if (m) { + _ref = &*m; + } + } + + template <typename T2> + Maybe(Maybe<T2 &> m) { + if (m) { + _ref = *m; + } + } + + template <typename T2> + Maybe(Maybe<T2 const &> 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 <typename T2> + bool operator==(Maybe<T2> const &other) const { + if ( !_ref || !other ) { + return !_ref && !other; + } else { + return *_ref == *other; + } + } + template <typename T2> + bool operator!=(Maybe <T2> const &other) const { + if ( !_ref || !other ) { + return _ref || other; + } else { + return *_ref != *other; + } + } + +private: + T *_ref; +}; + } /* namespace NR */ #endif |
