From: mental Date: Sun, 4 Mar 2007 04:23:10 +0000 (+0000) Subject: redo NR::Maybe to be less clever X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ce3c006c1ef19208d237bc98773eeee5b90e4af3;p=inkscape.git redo NR::Maybe to be less clever --- diff --git a/src/libnr/nr-maybe.h b/src/libnr/nr-maybe.h index 9c1aa0708..917323d3f 100644 --- a/src/libnr/nr-maybe.h +++ b/src/libnr/nr-maybe.h @@ -2,12 +2,9 @@ #define __NR_MAYBE_H__ /* - * Functionalesque "Maybe" class + * Nullable values for C++ * - * Copyright 2004 MenTaLguY - * - * Authors: - * MenTaLguY + * Copyright 2004, 2006 MenTaLguY * * This code is licensed under the GNU GPL; see COPYING for more information. */ @@ -17,83 +14,137 @@ #endif #include -#include #include namespace NR { -/** An exception class for run-time type errors */ -template -class IsNot : public std::domain_error { +class IsNothing : public std::domain_error { public: - IsNot() : domain_error(std::string("Is not ") + typeid(T).name()) {} + IsNothing() : domain_error(std::string("Is nothing")) {} }; struct Nothing {}; -template -struct MaybeTraits; - template class Maybe { public: - typedef MaybeTraits traits; - typedef typename traits::storage storage; - typedef typename traits::pointer pointer; - typedef typename traits::reference reference; + 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 n) : _is_nothing(true), _t() {} + template Maybe(Maybe const &m) + : _is_nothing(m._is_nothing), _t(m._t) {} - Maybe(const Maybe &m) : _is_nothing(m._is_nothing), _t(m._t) {} + template Maybe(T2 const &t) + : _is_nothing(false), _t(t) {}; - template - Maybe(const Maybe &m) - : _is_nothing(m._is_nothing), - _t(traits::to_storage(MaybeTraits::from_storage(m._t))) {} - - template - Maybe(T2 t) : _is_nothing(false), _t(traits::to_storage(t)) {} - - bool isNothing() const { return _is_nothing; } operator bool() const { return !_is_nothing; } - reference assume() const throw(IsNot) { + T const &operator*() const throw(IsNothing) { if (_is_nothing) { - throw IsNot(); + throw IsNothing(); } else { - return traits::from_storage(_t); + return _t; } } - reference operator*() const throw(IsNot) { - return assume(); + T &operator*() throw(IsNothing) { + if (_is_nothing) { + throw IsNothing(); + } else { + return _t; + } } - pointer operator->() const throw(IsNot) { - return &assume(); + + 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; + } } private: bool _is_nothing; - storage _t; + T _t; }; -/* traits classes used by Maybe */ - template -struct MaybeTraits { - typedef T const storage; - typedef T const *pointer; - typedef T const &reference; - static reference to_storage(reference t) { return t; } - static reference from_storage(reference t) { return t; } +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) {} + + template Maybe(Maybe const &m) + : _is_nothing(m._is_nothing), _t(m._t) {} + + template Maybe(T2 const &t) + : _is_nothing(false), _t(t) {}; + + operator bool() const { return !_is_nothing; } + + T const &operator*() const throw(IsNothing) { + if (_is_nothing) { + throw IsNothing(); + } else { + return _t; + } + } + + T const *operator->() const throw(IsNothing) { + if (_is_nothing) { + throw IsNothing(); + } else { + return &_t; + } + } + +private: + bool _is_nothing; + T const _t; }; template -struct MaybeTraits { - typedef T *storage; - typedef T *pointer; - typedef T &reference; - static storage to_storage(reference t) { return &t; } - static reference from_storage(storage t) { return *t; } +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) {} + + template Maybe(Maybe const &m) + : _is_nothing(m._is_nothing), _t(m._t) {} + + template Maybe(T2 &t) + : _is_nothing(false), _t(t) {}; + + operator bool() const { return !_is_nothing; } + + T &operator*() const throw(IsNothing) { + if (_is_nothing) { + throw IsNothing(); + } else { + return _t; + } + } + + T *operator->() const throw(IsNothing) { + if (_is_nothing) { + throw IsNothing(); + } else { + return &_t; + } + } + +private: + bool _is_nothing; + T &_t; }; } /* namespace NR */ diff --git a/src/object-snapper.cpp b/src/object-snapper.cpp index 4a21cbc04..684700b3b 100644 --- a/src/object-snapper.cpp +++ b/src/object-snapper.cpp @@ -131,7 +131,7 @@ void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s, if (o && o->t >= 0 && o->t <= 1) { /* Convert the nearest point back to desktop coordinates */ - NR::Point const o_it = get_point_on_Path(livarot_path, o.assume().piece, o.assume().t); + NR::Point const o_it = get_point_on_Path(livarot_path, o->piece, o->t); NR::Point const o_dt = desktop->doc2dt(o_it * i2doc); NR::Coord const dist = NR::L2(o_dt - p); diff --git a/src/shape-editor.cpp b/src/shape-editor.cpp index 491037bd1..d9eaf11bc 100644 --- a/src/shape-editor.cpp +++ b/src/shape-editor.cpp @@ -229,7 +229,11 @@ bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) { sp_nodepath_ensure_livarot_path(this->nodepath); NR::Maybe position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc); - NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position.assume().piece, position.assume().t); + if (!position) { + return false; + } + + NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t); NR::Point delta = nearest - this->curvepoint_doc; delta = desktop->d2w(delta); @@ -248,8 +252,8 @@ bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) { this->curvepoint_event[NR::X] = (gint) event_p [NR::X]; this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y]; this->hit = true; - this->grab_t = position.assume().t; - this->grab_node = position.assume().piece; + this->grab_t = position->t; + this->grab_node = position->piece; } return close;