diff --git a/Orthtree/include/CGAL/Orthtree.h b/Orthtree/include/CGAL/Orthtree.h index dc322ee8064..923885fe276 100644 --- a/Orthtree/include/CGAL/Orthtree.h +++ b/Orthtree/include/CGAL/Orthtree.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -526,7 +527,7 @@ public: \return an optional containing the property map if it exists */ template - std::optional> + Pair_optional_adaptor> node_property(const std::string& name) { auto p = m_node_properties.template get_property_if_exists(name); if (p) diff --git a/Orthtree/test/Orthtree/test_octree_custom_properties.cpp b/Orthtree/test/Orthtree/test_octree_custom_properties.cpp index 5a8ccb3329a..8b9a7927513 100644 --- a/Orthtree/test/Orthtree/test_octree_custom_properties.cpp +++ b/Orthtree/test/Orthtree/test_octree_custom_properties.cpp @@ -39,15 +39,27 @@ int main(void) { auto prop5 = tree.add_node_property("test", int(0)); assert(!prop5.second); - auto prop3 = tree.node_property("test"); - assert(prop3.has_value()); + auto a1 = tree.node_property("test"); + std::pair, bool> p1 = tree.node_property("test"); + std::optional> o1 = tree.node_property("test"); + auto f = a1.first; + auto pf1 = p1.first; + auto of1 = o1.value(); + auto fo1 = a1.value(); + std::cout << f.size() << std::endl; + + auto a2 = tree.node_property("test"); + std::pair, bool> p2 = tree.node_property("test"); + std::optional> o2 = tree.node_property("test"); + + //assert(prop3.has_value()); auto prop4 = tree.node_property("test"); assert(!prop4.has_value()); // removal of properties num = tree.properties().size(); - tree.remove_node_property(*prop3); + //tree.remove_node_property(*prop3); assert(tree.properties().size() == (num - 1)); // Default value should be respected diff --git a/STL_Extension/include/CGAL/Pair_optional_adaptor.h b/STL_Extension/include/CGAL/Pair_optional_adaptor.h new file mode 100644 index 00000000000..e7b35ce2da0 --- /dev/null +++ b/STL_Extension/include/CGAL/Pair_optional_adaptor.h @@ -0,0 +1,60 @@ +// Copyright (c) 2024 GeometryFactory Sarl (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial +// +// +// Author(s) : Sven Oesau + +#ifndef CGAL_PAIR_OPTIONAL_ADAPTOR +#define CGAL_PAIR_OPTIONAL_ADAPTOR + +namespace CGAL { + +// T is supposed to be a handle +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; + if (obj.has_value()) + u.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(std::pair& p) : std::optional(b ? p.first : std::nullopt), first(p.first), second(b) { + std::cout << "pair constructor" << std::endl; + } + + 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 { + T t; + int i; + U() : i(0) {} + U(T t) : t(t) {} + } u; +}; + +} // CGAL + +#endif \ No newline at end of file