JeVois  1.18
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
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 
23 namespace 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
45  PythonWrapper();
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
jevois::PythonWrapper::PythonWrapper
PythonWrapper()
Default constructor. Will need to call pythonload() later.
Definition: PythonWrapper.C:23
jevois::PythonWrapper::pyinst
boost::python::object & pyinst()
Get the python class pyinst, or throw if construction error occurred (e.g., file not found)
Definition: PythonWrapper.C:85
jevois::PythonWrapper::mainModule
boost::python::object & mainModule()
Get the main module.
Definition: PythonWrapper.C:92
jevois::PythonWrapper::mainNamespace
boost::python::object & mainNamespace()
Get the main namespace.
Definition: PythonWrapper.C:96
jevois::PythonWrapper::~PythonWrapper
virtual ~PythonWrapper()
Destructor.
Definition: PythonWrapper.C:104
jevois
Definition: Concepts.dox:1
jevois::PythonWrapper::pythonload
void pythonload(std::string const &path)
Init from path if default constructor was used.
Definition: PythonWrapper.C:34
jevois::PythonWrapper
Helper class to run python code from C++.
Definition: PythonWrapper.H:41
jevois::PythonWrapper::constructionError
const std::string & constructionError() const
Get the construction error if any, or empty string.
Definition: PythonWrapper.C:100