5 #ifndef __pinocchio_python_utils_cast_hpp__
6 #define __pinocchio_python_utils_cast_hpp__
22 template<
class C,
typename NewScalar = PINOCCHIO_PYTHON_SCALAR_TYPE_DEFAULT>
23 struct CastVisitor :
public bp::def_visitor<CastVisitor<C, NewScalar>>
25 template<
class PyClass>
28 cl.def(
"cast", &C::template cast<NewScalar>,
"Returns a cast of *this.");
32 template<
class _From,
class _To>
34 :
public bp::def_visitor<ExposeConstructorByCastVisitor<_From, _To>>
36 template<
class PyClass>
39 expose_constructor<_From, _To>();
40 expose_constructor<_To, _From>();
44 template<
typename From,
typename To>
47 if (eigenpy::check_registration<From>() && eigenpy::check_registration<To>())
49 const bp::type_info to_info = bp::type_id<To>();
50 const bp::converter::registration * to_reg = bp::converter::registry::query(to_info);
51 bp::object to_class_obj(bp::handle<>(bp::borrowed(to_reg->get_class_object())));
52 const std::string to_module_name =
53 bp::extract<std::string>(to_class_obj.attr(
"__module__"));
54 const std::string to_class_name = bp::extract<std::string>(to_class_obj.attr(
"__name__"));
56 const bp::type_info from_info = bp::type_id<From>();
57 const bp::converter::registration * from_reg = bp::converter::registry::query(from_info);
58 bp::object from_class_obj(bp::handle<>(bp::borrowed(from_reg->get_class_object())));
59 const std::string from_module_name =
60 bp::extract<std::string>(from_class_obj.attr(
"__module__"));
61 const std::string from_class_name =
62 bp::extract<std::string>(from_class_obj.attr(
"__name__"));
64 const std::string to_full_class_name = to_module_name +
"." + to_class_name;
65 const std::string from_full_class_name = from_module_name +
"." + from_class_name;
66 std::stringstream to_doc;
67 to_doc <<
"Copy constructor from " << from_full_class_name;
68 to_doc <<
" -> " << to_full_class_name;
69 bp::objects::add_to_namespace(
70 to_class_obj,
"__init__",
72 &ExposeConstructorByCastVisitor::constructor<From, To>, bp::default_call_policies(),
74 to_doc.str().c_str());
78 template<
typename From,
typename To>
82 return new To(
clone.template cast<NewScalar>());
88 #endif // ifndef __pinocchio_python_utils_cast_hpp__