14 #include <unordered_map>
16 #include <boost/preprocessor/seq/for_each.hpp>
17 #include <boost/preprocessor/variadic/to_seq.hpp>
18 #include <pybind11/pybind11.h>
20 namespace Acts::Python {
23 std::unordered_map<std::string, pybind11::module_>
modules;
27 template <
typename...
Args,
typename = std::enable_if_t<
sizeof...(Args) >= 2>>
33 template <
typename T,
typename Ur,
typename Ut>
39 return std::pair{
self.*
begin,
self.*end};
41 [=](Ut&
self, std::pair<Ur, Ur>
p) {
42 self.*begin =
p.first;
48 pybind11::module::import(
"acts._adapter").attr(
"_patch_config")(
m);
53 pybind11::module::import(
"acts._adapter").attr(
"_patchKwargsConstructor")(
c);
60 #define ACTS_PYTHON_MEMBER(name) \
61 _binding_instance.def_readwrite(#name, &_struct_type::name)
63 #define ACTS_PYTHON_STRUCT_BEGIN(obj, cls) \
65 [[maybe_unused]] auto& _binding_instance = obj; \
66 using _struct_type = cls; \
70 #define ACTS_PYTHON_STRUCT_END() \
76 #define ACTS_PYTHON_MEMBER_LOOP(r, data, elem) ACTS_PYTHON_MEMBER(elem);
80 #define ACTS_PYTHON_DECLARE_ALGORITHM(algorithm, mod, name, ...) \
82 using Alg = algorithm; \
83 using Config = Alg::Config; \
85 py::class_<Alg, ActsExamples::IAlgorithm, std::shared_ptr<Alg>>(mod, \
87 .def(py::init<const Config&, Acts::Logging::Level>(), \
88 py::arg("config"), py::arg("level")) \
89 .def_property_readonly("config", &Alg::config); \
91 auto c = py::class_<Config>(alg, "Config").def(py::init<>()); \
92 ACTS_PYTHON_STRUCT_BEGIN(c, Config); \
93 BOOST_PP_SEQ_FOR_EACH(ACTS_PYTHON_MEMBER_LOOP, _, \
94 BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
95 ACTS_PYTHON_STRUCT_END(); \
99 #define ACTS_PYTHON_DECLARE_WRITER(writer, mod, name, ...) \
101 using Writer = writer; \
102 using Config = Writer::Config; \
104 py::class_<Writer, ActsExamples::IWriter, std::shared_ptr<Writer>>( \
106 .def(py::init<const Config&, Acts::Logging::Level>(), \
107 py::arg("config"), py::arg("level")) \
108 .def_property_readonly("config", &Writer::config); \
110 constexpr bool has_write_method = \
111 Acts::Concepts::has_method<Writer, ActsExamples::ProcessCode, \
112 Acts::Python::write_method_trait_t, \
113 const ActsExamples::AlgorithmContext&>; \
115 if constexpr (has_write_method) { \
116 w.def("write", &Writer::write); \
119 auto c = py::class_<Config>(w, "Config").def(py::init<>()); \
120 ACTS_PYTHON_STRUCT_BEGIN(c, Config); \
121 BOOST_PP_SEQ_FOR_EACH(ACTS_PYTHON_MEMBER_LOOP, _, \
122 BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
123 ACTS_PYTHON_STRUCT_END(); \
127 #define ACTS_PYTHON_DECLARE_READER(reader, mod, name, ...) \
129 using Reader = reader; \
130 using Config = Reader::Config; \
132 py::class_<Reader, ActsExamples::IReader, std::shared_ptr<Reader>>( \
134 .def(py::init<const Config&, Acts::Logging::Level>(), \
135 py::arg("config"), py::arg("level")) \
136 .def_property_readonly("config", &Reader::config); \
138 auto c = py::class_<Config>(r, "Config").def(py::init<>()); \
139 ACTS_PYTHON_STRUCT_BEGIN(c, Config); \
140 BOOST_PP_SEQ_FOR_EACH(ACTS_PYTHON_MEMBER_LOOP, _, \
141 BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
142 ACTS_PYTHON_STRUCT_END(); \