GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/utils/cast.hpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 3 13 23.1%
Branches: 1 2 50.0%

Line Branch Exec Source
1
2
3 ///////////////////////////////////////////////////////////////////////////////
4 // BSD 3-Clause License
5 //
6 // Copyright (C) 2024-2025, Heriot-Watt University
7 // Copyright note valid unless otherwise stated in individual files.
8 // All rights reserved.
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BINDINGS_PYTHON_CROCODDYL_UTILS_CAST_HPP_
12 #define BINDINGS_PYTHON_CROCODDYL_UTILS_CAST_HPP_
13
14 #include <boost/python.hpp>
15
16 #include "crocoddyl/core/utils/conversions.hpp"
17 #include "python/crocoddyl/utils/scalar.hpp"
18
19 namespace crocoddyl {
20 namespace python {
21
22 namespace bp = boost::python;
23
24 /**
25 * @brief Add the Python method cast to allow casting of this by predefined cast
26 * types.
27 */
28 template <typename Model>
29 struct CastVisitor : public bp::def_visitor<CastVisitor<Model>> {
30 template <class PyClass>
31 2680 void visit(PyClass& cl) const {
32
1/2
✓ Branch 2 taken 1340 times.
✗ Branch 3 not taken.
2680 cl.def("cast", &cast_instance, bp::arg("dtype"),
33 "Returns a copy of *this.");
34 2680 }
35
36 private:
37 static bp::object cast_instance(const Model& self, DType dtype) {
38 switch (dtype) {
39 case DType::Float64:
40 return bp::object(self.template cast<Float64>());
41 case DType::Float32:
42 return bp::object(self.template cast<Float32>());
43 #ifdef CROCODDYL_WITH_CODEGEN_DISABLE
44 case DType::ADFloat64:
45 return bp::object(self.template cast<ADFloat64>());
46 #endif
47 default:
48 PyErr_SetString(PyExc_TypeError, "Unsupported dtype.");
49 bp::throw_error_already_set();
50 return bp::object();
51 }
52 }
53 };
54
55 } // namespace python
56 } // namespace crocoddyl
57
58 #endif // BINDINGS_PYTHON_CROCODDYL_UTILS_CAST_HPP_
59