From 679ee1f870372694605c97ae16a36ce05a456bd2 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Mon, 30 Jan 2023 19:39:42 +0000 Subject: [PATCH 1/4] Remove use of the safe-bool idiom, replace with explicit operator bool --- BGL/include/CGAL/boost/graph/iterator.h | 138 ++++-------------- STL_Extension/include/CGAL/Object.h | 6 +- .../include/CGAL/Surface_mesh/Properties.h | 7 +- 3 files changed, 30 insertions(+), 121 deletions(-) diff --git a/BGL/include/CGAL/boost/graph/iterator.h b/BGL/include/CGAL/boost/graph/iterator.h index 1fed18a2278..2146193156e 100644 --- a/BGL/include/CGAL/boost/graph/iterator.h +++ b/BGL/include/CGAL/boost/graph/iterator.h @@ -213,16 +213,10 @@ public: {} #ifndef DOXYGEN_RUNNING - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Halfedge_around_source_iterator::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base() == nullptr)) ? - &Halfedge_around_source_iterator::this_type_does_not_support_comparisons : 0; + return (! (this->base() == nullptr)); } bool operator==( const Self& i) const { @@ -313,16 +307,10 @@ public: {} #ifndef DOXYGEN_RUNNING - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Halfedge_around_target_iterator::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base() == nullptr)) ? - &Halfedge_around_target_iterator::this_type_does_not_support_comparisons : 0; + return (! (this->base() == nullptr)); } bool operator==( const Self& i) const { @@ -412,16 +400,9 @@ public: pointer operator -> ( ) { return &pos; } const value_type* operator -> ( ) const { return &pos; } - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Halfedge_around_face_iterator::*bool_type)() const; - - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base() == nullptr)) ? - &Halfedge_around_face_iterator::this_type_does_not_support_comparisons : 0; + return (! (this->base() == nullptr)); } bool operator==( const Self& i) const { @@ -522,16 +503,10 @@ public: Halfedge_around_source_circulator(vertex_descriptor vd, const Graph& g) : Halfedge_around_source_circulator::iterator_adaptor_(Halfedge_around_target_circulator(halfedge(vd,g),g)), opp(g) {} - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Halfedge_around_source_circulator::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &Halfedge_around_source_circulator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } bool operator== (void*) const @@ -580,16 +555,9 @@ public: #ifndef DOXYGEN_RUNNING typedef std::size_t size_type; - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Face_around_target_circulator::*bool_type)() const; - - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &Face_around_target_circulator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } bool operator== (void*) const @@ -654,17 +622,9 @@ public: bool operator == ( const Self& other) const { return g == other.g && pos == other.pos; } bool operator != ( const Self& other) const { return g != other.g || pos != other.pos; } - - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Halfedge_around_target_circulator::*bool_type)() const; - - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (g == nullptr)) ? - &Halfedge_around_target_circulator::this_type_does_not_support_comparisons : 0; + return (! (g == nullptr)); } @@ -751,17 +711,9 @@ public: bool operator == ( const Self& other) const { return g == other.g && pos == other.pos; } bool operator != ( const Self& other) const { return g != other.g || pos != other.pos; } - - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Halfedge_around_face_circulator::*bool_type)() const; - - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (g == nullptr)) ? - &Halfedge_around_face_circulator::this_type_does_not_support_comparisons : 0; + return (! (g == nullptr)); } bool operator== (void* ) const @@ -1008,16 +960,10 @@ public: {} #ifndef DOXYGEN_RUNNING - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Vertex_around_face_circulator::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &Vertex_around_face_circulator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } bool operator== (void*) const @@ -1062,16 +1008,10 @@ public: {} #ifndef DOXYGEN_RUNNING - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Vertex_around_face_iterator::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &Vertex_around_face_iterator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } bool operator== (void*) const @@ -1192,16 +1132,10 @@ public: {} #ifndef DOXYGEN_RUNNING - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Vertex_around_target_circulator::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &Vertex_around_target_circulator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } bool operator== (void*) const @@ -1250,16 +1184,10 @@ public: {} #ifndef DOXYGEN_RUNNING - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Vertex_around_target_iterator::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &Vertex_around_target_iterator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } private: friend class boost::iterator_core_access; @@ -1337,16 +1265,9 @@ public: Out_edge_iterator(halfedge_descriptor h, const Graph& g, int n = 0) : Out_edge_iterator::iterator_adaptor_(Halfedge_around_target_iterator(h,g,(h==halfedge_descriptor())?1:n)), opp(g) {} - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (Out_edge_iterator::*bool_type)() const; - - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &Out_edge_iterator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } @@ -1379,16 +1300,9 @@ public: : In_edge_iterator::iterator_adaptor_(Halfedge_around_target_iterator(h,g,(h==halfedge_descriptor())?1:n)), fct(g) {} - // design pattern: "safe bool" - // will be replaced by explicit operator bool with C++11 - typedef void (In_edge_iterator::*bool_type)() const; - - void this_type_does_not_support_comparisons() const {} - - operator bool_type() const + explicit operator bool() const { - return (! (this->base_reference() == nullptr)) ? - &In_edge_iterator::this_type_does_not_support_comparisons : 0; + return (! (this->base_reference() == nullptr)); } private: diff --git a/STL_Extension/include/CGAL/Object.h b/STL_Extension/include/CGAL/Object.h index fdfd59bd514..e0efd971006 100644 --- a/STL_Extension/include/CGAL/Object.h +++ b/STL_Extension/include/CGAL/Object.h @@ -52,8 +52,6 @@ class Object template friend T object_cast(const Object & o); - typedef void (Object::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} public: struct private_tag{}; @@ -97,8 +95,8 @@ class Object } // safe-bool conversion - operator bool_type() const { - return empty() == false ? &Object::this_type_does_not_support_comparisons : 0; + explicit operator bool() const { + return !empty(); } diff --git a/Surface_mesh/include/CGAL/Surface_mesh/Properties.h b/Surface_mesh/include/CGAL/Surface_mesh/Properties.h index 9fc752bb663..f0387f9790f 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/Properties.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/Properties.h @@ -538,8 +538,6 @@ class Property_map_base CRTP_derived_class> /// @endcond { - typedef void (Property_map_base::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} public: typedef I key_type; typedef T value_type; @@ -596,9 +594,8 @@ public: /// can be used, and \c false otherwise. operator bool () const; #else - operator bool_type() const { - return parray_ != nullptr ? - &Property_map_base::this_type_does_not_support_comparisons : 0; + explicit operator bool() const { + return parray_ != nullptr; } #endif /// Access the property associated with the key \c i. From ff6b741afcf2175f5df3cd1d79d403ed37bf98e6 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Mon, 30 Jan 2023 20:31:19 +0000 Subject: [PATCH 2/4] Is a comparison operator needed --- Surface_mesh/include/CGAL/Surface_mesh/Properties.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Surface_mesh/include/CGAL/Surface_mesh/Properties.h b/Surface_mesh/include/CGAL/Surface_mesh/Properties.h index f0387f9790f..3bca0eef860 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/Properties.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/Properties.h @@ -598,6 +598,11 @@ public: return parray_ != nullptr; } #endif + + bool operator!=(const Property_map_base& pm) const { + return parray_ != pm.parray_; + } + /// Access the property associated with the key \c i. reference operator[](const I& i) { From 2c7f7751a82b950f5c6422a819db6b4023509cad Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Mon, 30 Jan 2023 20:47:18 +0000 Subject: [PATCH 3/4] Add equal operator to Properties.h --- Surface_mesh/include/CGAL/Surface_mesh/Properties.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Surface_mesh/include/CGAL/Surface_mesh/Properties.h b/Surface_mesh/include/CGAL/Surface_mesh/Properties.h index 3bca0eef860..31eea166ee5 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/Properties.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/Properties.h @@ -599,6 +599,10 @@ public: } #endif + bool operator==(const Property_map_base& pm) const { + return parray_ == pm.parray_; + } + bool operator!=(const Property_map_base& pm) const { return parray_ != pm.parray_; } From c330beb50409db9ec3b16b330adfa7413a817cd7 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Fri, 14 Apr 2023 17:04:02 +0100 Subject: [PATCH 4/4] Add operator!=(void*) --- BGL/include/CGAL/boost/graph/iterator.h | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/BGL/include/CGAL/boost/graph/iterator.h b/BGL/include/CGAL/boost/graph/iterator.h index 2146193156e..66b088eac04 100644 --- a/BGL/include/CGAL/boost/graph/iterator.h +++ b/BGL/include/CGAL/boost/graph/iterator.h @@ -514,6 +514,11 @@ public: return this->base_reference() == nullptr; } + bool operator!= (void*) const + { + return this->base_reference() != nullptr; + } + private: friend class boost::iterator_core_access; typename boost::graph_traits::halfedge_descriptor dereference() const { return opp(*this->base_reference()); } @@ -565,6 +570,11 @@ public: return this->base_reference() == nullptr; } + bool operator!= (void*) const + { + return this->base_reference() != nullptr; + } + private: friend class boost::iterator_core_access; @@ -633,6 +643,11 @@ public: return g == nullptr; } + bool operator!= (void* ) const + { + return g != nullptr; + } + Self& operator++() { @@ -721,6 +736,11 @@ public: return g == nullptr; } + bool operator!= (void* ) const + { + return g != nullptr; + } + Self& operator++() { CGAL_assertion(g != nullptr); @@ -970,6 +990,12 @@ public: { return this->base_reference()== nullptr; } + + bool operator!= (void*) const + { + return this->base_reference()!= nullptr; + } + private: friend class boost::iterator_core_access; typename boost::graph_traits::vertex_descriptor dereference() const { return fct(*this->base_reference()); } @@ -1143,6 +1169,11 @@ public: return this->base_reference()== nullptr; } + bool operator!= (void*) const + { + return this->base_reference()!= nullptr; + } + private: friend class boost::iterator_core_access; typename boost::graph_traits::vertex_descriptor dereference() const { return fct(*this->base_reference()); }