Program Listing for File std-unique-ptr.hpp

Return to documentation for file (/tmp/ws/src/eigenpy/include/eigenpy/std-unique-ptr.hpp)

//
// Copyright (c) 2024 INRIA
//

#ifndef __eigenpy_utils_std_unique_ptr_hpp__
#define __eigenpy_utils_std_unique_ptr_hpp__

#include "eigenpy/fwd.hpp"
#include "eigenpy/utils/traits.hpp"
#include "eigenpy/utils/python-compat.hpp"

#include <boost/python.hpp>

#include <memory>
#include <type_traits>

namespace eigenpy {

namespace details {

template <typename T>
typename std::enable_if<!is_python_primitive_type<T>::value, PyObject*>::type
unique_ptr_to_python(std::unique_ptr<T>&& x) {
  typedef bp::objects::pointer_holder<std::unique_ptr<T>, T> holder_t;
  if (!x) {
    return bp::detail::none();
  } else {
    return bp::objects::make_ptr_instance<T, holder_t>::execute(x);
  }
}

template <typename T>
typename std::enable_if<is_python_primitive_type<T>::value, PyObject*>::type
unique_ptr_to_python(std::unique_ptr<T>&& x) {
  if (!x) {
    return bp::detail::none();
  } else {
    return bp::to_python_value<const T&>()(*x);
  }
}

template <typename T>
typename std::enable_if<!is_python_primitive_type<T>::value, PyObject*>::type
internal_unique_ptr_to_python(std::unique_ptr<T>& x) {
  if (!x) {
    return bp::detail::none();
  } else {
    return bp::detail::make_reference_holder::execute(x.get());
  }
}

template <typename T>
typename std::enable_if<is_python_primitive_type<T>::value, PyObject*>::type
internal_unique_ptr_to_python(std::unique_ptr<T>& x) {
  if (!x) {
    return bp::detail::none();
  } else {
    return bp::to_python_value<const T&>()(*x);
  }
}

struct StdUniquePtrResultConverter {
  template <typename T>
  struct apply {
    struct type {
      typedef typename T::element_type element_type;

      PyObject* operator()(T&& x) const {
        return unique_ptr_to_python(std::forward<T>(x));
      }
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
      PyTypeObject const* get_pytype() const {
        return bp::to_python_value<const element_type&>().get_pytype();
      }
#endif
    };
  };
};

struct InternalStdUniquePtrConverter {
  template <typename T>
  struct apply {
    struct type {
      typedef typename remove_cvref<T>::type::element_type element_type;

      PyObject* operator()(T x) const {
        return internal_unique_ptr_to_python(x);
      }
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
      PyTypeObject const* get_pytype() const {
        return bp::to_python_value<const element_type&>().get_pytype();
      }
#endif
    };
  };
};

}  // namespace details

struct StdUniquePtrCallPolicies : bp::default_call_policies {
  typedef details::StdUniquePtrResultConverter result_converter;
};

struct ReturnInternalStdUniquePtr : bp::return_internal_reference<> {
  typedef details::InternalStdUniquePtrConverter result_converter;

  template <class ArgumentPackage>
  static PyObject* postcall(ArgumentPackage const& args_, PyObject* result) {
    // Don't run return_internal_reference postcall on primitive type
    if (PyInt_Check(result) || PyBool_Check(result) || PyFloat_Check(result) ||
        PyStr_Check(result) || PyComplex_Check(result)) {
      return result;
    }
    return bp::return_internal_reference<>::postcall(args_, result);
  }
};

}  // namespace eigenpy

namespace boost {
namespace python {

template <typename T>
struct to_python_value<const std::unique_ptr<T>&>
    : eigenpy::details::StdUniquePtrResultConverter::apply<
          std::unique_ptr<T> >::type {};

}  // namespace python
}  // namespace boost

#endif  // ifndef __eigenpy_utils_std_unique_ptr_hpp__