Program Listing for File registration_class.hpp

Return to documentation for file (include/eigenpy/registration_class.hpp)

/*
 * Copyright 2023, INRIA
 */

#ifndef __eigenpy_registration_class_hpp__
#define __eigenpy_registration_class_hpp__

#include <boost/python/class.hpp>

#include "eigenpy/fwd.hpp"

namespace eigenpy {

template <class W>
class registration_class {
 public:
  using self = registration_class;

  registration_class(bp::object object) : m_object(object) {}

  template <class Visitor>
  self& def(Visitor const& visitor) {
    visitor.visit(*this);
    return *this;
  }

  template <class DerivedVisitor>
  self& def(bp::def_visitor<DerivedVisitor> const& visitor) {
    static_cast<DerivedVisitor const&>(visitor).visit(*this);
    return *this;
  }

  template <class F>
  self& def(char const* name, F f) {
    def_impl(bp::detail::unwrap_wrapper((W*)0), name, f,
             bp::detail::def_helper<char const*>(0), &f);
    return *this;
  }

  template <class A1, class A2>
  self& def(char const* name, A1 a1, A2 const& a2) {
    def_maybe_overloads(name, a1, a2, &a2);
    return *this;
  }

  template <class Fn, class A1, class A2>
  self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2) {
    def_impl(bp::detail::unwrap_wrapper((W*)0), name, fn,
             bp::detail::def_helper<A1, A2>(a1, a2), &fn);

    return *this;
  }

  template <class Fn, class A1, class A2, class A3>
  self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2, A3 const& a3) {
    def_impl(bp::detail::unwrap_wrapper((W*)0), name, fn,
             bp::detail::def_helper<A1, A2, A3>(a1, a2, a3), &fn);

    return *this;
  }

 private:
  template <class T, class Fn, class Helper>
  inline void def_impl(T*, char const* name, Fn fn, Helper const& helper, ...) {
    bp::objects::add_to_namespace(
        m_object, name,
        make_function(fn, helper.policies(), helper.keywords(),
                      bp::detail::get_signature(fn, (T*)0)),
        helper.doc());

    def_default(name, fn, helper,
                boost::mpl::bool_<Helper::has_default_implementation>());
  }

  template <class Fn, class Helper>
  inline void def_default(char const* name, Fn, Helper const& helper,
                          boost::mpl::bool_<true>) {
    bp::detail::error::virtual_function_default<
        W, Fn>::must_be_derived_class_member(helper.default_implementation());

    bp::objects::add_to_namespace(
        m_object, name,
        make_function(helper.default_implementation(), helper.policies(),
                      helper.keywords()));
  }

  template <class Fn, class Helper>
  inline void def_default(char const*, Fn, Helper const&,
                          boost::mpl::bool_<false>) {}

  template <class OverloadsT, class SigT>
  void def_maybe_overloads(char const* name, SigT sig,
                           OverloadsT const& overloads,
                           bp::detail::overloads_base const*)

  {
    bp::detail::define_with_defaults(name, overloads, *this,
                                     bp::detail::get_signature(sig));
  }

  template <class Fn, class A1>
  void def_maybe_overloads(char const* name, Fn fn, A1 const& a1, ...) {
    def_impl(bp::detail::unwrap_wrapper((W*)0), name, fn,
             bp::detail::def_helper<A1>(a1), &fn);
  }

 private:
  bp::object m_object;
};

}  // namespace eigenpy

#endif  // ifndef __eigenpy_registration_class_hpp__