pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
keep-alive.hpp
1//
2// Copyright (c) 2024 INRIA
3//
4
5#pragma once
6
7#include <eigenpy/eigenpy.hpp>
8
9namespace pinocchio
10{
11 namespace python
12 {
13 template<
14 typename ReturnType,
15 std::size_t custodian,
16 std::size_t ward,
17 class BasePolicy_ = boost::python::default_call_policies>
18 struct keep_alive;
19
20 template<std::size_t custodian, std::size_t ward, class BasePolicy_>
21 struct keep_alive<boost::python::tuple, custodian, ward, BasePolicy_> : BasePolicy_
22 {
23 BOOST_STATIC_ASSERT(custodian != ward);
24
25 template<class ArgumentPackage>
26 static PyObject * postcall(ArgumentPackage const & args_, PyObject * result)
27 {
28 std::size_t arity_ = boost::python::detail::arity(args_);
29 // check if either custodian or ward exceeds the arity
30 // (this weird formulation avoids "always false" warnings
31 // for arity_ = 0)
32 if ((std::max)(custodian, ward) > arity_)
33 {
34 PyErr_SetString(PyExc_IndexError, "keep_alive_with_tuple: argument index out of range");
35 return 0;
36 }
37
38 if (!PyTuple_Check(result))
39 {
40 PyErr_SetString(PyExc_RuntimeError, "keep_alive_with_tuple: result is not a tuple");
41 return 0;
42 }
43
44 int tuple_size = PyTuple_Size(result);
45 PINOCCHIO_THROW_PRETTY_IF(
46 custodian >= std::size_t(tuple_size), std::runtime_error,
47 "custodian is greater than tuple_size.");
48
49 // keep_alive<Nurse, Patient> indicates that the argument with index Patient should be kept
50 // alive at least until the argument with index Nurse is freed by the garbage collector.
51 PyObject * patient = boost::python::detail::get_prev<ward>::execute(args_, result);
52 PyObject * nurse = PyTuple_GetItem(result, custodian);
53
54 if (nurse == 0)
55 return 0;
56
57 nurse = BasePolicy_::postcall(args_, nurse);
58 if (nurse == 0)
59 return 0;
60
61 if (boost::python::objects::make_nurse_and_patient(nurse, patient) == 0)
62 {
63 Py_XDECREF(nurse);
64 return 0;
65 }
66 return result;
67 }
68 };
69
70 } // namespace python
71} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11