File indexing completed on 2026-08-26 08:38:45
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Utilities/Any.hpp"
0012 #include "Acts/Utilities/HashedString.hpp"
0013
0014 #include <concepts>
0015 #include <functional>
0016 #include <unordered_map>
0017
0018 #include <pybind11/pybind11.h>
0019
0020 namespace ActsPython {
0021
0022
0023 template <typename... Ts>
0024 concept PyClassWithSmartHolder =
0025 std::same_as<typename pybind11::class_<Ts...>::holder_type,
0026 pybind11::smart_holder>;
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 class WhiteBoardRegistry {
0044 private:
0045
0046
0047 using ToPythonFunction =
0048 std::function<PyObject*(const Acts::AnyMoveOnly& any, PyObject* wbPy)>;
0049
0050
0051
0052 using FromPythonFunction =
0053 std::function<std::unique_ptr<Acts::AnyMoveOnly>(PyObject* obj)>;
0054
0055 template <typename T>
0056 static void registerType() {
0057 namespace py = pybind11;
0058 return registerType<T>(py::type::of<T>());
0059 }
0060
0061 template <typename T>
0062 static void registerType(const pybind11::object& pyType) {
0063 namespace py = pybind11;
0064
0065 using type = T;
0066
0067 instance()[pyType.ptr()] = {
0068 .toPython = [](const Acts::AnyMoveOnly& any,
0069 PyObject* wbPy) -> PyObject* {
0070 py::object pyWb = py::reinterpret_borrow<py::object>(wbPy);
0071 py::object out =
0072 py::cast(any.as<type>(),
0073 py::return_value_policy::reference_internal, pyWb);
0074 return out.release().ptr();
0075 },
0076 .fromPython =
0077 [](PyObject* obj) {
0078 py::object pyObj = py::reinterpret_borrow<py::object>(obj);
0079
0080
0081 auto up = py::cast<std::unique_ptr<T>>(pyObj);
0082 return std::make_unique<Acts::AnyMoveOnly>(std::move(*up));
0083 },
0084 .typeinfo = &typeid(type),
0085 .typeHash = Acts::typeHash<type>(),
0086 };
0087 }
0088
0089 public:
0090
0091
0092
0093
0094 template <typename... Ts>
0095 static void registerClass(const pybind11::class_<Ts...>& pyClass)
0096 requires PyClassWithSmartHolder<Ts...>
0097 {
0098 using type = pybind11::class_<Ts...>::type;
0099 registerType<type>(pyClass);
0100 }
0101
0102
0103 struct RegistryEntry {
0104
0105 ToPythonFunction toPython{nullptr};
0106
0107 FromPythonFunction fromPython{nullptr};
0108
0109 const std::type_info* typeinfo{nullptr};
0110
0111 std::uint64_t typeHash{0};
0112 };
0113
0114
0115
0116
0117 static RegistryEntry* find(const pybind11::object& pyType) {
0118 if (auto it = instance().find(pyType.ptr()); it != instance().end()) {
0119 return &it->second;
0120 }
0121 return nullptr;
0122 }
0123
0124 private:
0125 WhiteBoardRegistry() = default;
0126
0127 static std::unordered_map<PyObject*, RegistryEntry>& instance();
0128 };
0129
0130 }