JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
PythonWrapper.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2022 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#pragma once
19
20#include <boost/python.hpp>
21#include <mutex>
22
23namespace jevois
24{
25 //! Helper class to run python code from C++
26 /*! This class loads python code and makes it accessible through pyinst(). It is designed to be composed with other
27 classes that will actually execute some python code via pyinst().
28
29 The main convention here is that the python code will define a class with the exact same name as the python
30 filename (without trailing .py extension). This is the class that will then become accessible via pyinst().
31
32 Note that constructors and pythonload() will not throw, but pyinst() will if an error occurred at construction.
33
34 Often this class is inherited from by a class that also inherits from Component, to create a Component derivative
35 that can run python code. In such case, PythonWrapper will register that sibling association with Engine when the
36 python code is loaded. It is critical that you inherit from Component (or a derivative) before you inherit from
37 PythonWrapper, so that by the time the PythonWrapper destructor is called the Component base should not yet have
38 been destroyed, and it will be unregistered from Engine.
39
40 \ingroup python */
42 {
43 public:
44 //! Default constructor. Will need to call pythonload() later
46
47 //! Destructor
48 virtual ~PythonWrapper();
49
50 //! Construct from path
51 PythonWrapper(std::string const & path);
52
53 //! Init from path if default constructor was used
54 void pythonload(std::string const & path);
55
56 //! Get the python class pyinst, or throw if construction error occurred (e.g., file not found)
57 boost::python::object & pyinst();
58
59 //! Get the main module
60 boost::python::object & mainModule();
61
62 //! Get the main namespace
63 boost::python::object & mainNamespace();
64
65 //! Get the construction error if any, or empty string
66 std::string const & constructionError() const;
67
68 private:
69 boost::python::object itsMainModule, itsMainNamespace, itsInstance;
70 std::string itsConstructionError;
71 mutable std::mutex itsMtx; // make sure we don't get destroyed while loading python code
72 };
73
74} // namespace jevois
Helper class to run python code from C++.
boost::python::object & mainModule()
Get the main module.
PythonWrapper()
Default constructor. Will need to call pythonload() later.
void pythonload(std::string const &path)
Init from path if default constructor was used.
boost::python::object & pyinst()
Get the python class pyinst, or throw if construction error occurred (e.g., file not found)
std::string const & constructionError() const
Get the construction error if any, or empty string.
virtual ~PythonWrapper()
Destructor.
boost::python::object & mainNamespace()
Get the main namespace.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2