From: mental Date: Thu, 8 Mar 2007 23:42:21 +0000 (+0000) Subject: rework maybe so storage is customizable and can be optimized on a X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=0823b03fe362fa611a1dadb0a5e457e6829a2bcd;p=inkscape.git rework maybe so storage is customizable and can be optimized on a per-type basis --- diff --git a/src/libnr/nr-maybe.h b/src/libnr/nr-maybe.h index 6749dfc01..59e064f34 100644 --- a/src/libnr/nr-maybe.h +++ b/src/libnr/nr-maybe.h @@ -26,185 +26,126 @@ public: struct Nothing {}; template -class Maybe { +class MaybeStorage { public: - Maybe(Nothing) : _is_nothing(true) {} - Maybe(T const &t) : _t(t), _is_nothing(false) {} - Maybe(Maybe const &m) : _t(m._t), _is_nothing(m._is_nothing) {} + MaybeStorage() : _is_nothing(true) {} + MaybeStorage(T const &value) + : _value(value), _is_nothing(false) {} - template Maybe(Maybe const &m) - : _is_nothing(!m) - { - if (m) { - _t = *m; - } - } + bool is_nothing() const { return _is_nothing; } + T &value() { return _value; } + T const &value() const { return _value; } - template Maybe(T2 const &t) - : _t(t), _is_nothing(false) {} +private: + T _value; + bool _is_nothing; +}; - operator bool() const { return !_is_nothing; } +template +class MaybeStorage { +public: + MaybeStorage() : _ref(NULL) {} + MaybeStorage(T &value) : _ref(&value) {} - T const &operator*() const throw(IsNothing) { - if (_is_nothing) { - throw IsNothing(); - } else { - return _t; - } - } - T &operator*() throw(IsNothing) { - if (_is_nothing) { - throw IsNothing(); - } else { - return _t; - } - } + bool is_nothing() const { return !_ref; } + T &value() const { return *_ref; } - T const *operator->() const throw(IsNothing) { - if (_is_nothing) { - throw IsNothing(); - } else { - return &_t; +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; } } - T *operator->() throw(IsNothing) { - if (_is_nothing) { - throw IsNothing(); - } else { - return &_t; + template + Maybe(Maybe const &m) { + if (m) { + _storage = *m; } } template - bool operator==(NR::Maybe const &other) const { - if ( _is_nothing || !other ) { - return _is_nothing && !other; - } else { - return _t == *other; + Maybe(Maybe m) { + if (m) { + _storage = *m; } } template - bool operator!=(NR::Maybe const &other) const { - if ( _is_nothing || !other ) { - return !_is_nothing || other; - } else { - return _t != *other; - } - } - -private: - T _t; - bool _is_nothing; -}; - -template -class Maybe { -public: - Maybe(Nothing) : _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) - { + Maybe(Maybe m) { if (m) { - _t = *m; + _storage = *m; } } - template Maybe(T2 const &t) - : _t(t), _is_nothing(false) {} - - operator bool() const { return !_is_nothing; } + operator bool() const { return !_storage.is_nothing(); } T const &operator*() const throw(IsNothing) { - if (_is_nothing) { + if (_storage.is_nothing()) { throw IsNothing(); } else { - return _t; + return _storage.value(); } } - - T const *operator->() const throw(IsNothing) { - if (_is_nothing) { + T &operator*() throw(IsNothing) { + if (_storage.is_nothing()) { throw IsNothing(); } else { - return &_t; - } - } - - template - bool operator==(NR::Maybe const &other) const { - if ( _is_nothing || !other ) { - return _is_nothing && !other; - } else { - return _t == *other; + return _storage.value(); } } - template - bool operator!=(NR::Maybe const &other) const { - if ( _is_nothing || !other ) { - return !_is_nothing || other; - } else { - return _t != *other; - } - } - -private: - T const _t; - bool _is_nothing; -}; -template -class Maybe { -public: - Maybe(Nothing) : _ref(NULL) {} - Maybe(T &t) : _ref(&t) {} - Maybe(Maybe const &m) : _ref(m._ref) {} - - template Maybe(Maybe const &m) - : _ref( m ? &*m : NULL ) {} - template Maybe(T2 &t) : _ref(&t) {} - - operator bool() const { return _ref; } - - T &operator*() const throw(IsNothing) { - if (_ref) { + T const *operator->() const throw(IsNothing) { + if (_storage.is_nothing()) { throw IsNothing(); } else { - return *_ref; + return &_storage.value(); } } - - T *operator->() const throw(IsNothing) { - if (_ref) { + T *operator->() throw(IsNothing) { + if (_storage.is_nothing()) { throw IsNothing(); } else { - return _ref; + return &_storage.value(); } } template bool operator==(NR::Maybe const &other) const { - if ( !_ref || !other ) { - return !_ref && !other; + bool is_nothing = _storage.is_nothing(); + if ( is_nothing || !other ) { + return is_nothing && !other; } else { - return *_ref == *other; + return _storage.value() == *other; } } template bool operator!=(NR::Maybe const &other) const { - if ( !_ref || !other ) { - return _ref || other; + bool is_nothing = _storage.is_nothing(); + if ( is_nothing || !other ) { + return !is_nothing || other; } else { - return *_ref != *other; + return _storage.value() != *other; } } private: - void operator=(Maybe const &); // no assign - - T *_ref; + MaybeStorage _storage; }; } /* namespace NR */