You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

  1. #include <boost/optional.hpp>
  2. namespace pybind11 {
  3. namespace detail {
  4. template<typename T> struct boost_optional_caster {
  5. using value_conv = make_caster<typename T::value_type>;
  6. static handle cast(const T& src, return_value_policy policy, handle parent) {
  7. if (!src)
  8. return none().inc_ref();
  9. return value_conv::cast(*src, policy, parent);
  10. }
  11. bool load(handle src, bool convert) {
  12. if (!src) {
  13. return false;
  14. } else if (src.is_none()) {
  15. value = boost::none; // nullopt
  16. return true;
  17. }
  18. value_conv inner_caster;
  19. if (!inner_caster.load(src, convert))
  20. return false;
  21. value.emplace(cast_op<typename T::value_type>(inner_caster));
  22. return true;
  23. }
  24. PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name() + _("]"));
  25. };
  26. template<typename T> struct type_caster<boost::optional<T>>
  27. : public boost_optional_caster<boost::optional<T>> {};
  28. template<> struct type_caster<boost::none_t>
  29. : public void_caster<boost::none_t> {};
  30. }
  31. }