working version of Pair_optional_adaptor

This commit is contained in:
Sven Oesau 2024-02-06 10:18:35 +01:00
parent 59ab39997e
commit 4c1cf8d9c4
3 changed files with 17 additions and 20 deletions

View File

@ -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 <typename T>
std::pair<Property_map<T>, bool>
Pair_optional_adaptor<Property_map<T>>
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<Property_map<T>, bool>(Property_map<T>(p.first), p.second);

View File

@ -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<typename Octree::Property_map<int>, bool> p3 = tree.add_property("test2", int(0));
std::optional<typename Octree::Property_map<int>> o3 = tree.add_property("test3", int(0));
auto a4 = tree.add_property("test", int(0));
std::pair<typename Octree::Property_map<int>, bool> p4 = tree.add_property("test", int(0));
std::optional<typename Octree::Property_map<int>> o4 = tree.add_property("test", int(0));
auto a1 = tree.property<int>("test");
std::pair<typename Octree::Property_map<int>, bool> p1 = tree.property<int>("test");
std::optional<typename Octree::Property_map<int>> o1 = tree.property<int>("test");

View File

@ -19,40 +19,29 @@ namespace CGAL {
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;
Pair_optional_adaptor(std::optional<T>& obj) : std::optional<T>(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<T>(std::nullopt), second(false), first(u.t) {
std::cout << "nullopt constructor" << std::endl;
}
Pair_optional_adaptor(const std::nullopt_t& obj) : std::optional<T>(std::nullopt), second(false), first(t_storage.t) {}
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;
}
Pair_optional_adaptor(std::pair<T, bool>& p) : std::optional<T>(p.second ? p.first : std::optional<T>()), first(t_storage.t), second(p.second), t_storage(p.first) {}
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 {
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