From 4c1cf8d9c4269346f2b7b0a67d9ac4ed3c084eda Mon Sep 17 00:00:00 2001 From: Sven Oesau Date: Tue, 6 Feb 2024 10:18:35 +0100 Subject: [PATCH] working version of Pair_optional_adaptor --- Orthtree/include/CGAL/Orthtree.h | 2 +- .../test_octree_custom_properties.cpp | 8 ++++++ .../include/CGAL/Pair_optional_adaptor.h | 27 ++++++------------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/Orthtree/include/CGAL/Orthtree.h b/Orthtree/include/CGAL/Orthtree.h index ca7bec5de3d..dc2e41555d1 100644 --- a/Orthtree/include/CGAL/Orthtree.h +++ b/Orthtree/include/CGAL/Orthtree.h @@ -511,7 +511,7 @@ public: \return pair of the property map and a Boolean which is `true` if the property needed to be created */ template - std::pair, bool> + Pair_optional_adaptor> add_property(const std::string& name, const T default_value = T()) { auto p = m_node_properties.get_or_add_property(name, default_value); return std::pair, bool>(Property_map(p.first), p.second); diff --git a/Orthtree/test/Orthtree/test_octree_custom_properties.cpp b/Orthtree/test/Orthtree/test_octree_custom_properties.cpp index e75837a0312..0e14a51fbbc 100644 --- a/Orthtree/test/Orthtree/test_octree_custom_properties.cpp +++ b/Orthtree/test/Orthtree/test_octree_custom_properties.cpp @@ -39,6 +39,14 @@ int main(void) { auto prop5 = tree.add_property("test", int(0)); assert(!prop5.second); + auto a3 = tree.add_property("test1", int(0)); + std::pair, bool> p3 = tree.add_property("test2", int(0)); + std::optional> o3 = tree.add_property("test3", int(0)); + + auto a4 = tree.add_property("test", int(0)); + std::pair, bool> p4 = tree.add_property("test", int(0)); + std::optional> o4 = tree.add_property("test", int(0)); + auto a1 = tree.property("test"); std::pair, bool> p1 = tree.property("test"); std::optional> o1 = tree.property("test"); diff --git a/STL_Extension/include/CGAL/Pair_optional_adaptor.h b/STL_Extension/include/CGAL/Pair_optional_adaptor.h index 1620edbf3b2..49c5c076dfd 100644 --- a/STL_Extension/include/CGAL/Pair_optional_adaptor.h +++ b/STL_Extension/include/CGAL/Pair_optional_adaptor.h @@ -19,40 +19,29 @@ namespace CGAL { template class Pair_optional_adaptor : public std::optional { public: - Pair_optional_adaptor(std::optional& obj) : std::optional(obj), second(obj.has_value()), first(u.t) { - std::cout << "optional constructor" << std::endl; + Pair_optional_adaptor(std::optional& obj) : std::optional(obj), second(obj.has_value()), first(t_storage.t) { if (obj.has_value()) - u.t = *obj; + t_storage.t = *obj; } - Pair_optional_adaptor(const std::nullopt_t& obj) : std::optional(std::nullopt), second(false), first(u.t) { - std::cout << "nullopt constructor" << std::endl; - } + Pair_optional_adaptor(const std::nullopt_t& obj) : std::optional(std::nullopt), second(false), first(t_storage.t) {} - Pair_optional_adaptor(std::pair& p) : std::optional(b ? p.first : std::nullopt), first(p.first), second(b) { - std::cout << "pair constructor" << std::endl; - } + Pair_optional_adaptor(std::pair& p) : std::optional(p.second ? p.first : std::optional()), first(t_storage.t), second(p.second), t_storage(p.first) {} operator std::pair() { return std::pair(first, second); } - operator std::optional() { - if (second) - return std::optional(first); - else return std::nullopt; - } - T &first; bool second; private: - union U { + union T_value { T t; int i; - U() : i(0) {} - U(T t) : t(t) {} - } u; + T_value() : i(0) {} + T_value(T t) : t(t) {} + } t_storage; }; } // CGAL