From 161a313109433e6c8f6283ccac8d66de98d0ffdb Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Wed, 4 Jun 2025 00:51:02 +0200 Subject: [PATCH] fix the move semantic of Constrained_triangulation_plus_2 The comparison functor passed to the constraints hierarchy has a pointer to the `Constrained_triangulation_plus_2` that contains it. That means the defaulted move constructor and move assignment operator of `Polyline_constraint_hierarchy_2` cannot be used to implement the move constructor and move assignment operator of `Constrained_triangulation_plus_2`. Add a special constructor for `Polyline_constraint_hierarchy_2`, that is usee to move the hierarchy and reassign the comparison operator. --- .../CGAL/Constrained_triangulation_plus_2.h | 17 +++++++++++-- .../Polyline_constraint_hierarchy_2.h | 25 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Triangulation_2/include/CGAL/Constrained_triangulation_plus_2.h b/Triangulation_2/include/CGAL/Constrained_triangulation_plus_2.h index 4b33c7ad456..4e23c81fcb3 100644 --- a/Triangulation_2/include/CGAL/Constrained_triangulation_plus_2.h +++ b/Triangulation_2/include/CGAL/Constrained_triangulation_plus_2.h @@ -206,7 +206,13 @@ public: : Constrained_triangulation_plus_2(ctp.geom_traits()) { copy_triangulation(ctp);} - Constrained_triangulation_plus_2(Constrained_triangulation_plus_2&&) = default; + Constrained_triangulation_plus_2(Constrained_triangulation_plus_2&& other_ctp) noexcept + : Triangulation(std::move(other_ctp)) + , Constraint_hierarchy(std::move(other_ctp.hierarchy_ref()), Vertex_handle_compare(this)) + { + // The hierarchy is moved, so the vertex handles are still valid. + // The triangulation is moved, so the vertex handles are still valid. + } ~Constrained_triangulation_plus_2() override {} @@ -216,7 +222,14 @@ public: return *this; } - Constrained_triangulation_plus_2& operator=(Constrained_triangulation_plus_2&&) = default; + Constrained_triangulation_plus_2& operator=(Constrained_triangulation_plus_2&& other_ctp) noexcept { + if (this != &other_ctp) { + static_cast(*this) = std::move(other_ctp); + static_cast(*this) = + Constraint_hierarchy(std::move(other_ctp.hierarchy_ref()), Vertex_handle_compare(this)); + } + return *this; + } template Constrained_triangulation_plus_2(InputIterator first, diff --git a/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h b/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h index f39a525c035..85e91b0d5c8 100644 --- a/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h +++ b/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h @@ -524,13 +524,29 @@ private: struct Priv { // encapsulate the private members in a struct, to detect direct access to them Priv(Compare comp) : comp(comp) - #if CGAL_USE_BARE_STD_MAP +#if CGAL_USE_BARE_STD_MAP , sc_to_c_map(Pair_compare(comp)) #else , sc_to_c_map() #endif {} + Priv(const Priv&) = default; + Priv(Priv&&) = default; + Priv& operator=(const Priv&) = default; + Priv& operator=(Priv&&) = default; + + Priv(Priv&& other, const Compare& comp) noexcept + : comp(comp) + , sc_to_c_map(std::move(other.sc_to_c_map)) + , free_ids(std::move(other.free_ids)) + , constraints_set(std::move(other.constraints_set)) + { +#if CGAL_USE_BARE_STD_MAP + CGAL_error_msg("This constructor cannot be used with CGAL_USE_BARE_STD_MAP"); +#endif + } + Compare comp; Sc_to_c_map sc_to_c_map; std::stack> free_ids; @@ -538,10 +554,17 @@ private: } priv; public: Polyline_constraint_hierarchy_2(const Compare& comp) : priv(comp) {} + Polyline_constraint_hierarchy_2(const Polyline_constraint_hierarchy_2& ch) : priv(ch.priv.comp) { copy(ch); } + Polyline_constraint_hierarchy_2(Polyline_constraint_hierarchy_2&&) = default; + Polyline_constraint_hierarchy_2(Polyline_constraint_hierarchy_2&& other, const Compare& comp) noexcept + : priv(std::move(other.priv), comp) + {} + ~Polyline_constraint_hierarchy_2() { clear(); } + void clear(); Polyline_constraint_hierarchy_2& operator=(const Polyline_constraint_hierarchy_2& ch) { return copy(ch); }