From b3c5764c32a9364ae37faee60838969b116cc524 Mon Sep 17 00:00:00 2001 From: Sebastian Junges Date: Thu, 12 Jan 2017 22:34:45 +0100 Subject: [PATCH] support for boost;:optional --- src/boost.h | 39 +++++++++++++++++++++++++++++++++++++++ src/common.h | 2 ++ 2 files changed, 41 insertions(+) create mode 100644 src/boost.h diff --git a/src/boost.h b/src/boost.h new file mode 100644 index 0000000..9dca22d --- /dev/null +++ b/src/boost.h @@ -0,0 +1,39 @@ +#include + +namespace pybind11 { + namespace detail { +template struct boost_optional_caster { + using value_conv = make_caster; + + static handle cast(const T& src, return_value_policy policy, handle parent) { + if (!src) + return none().inc_ref(); + return value_conv::cast(*src, policy, parent); + } + + bool load(handle src, bool convert) { + if (!src) { + return false; + } else if (src.is_none()) { + value = boost::none; // nullopt + return true; + } + value_conv inner_caster; + if (!inner_caster.load(src, convert)) + return false; + + value.emplace(cast_op(inner_caster)); + return true; + } + + PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name() + _("]")); +}; + +template struct type_caster> + : public boost_optional_caster> {}; + +template<> struct type_caster + : public void_caster {}; + + } +} diff --git a/src/common.h b/src/common.h index 7fbfb27..da1e0f2 100644 --- a/src/common.h +++ b/src/common.h @@ -20,3 +20,5 @@ using namespace pybind11::literals; PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr) PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr) + +#include "boost.h"