From 53048f81c9d59abb522040bb939679c9bfff4da7 Mon Sep 17 00:00:00 2001 From: mental Date: Sun, 4 Mar 2007 02:08:21 +0000 Subject: [PATCH] make conversions away from NR::Maybe explicit --- src/event-context.cpp | 4 ++-- src/libnr/nr-maybe.h | 50 ++++++++++-------------------------------- src/node-context.cpp | 6 ++--- src/nodepath.cpp | 12 ++++++++-- src/object-snapper.cpp | 2 +- src/select-context.cpp | 6 ++--- src/zoom-context.cpp | 4 ++-- 7 files changed, 32 insertions(+), 52 deletions(-) diff --git a/src/event-context.cpp b/src/event-context.cpp index 0cf4f8924..7a3de5e85 100644 --- a/src/event-context.cpp +++ b/src/event-context.cpp @@ -463,8 +463,8 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context, zoom_rb = 0; NR::Maybe const b = Inkscape::Rubberband::get()->getRectangle(); Inkscape::Rubberband::get()->stop(); - if (b != NR::Nothing() && !within_tolerance) { - desktop->set_display_area(b.assume(), 10); + if (b && !within_tolerance) { + desktop->set_display_area(*b, 10); } ret = TRUE; } diff --git a/src/libnr/nr-maybe.h b/src/libnr/nr-maybe.h index 6eed01ec1..9c1aa0708 100644 --- a/src/libnr/nr-maybe.h +++ b/src/libnr/nr-maybe.h @@ -29,27 +29,7 @@ public: IsNot() : domain_error(std::string("Is not ") + typeid(T).name()) {} }; -/** A type with only one value, which (in principle) is only equal to itself. - * - * Types that may (at runtime) pretend to be Nothing need only provide an - * operator bool operator==(Type, Nothing); the rest of the operator - * definitions will be taken care of automatically. - * - * Such types should also provide a casting operator to Nothing, obviously. - */ -struct Nothing { - bool operator==(Nothing n) { return true; } - bool operator!=(Nothing n) { return false; } - template - bool operator==(T t) { return t == *this; } - template - bool operator!=(T t) { return t != *this; } -}; - -template -bool operator==(T t, Nothing n) { return false; } -template -bool operator!=(T t, Nothing n) { return !( t == n ); } +struct Nothing {}; template struct MaybeTraits; @@ -59,6 +39,7 @@ class Maybe { public: typedef MaybeTraits traits; typedef typename traits::storage storage; + typedef typename traits::pointer pointer; typedef typename traits::reference reference; Maybe(Nothing n) : _is_nothing(true), _t() {} @@ -73,32 +54,21 @@ public: template Maybe(T2 t) : _is_nothing(false), _t(traits::to_storage(t)) {} - reference assume() const throw(IsNot) { - if (_is_nothing) { - throw IsNot(); - } else { - return traits::from_storage(_t); - } - } + bool isNothing() const { return _is_nothing; } + operator bool() const { return !_is_nothing; } - operator reference() const throw(IsNot) { + reference assume() const throw(IsNot) { if (_is_nothing) { throw IsNot(); } else { return traits::from_storage(_t); } } - operator Nothing() const throw(IsNot) { - if (!_is_nothing) { - throw IsNot(); - } else { - return Nothing(); - } + reference operator*() const throw(IsNot) { + return assume(); } - - bool operator==(Nothing n) { return _is_nothing; } - bool operator==(reference r) { - return traits::from_storage(_t) == r; + pointer operator->() const throw(IsNot) { + return &assume(); } private: @@ -111,6 +81,7 @@ private: 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; } @@ -119,6 +90,7 @@ struct MaybeTraits { 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; } diff --git a/src/node-context.cpp b/src/node-context.cpp index 235b89db3..352bac584 100644 --- a/src/node-context.cpp +++ b/src/node-context.cpp @@ -419,8 +419,8 @@ sp_node_context_root_handler(SPEventContext *event_context, GdkEvent *event) if (nc->shape_editor->hits_curve() && !event_context->within_tolerance) { //drag curve nc->shape_editor->finish_drag(); - } else if (b != NR::Nothing() && !event_context->within_tolerance) { // drag to select - nc->shape_editor->select_rect(b.assume(), event->button.state & GDK_SHIFT_MASK); + } else if (b && !event_context->within_tolerance) { // drag to select + nc->shape_editor->select_rect(*b, event->button.state & GDK_SHIFT_MASK); } else { if (!(nc->rb_escaped)) { // unless something was cancelled if (nc->shape_editor->has_selection()) @@ -584,7 +584,7 @@ sp_node_context_root_handler(SPEventContext *event_context, GdkEvent *event) case GDK_Escape: { NR::Maybe const b = Inkscape::Rubberband::get()->getRectangle(); - if (b != NR::Nothing()) { + if (b) { Inkscape::Rubberband::get()->stop(); nc->current_state = SP_NODE_CONTEXT_INACTIVE; nc->rb_escaped = true; diff --git a/src/nodepath.cpp b/src/nodepath.cpp index 78d76404d..1d854418b 100644 --- a/src/nodepath.cpp +++ b/src/nodepath.cpp @@ -1549,7 +1549,11 @@ sp_nodepath_select_segment_near_point(Inkscape::NodePath::Path *nodepath, NR::Po } sp_nodepath_ensure_livarot_path(nodepath); - Path::cut_position position = get_nearest_position_on_Path(nodepath->livarot_path, p); + NR::Maybe maybe_position = get_nearest_position_on_Path(nodepath->livarot_path, p); + if (!maybe_position) { + return; + } + Path::cut_position position = *maybe_position; //find segment to segment Inkscape::NodePath::Node *e = sp_nodepath_get_node_by_index(position.piece); @@ -1581,7 +1585,11 @@ sp_nodepath_add_node_near_point(Inkscape::NodePath::Path *nodepath, NR::Point p) } sp_nodepath_ensure_livarot_path(nodepath); - Path::cut_position position = get_nearest_position_on_Path(nodepath->livarot_path, p); + NR::Maybe maybe_position = get_nearest_position_on_Path(nodepath->livarot_path, p); + if (!maybe_position) { + return; + } + Path::cut_position position = *maybe_position; //find segment to split Inkscape::NodePath::Node *e = sp_nodepath_get_node_by_index(position.piece); diff --git a/src/object-snapper.cpp b/src/object-snapper.cpp index 6b51f8508..4a21cbc04 100644 --- a/src/object-snapper.cpp +++ b/src/object-snapper.cpp @@ -128,7 +128,7 @@ void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s, /* Look for the nearest position on this SPItem to our snap point */ NR::Maybe const o = get_nearest_position_on_Path(livarot_path, p_it); - if (o != NR::Nothing() && o.assume().t >= 0 && o.assume().t <= 1) { + 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); diff --git a/src/select-context.cpp b/src/select-context.cpp index 5d8babe57..e1649e34f 100644 --- a/src/select-context.cpp +++ b/src/select-context.cpp @@ -229,7 +229,7 @@ sp_select_context_abort(SPEventContext *event_context) } } else { NR::Maybe const b = Inkscape::Rubberband::get()->getRectangle(); - if (b != NR::Nothing()) { + if (b) { Inkscape::Rubberband::get()->stop(); rb_escaped = 1; SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selection canceled.")); @@ -567,12 +567,12 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event) sc->item = NULL; } else { NR::Maybe const b = Inkscape::Rubberband::get()->getRectangle(); - if (b != NR::Nothing() && !within_tolerance) { + if (b && !within_tolerance) { // this was a rubberband drag Inkscape::Rubberband::get()->stop(); seltrans->resetState(); // find out affected items: - GSList *items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, b.assume()); + GSList *items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b); if (event->button.state & GDK_SHIFT_MASK) { // with shift, add to selection selection->addList (items); diff --git a/src/zoom-context.cpp b/src/zoom-context.cpp index 62395581f..d2b0d6849 100644 --- a/src/zoom-context.cpp +++ b/src/zoom-context.cpp @@ -163,8 +163,8 @@ static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent case GDK_BUTTON_RELEASE: if ( event->button.button == 1 ) { NR::Maybe const b = Inkscape::Rubberband::get()->getRectangle(); - if (b != NR::Nothing() && !within_tolerance) { - desktop->set_display_area(b.assume(), 10); + if (b && !within_tolerance) { + desktop->set_display_area(*b, 10); } else if (!escaped) { NR::Point const button_w(event->button.x, event->button.y); NR::Point const button_dt(desktop->w2d(button_w)); -- 2.30.2