Skip to content

Commit

Permalink
Wrappers: Use wrapper factory to construct Python wrapper
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
RobKleinIkink authored and johningve committed Jun 27, 2024
1 parent 3e83127 commit e5cb5c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/SingletonApplication.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Zivid/CameraInfo.h>
#include <Zivid/Detail/ToolchainDetector.h>

#include <ZividPython/SingletonApplication.h>

Expand All @@ -10,7 +11,13 @@ namespace ZividPython
{
void wrapClass(pybind11::class_<SingletonApplication> 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(
Expand Down
5 changes: 3 additions & 2 deletions src/include/ZividPython/Releasable.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ namespace ZividPython
class Singleton
{
public:
Singleton()
template<typename... Args>
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<T>();
if(!globalImpl) globalImpl = std::make_shared<T>(std::forward<Args>(args)...);
}

decltype(auto) toString() const
Expand Down

0 comments on commit e5cb5c7

Please sign in to comment.