Skip to content
Snippets Groups Projects
Commit 0afe3c9c authored by Wilson Jallet's avatar Wilson Jallet :clapper:
Browse files

optional: add support for std::optional under cpp17

parent 3666ff02
No related branches found
No related tags found
No related merge requests found
...@@ -8,17 +8,28 @@ ...@@ -8,17 +8,28 @@
#include "eigenpy/fwd.hpp" #include "eigenpy/fwd.hpp"
#include "eigenpy/eigen-from-python.hpp" #include "eigenpy/eigen-from-python.hpp"
#include <boost/optional.hpp> #include <boost/optional.hpp>
#ifdef EIGENPY_WITH_CXX17_SUPPORT
#include <optional>
#endif
#ifndef EIGENPY_DEFAULT_OPTIONAL
#define EIGENPY_DEFAULT_OPTIONAL boost::optional #define EIGENPY_DEFAULT_OPTIONAL boost::optional
#endif
namespace boost { namespace boost {
namespace python { namespace python {
namespace converter { namespace converter {
template <typename T> template <typename T>
struct expected_pytype_for_arg<EIGENPY_DEFAULT_OPTIONAL<T> > struct expected_pytype_for_arg<boost::optional<T> >
: expected_pytype_for_arg<T> {}; : expected_pytype_for_arg<T> {};
#ifdef EIGENPY_WITH_CXX17_SUPPORT
template <typename T>
struct expected_pytype_for_arg<std::optional<T> >
: expected_pytype_for_arg<T> {};
#endif
} // namespace converter } // namespace converter
} // namespace python } // namespace python
} // namespace boost } // namespace boost
...@@ -26,6 +37,24 @@ struct expected_pytype_for_arg<EIGENPY_DEFAULT_OPTIONAL<T> > ...@@ -26,6 +37,24 @@ struct expected_pytype_for_arg<EIGENPY_DEFAULT_OPTIONAL<T> >
namespace eigenpy { namespace eigenpy {
namespace detail { namespace detail {
/// Helper struct to decide which type is the "none" type for a specific optional<T> implementation.
template<template <typename> class OptionalTpl>
struct nullopt_helper {};
template<>
struct nullopt_helper<boost::optional> {
typedef boost::none_t type;
static type value() { return boost::none; }
};
#ifdef EIGENPY_WITH_CXX17_SUPPORT
template<>
struct nullopt_helper<std::optional> {
typedef std::nullopt_t type;
static type value() { return std::nullopt; }
};
#endif
template <typename T, template <typename T,
template <typename> class OptionalTpl = EIGENPY_DEFAULT_OPTIONAL> template <typename> class OptionalTpl = EIGENPY_DEFAULT_OPTIONAL>
struct OptionalToPython { struct OptionalToPython {
...@@ -80,7 +109,7 @@ void OptionalFromPython<T, OptionalTpl>::construct( ...@@ -80,7 +109,7 @@ void OptionalFromPython<T, OptionalTpl>::construct(
->storage.bytes; ->storage.bytes;
if (obj_ptr == Py_None) { if (obj_ptr == Py_None) {
new (storage) OptionalTpl<T>(boost::none); new (storage) OptionalTpl<T>(nullopt_helper<OptionalTpl>::value());
} else { } else {
const T value = bp::extract<T>(obj_ptr); const T value = bp::extract<T>(obj_ptr);
new (storage) OptionalTpl<T>(value); new (storage) OptionalTpl<T>(value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment