4 #include <pybind11/pybind11.h>
18 #include <pybind11/chrono.h>
19 #include <pybind11/operators.h>
20 #include <pybind11/pytypes.h>
21 #include <pybind11/stl.h>
23 #include <ChimeraTK/DataConsistencyGroup.h>
24 #include <ChimeraTK/SupportedUserTypes.h>
25 #include <ChimeraTK/VariantUserTypes.h>
27 #include <boost/fusion/algorithm.hpp>
28 #include <boost/fusion/container/map.hpp>
36 using namespace py::literals;
48 if(Application::hasInstance()) {
55 Application::getInstance().getPythonModuleManager().deinit();
66 template<
class UnloadHook>
67 static void registerUnloadHook() {
68 static UnloadHook hook;
77 py::register_exception<ChimeraTK::logic_error>(m,
"LogicError", PyExc_RuntimeError);
82 py::class_<DataType> mDataType(m,
"DataType");
83 mDataType.def(py::init<DataType::TheType>())
84 .def(
"__str__", &DataType::getAsString)
85 .def(py::self == py::self)
86 .def(
"__repr__", [](
const DataType& type) {
return "DataType." + type.getAsString(); });
87 py::enum_<DataType::TheType>(mDataType,
"TheType")
88 .value(
"none", DataType::none)
89 .value(
"int8", DataType::int8)
90 .value(
"uint8", DataType::uint8)
91 .value(
"int16", DataType::int16)
92 .value(
"uint16", DataType::uint16)
93 .value(
"int32", DataType::int32)
94 .value(
"uint32", DataType::uint32)
95 .value(
"int64", DataType::int64)
96 .value(
"uint64", DataType::uint64)
97 .value(
"float32", DataType::float32)
98 .value(
"float64", DataType::float64)
99 .value(
"string", DataType::string)
100 .value(
"Boolean", DataType::Boolean)
101 .value(
"Void", DataType::Void)
103 py::implicitly_convertible<DataType::TheType, DataType>();
104 py::implicitly_convertible<DataType, DataType::TheType>();
109 py::enum_<DataValidity>(m,
"DataValidity")
110 .value(
"ok", DataValidity::ok)
111 .value(
"faulty", DataValidity::faulty)
117 py::class_<VersionNumber>(m,
"VersionNumber")
119 .def(py::init<std::chrono::system_clock::time_point>())
120 .def(py::init<std::nullptr_t>(), py::arg(
"version").none(
true))
122 .def(py::self == py::self)
123 .def(py::self != py::self)
124 .def(py::self < py::self)
125 .def(py::self <= py::self)
126 .def(py::self > py::self)
127 .def(py::self >= py::self)
129 .def(
"getTime", &VersionNumber::getTime, R
"(Return the time stamp associated with this version number.)")
130 .def("__str__", [](VersionNumber& v) {
return std::string(v); })
131 .def(
"__repr__", [](VersionNumber& v) {
return "VersionNumber(" + std::string(v) +
")"; });
136 py::class_<TransferElementID>(m,
"TransferElementID")
139 .def(py::self == py::self)
140 .def(py::self != py::self)
142 .def(
"isValid", &TransferElementID::isValid, R
"(Check whether the ID is valid.)")
144 [](TransferElementID&
id) {
145 std::stringstream ss;
149 .def(
"__repr__", [](TransferElementID&
id) {
150 std::stringstream ss;
152 return "TransferElementID(" + ss.str() +
")";
159 py::class_<PyOwnedObject> pwnedObject(m,
"PyOwnedObject");
160 py::class_<PyOwningObject> pwningObject(m,
"PyOwningObject");
169 PyTransferElementBase::bind(m);
174 PyReadAnyGroup::bind(m);
179 PyDataConsistencyGroup::bind(m);
189 PyScalarAccessor::bind(m);
194 PyArrayAccessor::bind(m);
199 PyVariableGroup::bind(m);
204 PyApplicationModule::bind(m);
209 PyModuleGroup::bind(m);
214 PyConfigReader::bind(m);
221 if(Application::hasInstance()) {
222 Application::getInstance().getPythonModuleManager().setOnMainGroupChange(
223 [m](
const std::unique_ptr<PyModuleGroup>& mainGroup) {
224 m.attr(
"app") = py::cast(mainGroup.get(), py::return_value_policy::reference);
228 registerUnloadHook<PyApplicationCoreUnload>();