From e5cb5c7d4b3b8e2e4e7e2d3aea5a8e010599b3ab Mon Sep 17 00:00:00 2001 From: RobKleinIkink Date: Wed, 15 May 2024 14:34:29 +0200 Subject: [PATCH] Wrappers: Use wrapper factory to construct Python wrapper - Modify Singleton to accept constructor arguments for the object it manages - Use Zivid::Detail::createApplicationForWrapper to construct the Application for the Python wrapper Part of ZIVID-8484 --- src/SingletonApplication.cpp | 9 ++++++++- src/include/ZividPython/Releasable.h | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/SingletonApplication.cpp b/src/SingletonApplication.cpp index 3a22febe..a96aba28 100644 --- a/src/SingletonApplication.cpp +++ b/src/SingletonApplication.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -10,7 +11,13 @@ namespace ZividPython { void wrapClass(pybind11::class_ pyClass) { - pyClass.def(py::init()) + pyClass + .def(py::init([] { + // This method constructs a Zivid::Application and identifies the wrapper as the Zivid Python wrapper. + // For users of the SDK: please do not use this method and construct the Zivid::Application directly instead. + return SingletonApplication{ Zivid::Detail::createApplicationForWrapper( + Zivid::Detail::EnvironmentInfo::Wrapper::python) }; + })) .def("cameras", &SingletonApplication::cameras) .def("connect_camera", [](SingletonApplication &application) { return application.connectCamera(); }) .def( diff --git a/src/include/ZividPython/Releasable.h b/src/include/ZividPython/Releasable.h index 2eabdb2d..a4da9cec 100644 --- a/src/include/ZividPython/Releasable.h +++ b/src/include/ZividPython/Releasable.h @@ -129,12 +129,13 @@ namespace ZividPython class Singleton { public: - Singleton() + template + Singleton(Args &&...args) { // Keep the singleton alive forever to avoid races with // static variables that the singleton may need during destruction // This should be fixed a more elegant way! - if(!globalImpl) globalImpl = std::make_shared(); + if(!globalImpl) globalImpl = std::make_shared(std::forward(args)...); } decltype(auto) toString() const