1
1
#pragma once
2
2
3
3
#include " aligator/context.hpp"
4
- #include " aligator/python/macros .hpp"
4
+ #include " aligator/utils/exceptions .hpp"
5
5
6
6
#include < eigenpy/eigenpy.hpp>
7
7
#include < eigenpy/std-vector.hpp>
8
8
9
+ #include < type_traits>
10
+
9
11
#include < proxsuite-nlp/python/polymorphic.hpp>
10
12
11
- namespace aligator {
12
13
// / @brief The Python bindings.
13
- namespace python {
14
+ namespace aligator :: python {
14
15
namespace bp = boost::python;
15
16
using eigenpy::StdVectorPythonVisitor;
16
17
@@ -19,6 +20,16 @@ inline bp::arg operator""_a(const char *argname, std::size_t) {
19
20
return bp::arg (argname);
20
21
}
21
22
23
+ namespace internal {
24
+
25
+ template <typename ret_type>
26
+ ret_type suppress_if_void (bp::detail::method_result &&o) {
27
+ if constexpr (!std::is_void_v<ret_type>)
28
+ return o.operator ret_type ();
29
+ }
30
+
31
+ } // namespace internal
32
+
22
33
// / Expose GAR module
23
34
void exposeGAR ();
24
35
// / Expose stagewise function classes
@@ -53,5 +64,30 @@ void exposeFilter();
53
64
void exposePinocchioFeatures ();
54
65
#endif
55
66
56
- } // namespace python
57
- } // namespace aligator
67
+ } // namespace aligator::python
68
+
69
+ #define ALIGATOR_PYTHON_OVERRIDE_IMPL (ret_type, pyname, ...) \
70
+ do { \
71
+ if (bp::override fo = this ->get_override (pyname)) { \
72
+ decltype (auto ) o = fo (__VA_ARGS__); \
73
+ return ::aligator::python::internal::suppress_if_void<ret_type>( \
74
+ std::move (o)); \
75
+ } \
76
+ } while (false )
77
+
78
+ /* *
79
+ * @def ALIGATOR_PYTHON_OVERRIDE_PURE(ret_type, pyname, ...)
80
+ * @brief Define the body of a virtual function override. This is meant
81
+ * to reduce boilerplate code when exposing virtual member functions.
82
+ */
83
+ #define ALIGATOR_PYTHON_OVERRIDE_PURE (ret_type, pyname, ...) \
84
+ ALIGATOR_PYTHON_OVERRIDE_IMPL (ret_type, pyname, __VA_ARGS__); \
85
+ ALIGATOR_RUNTIME_ERROR (" Tried to call pure virtual function {:s}." , pyname)
86
+
87
+ /* *
88
+ * @def ALIGATOR_PYTHON_OVERRIDE(ret_type, cname, fname, ...)
89
+ * @copybrief ALIGATOR_PYTHON_OVERRIDE_PURE()
90
+ */
91
+ #define ALIGATOR_PYTHON_OVERRIDE (ret_type, cname, fname, ...) \
92
+ ALIGATOR_PYTHON_OVERRIDE_IMPL (ret_type, #fname, __VA_ARGS__); \
93
+ return cname::fname(__VA_ARGS__)
0 commit comments