Skip to content

Commit 0afe3c9

Browse files
committed
optional: add support for std::optional under cpp17
1 parent 3666ff0 commit 0afe3c9

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

include/eigenpy/optional.hpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,53 @@
88
#include "eigenpy/fwd.hpp"
99
#include "eigenpy/eigen-from-python.hpp"
1010
#include <boost/optional.hpp>
11+
#ifdef EIGENPY_WITH_CXX17_SUPPORT
12+
#include <optional>
13+
#endif
1114

15+
#ifndef EIGENPY_DEFAULT_OPTIONAL
1216
#define EIGENPY_DEFAULT_OPTIONAL boost::optional
17+
#endif
1318

1419
namespace boost {
1520
namespace python {
1621
namespace converter {
1722

1823
template <typename T>
19-
struct expected_pytype_for_arg<EIGENPY_DEFAULT_OPTIONAL<T> >
24+
struct expected_pytype_for_arg<boost::optional<T> >
2025
: expected_pytype_for_arg<T> {};
2126

27+
#ifdef EIGENPY_WITH_CXX17_SUPPORT
28+
template <typename T>
29+
struct expected_pytype_for_arg<std::optional<T> >
30+
: expected_pytype_for_arg<T> {};
31+
#endif
32+
2233
} // namespace converter
2334
} // namespace python
2435
} // namespace boost
2536

2637
namespace eigenpy {
2738
namespace detail {
2839

40+
/// Helper struct to decide which type is the "none" type for a specific optional<T> implementation.
41+
template<template <typename> class OptionalTpl>
42+
struct nullopt_helper {};
43+
44+
template<>
45+
struct nullopt_helper<boost::optional> {
46+
typedef boost::none_t type;
47+
static type value() { return boost::none; }
48+
};
49+
50+
#ifdef EIGENPY_WITH_CXX17_SUPPORT
51+
template<>
52+
struct nullopt_helper<std::optional> {
53+
typedef std::nullopt_t type;
54+
static type value() { return std::nullopt; }
55+
};
56+
#endif
57+
2958
template <typename T,
3059
template <typename> class OptionalTpl = EIGENPY_DEFAULT_OPTIONAL>
3160
struct OptionalToPython {
@@ -80,7 +109,7 @@ void OptionalFromPython<T, OptionalTpl>::construct(
80109
->storage.bytes;
81110

82111
if (obj_ptr == Py_None) {
83-
new (storage) OptionalTpl<T>(boost::none);
112+
new (storage) OptionalTpl<T>(nullopt_helper<OptionalTpl>::value());
84113
} else {
85114
const T value = bp::extract<T>(obj_ptr);
86115
new (storage) OptionalTpl<T>(value);

0 commit comments

Comments
 (0)