added Pair_optional_adaptor (WIP)

This commit is contained in:
Sven Oesau 2024-02-05 18:06:39 +01:00
parent cde61a5c49
commit 3310585228
3 changed files with 77 additions and 4 deletions

View File

@ -27,6 +27,7 @@
#include <CGAL/intersections.h> #include <CGAL/intersections.h>
#include <CGAL/squared_distance_3.h> #include <CGAL/squared_distance_3.h>
#include <CGAL/span.h> #include <CGAL/span.h>
#include <CGAL/Pair_optional_adaptor.h>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/iterator/iterator_facade.hpp> #include <boost/iterator/iterator_facade.hpp>
@ -526,7 +527,7 @@ public:
\return an optional containing the property map if it exists \return an optional containing the property map if it exists
*/ */
template <typename T> template <typename T>
std::optional<Property_map<T>> Pair_optional_adaptor<Property_map<T>>
node_property(const std::string& name) { node_property(const std::string& name) {
auto p = m_node_properties.template get_property_if_exists<T>(name); auto p = m_node_properties.template get_property_if_exists<T>(name);
if (p) if (p)

View File

@ -39,15 +39,27 @@ int main(void) {
auto prop5 = tree.add_node_property("test", int(0)); auto prop5 = tree.add_node_property("test", int(0));
assert(!prop5.second); assert(!prop5.second);
auto prop3 = tree.node_property<int>("test"); auto a1 = tree.node_property<int>("test");
assert(prop3.has_value()); std::pair<typename Octree::Property_map<int>, bool> p1 = tree.node_property<int>("test");
std::optional<typename Octree::Property_map<int>> o1 = tree.node_property<int>("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<std::string>("test");
std::pair<typename Octree::Property_map<std::string>, bool> p2 = tree.node_property<std::string>("test");
std::optional<typename Octree::Property_map<std::string>> o2 = tree.node_property<std::string>("test");
//assert(prop3.has_value());
auto prop4 = tree.node_property<std::string>("test"); auto prop4 = tree.node_property<std::string>("test");
assert(!prop4.has_value()); assert(!prop4.has_value());
// removal of properties // removal of properties
num = tree.properties().size(); num = tree.properties().size();
tree.remove_node_property(*prop3); //tree.remove_node_property(*prop3);
assert(tree.properties().size() == (num - 1)); assert(tree.properties().size() == (num - 1));
// Default value should be respected // Default value should be respected

View File

@ -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<typename T>
class Pair_optional_adaptor : public std::optional<T> {
public:
Pair_optional_adaptor(std::optional<T>& obj) : std::optional<T>(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<T>(std::nullopt), second(false), first(u.t) {
std::cout << "nullopt constructor" << std::endl;
}
Pair_optional_adaptor(std::pair<T, bool>& p) : std::optional<T>(b ? p.first : std::nullopt), first(p.first), second(b) {
std::cout << "pair constructor" << std::endl;
}
operator std::pair<T, bool>() {
return std::pair<T, bool>(first, second);
}
operator std::optional<T>() {
if (second)
return std::optional<T>(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