-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexpose_callbacks.cpp
74 lines (58 loc) · 2.03 KB
/
expose_callbacks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2020-2025 Francesco Biscani ([email protected]), Dario Izzo ([email protected])
//
// This file is part of the heyoka.py library.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <sstream>
#include <vector>
#include <fmt/core.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <heyoka/callbacks.hpp>
#include <heyoka/expression.hpp>
#include "common_utils.hpp"
#include "expose_callbacks.hpp"
#include "pickle_wrappers.hpp"
namespace heyoka_py
{
namespace py = pybind11;
namespace detail
{
namespace
{
// NOTE: possible improvements:
// - expose operator()/pre_hook()? Might be handy in step-by-step
// integrations. If we do it we probably need to expose them
// *after* having exposed the integrators, otherwise we have the usual
// issue of the signatures with the "wrong" names.
template <typename StepCallback>
py::class_<StepCallback> expose_step_callback(py::module_ &m, const char *name)
{
// NOLINTNEXTLINE(google-build-using-namespace)
using namespace pybind11::literals;
py::class_<StepCallback> ret(m, fmt::format("_callback_{}", name).c_str(), py::dynamic_attr{});
ret.def(py::init<>());
// Repr.
ret.def("__repr__", [](const StepCallback &cb) {
std::ostringstream oss;
oss << cb;
return oss.str();
});
// Copy/deepcopy.
ret.def("__copy__", copy_wrapper<StepCallback>);
ret.def("__deepcopy__", deepcopy_wrapper<StepCallback>, "memo"_a);
// Pickle support.
ret.def(py::pickle(&pickle_getstate_wrapper<StepCallback>, &pickle_setstate_wrapper<StepCallback>));
return ret;
}
} // namespace
} // namespace detail
void expose_callbacks(py::module_ &m)
{
// Angle reducer.
auto ar_class = detail::expose_step_callback<heyoka::callback::angle_reducer>(m, "angle_reducer");
ar_class.def(py::init<std::vector<heyoka::expression>>());
}
} // namespace heyoka_py