GCC Code Coverage Report


Directory: ./
File: include/pinocchio/bindings/python/utils/cast.hpp
Date: 2024-08-27 18:20:05
Exec Total Coverage
Lines: 31 34 91.2%
Branches: 44 90 48.9%

Line Branch Exec Source
1 //
2 // Copyright (c) 2020 INRIA
3 //
4
5 #ifndef __pinocchio_python_utils_cast_hpp__
6 #define __pinocchio_python_utils_cast_hpp__
7
8 #include "pinocchio/bindings/python/fwd.hpp"
9
10 #include <eigenpy/registration.hpp>
11
12 namespace pinocchio
13 {
14 namespace python
15 {
16
17 namespace bp = boost::python;
18
19 ///
20 /// \brief Add the Python method cast
21 ///
22 template<class C, typename NewScalar = PINOCCHIO_PYTHON_SCALAR_TYPE_DEFAULT>
23 struct CastVisitor : public bp::def_visitor<CastVisitor<C, NewScalar>>
24 {
25 template<class PyClass>
26 220 void visit(PyClass & cl) const
27 {
28 220 cl.def("cast", &C::template cast<NewScalar>, "Returns a cast of *this.");
29 220 }
30 };
31
32 template<class _From, class _To>
33 struct ExposeConstructorByCastVisitor
34 : public bp::def_visitor<ExposeConstructorByCastVisitor<_From, _To>>
35 {
36 template<class PyClass>
37 220 void visit(PyClass & /*cl*/) const
38 {
39 220 expose_constructor<_From, _To>();
40 220 expose_constructor<_To, _From>();
41 220 }
42
43 protected:
44 template<typename From, typename To>
45 440 static void expose_constructor()
46 {
47
3/6
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 360 times.
✗ Branch 7 not taken.
440 if (eigenpy::check_registration<From>() && eigenpy::check_registration<To>())
48 {
49 440 const bp::type_info to_info = bp::type_id<To>();
50
1/2
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
440 const bp::converter::registration * to_reg = bp::converter::registry::query(to_info);
51
3/6
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 360 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 360 times.
✗ Branch 9 not taken.
440 bp::object to_class_obj(bp::handle<>(bp::borrowed(to_reg->get_class_object())));
52
2/4
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
440 const std::string to_module_name =
53
3/6
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 360 times.
✗ Branch 8 not taken.
880 bp::extract<std::string>(to_class_obj.attr("__module__"));
54
5/10
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 360 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 360 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 360 times.
✗ Branch 14 not taken.
880 const std::string to_class_name = bp::extract<std::string>(to_class_obj.attr("__name__"));
55
56 440 const bp::type_info from_info = bp::type_id<From>();
57
1/2
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
440 const bp::converter::registration * from_reg = bp::converter::registry::query(from_info);
58
3/6
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 360 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 360 times.
✗ Branch 9 not taken.
440 bp::object from_class_obj(bp::handle<>(bp::borrowed(from_reg->get_class_object())));
59
2/4
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
440 const std::string from_module_name =
60
3/6
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 360 times.
✗ Branch 8 not taken.
880 bp::extract<std::string>(from_class_obj.attr("__module__"));
61
2/4
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
440 const std::string from_class_name =
62
3/6
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 360 times.
✗ Branch 8 not taken.
880 bp::extract<std::string>(from_class_obj.attr("__name__"));
63
64
2/4
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
440 const std::string to_full_class_name = to_module_name + "." + to_class_name;
65
2/4
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
440 const std::string from_full_class_name = from_module_name + "." + from_class_name;
66
1/2
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
440 std::stringstream to_doc;
67
2/4
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
440 to_doc << "Copy constructor from " << from_full_class_name;
68
2/4
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
440 to_doc << " -> " << to_full_class_name;
69
3/6
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 360 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 360 times.
✗ Branch 9 not taken.
880 bp::objects::add_to_namespace(
70 to_class_obj, "__init__",
71 bp::make_constructor(
72 &ExposeConstructorByCastVisitor::constructor<From, To>, bp::default_call_policies(),
73
1/2
✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
880 bp::arg("clone")),
74 to_doc.str().c_str());
75 440 }
76 440 }
77
78 template<typename From, typename To>
79 static To * constructor(const From & clone)
80 {
81 typedef typename To::Scalar NewScalar;
82 return new To(clone.template cast<NewScalar>());
83 }
84 };
85 } // namespace python
86 } // namespace pinocchio
87
88 #endif // ifndef __pinocchio_python_utils_cast_hpp__
89