JeVois  1.23
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
PythonParameter.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 20122by 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
21#include <boost/python.hpp>
22
23namespace jevois
24{
25 namespace python
26 {
27 //! Base helper class to allow creation of Parameter in python \ingroup python
29 {
30 public:
31 //! Constructor
33
34 //! Remove param from component
35 virtual ~PyParHelperBase();
36
37 //! Get the value, typed, then wrapped into python object
38 virtual boost::python::object get() = 0;
39
40 //! Set the value from python object that should contain a value of the correct type
41 virtual void set(boost::python::object const & val) = 0;
42
43 //! Set the parameter's callback
44 virtual void setCallback(boost::python::object const & cb) = 0;
45
46 //! Set valid values that the parameter may take from a list
47 virtual void setValidValues(boost::python::list const & vv) = 0;
48
49 //! Access the associated C++ base Parameter
50 virtual std::shared_ptr<jevois::ParameterBase> par() const = 0;
51
52 protected:
53 //! Access the associated C++ Component
54 jevois::Component * comp() const;
55
56 private:
57 jevois::Component * itsComp;
58 };
59
60 //! Typed class to allow creation of Parameter in Python \ingroup python
61 template <typename T>
63 {
64 public:
65 //! Create and add the parameter to the component
66 static std::shared_ptr<jevois::python::PyParHelperBase>
67 create(jevois::Component * comp, std::string const & name, std::string const & description,
68 boost::python::object const & defaultValue, jevois::ParameterCategory const & category);
69
70 //! Create and add the parameter to the component
71 PyParHelper(jevois::Component * comp, std::string const & name, std::string const & description,
72 boost::python::object const & defaultValue, jevois::ParameterCategory const & category);
73
74 //! Remove param from component
75 virtual ~PyParHelper();
76
77 //! Get the value, typed, then wrapped into python object
78 boost::python::object get() override;
79
80 //! Set the value from python object
81 void set(boost::python::object const & val) override;
82
83 //! Set the parameter's callback
84 void setCallback(boost::python::object const & cb) override;
85
86 //! Set valid values that the parameter may take from a list
87 void setValidValues(boost::python::list const & vv) override;
88
89 //! Access the associated C++ base parameter, used to forward strget(), strset(), freeze(), etc to it
90 std::shared_ptr<jevois::ParameterBase> par() const override;
91
92 protected:
93 std::shared_ptr<jevois::DynamicParameter<T>> itsParam;
94 boost::python::object itsPyCallback;
95 };
96 } // namespace python
97
98 //! Wrapper for jevois Parameter in python
99 /*! This wrapper allows python modules to create JeVois Parameter objects, allowing users to interact with the
100 parameters through the console, JeVois-Inventor, or the JeVois-Pro GUI. This class does not actually contain the
101 parameter, it is just an interface. Parameters belong to the Component associated with the python code via
102 PythonWrapper. \ingroup python */
104 {
105 public:
106 //! Constructor. Adds a dynamic parameter to the Component associated with pyinst
107 PythonParameter(boost::python::object & pyinst, std::string const & name, std::string const & typ,
108 std::string const & description, boost::python::object const & defaultValue,
109 jevois::ParameterCategory const & category);
110
111 //! Destructor. Removes the dynamic parameter from the associated Component
113
114 //! Get the parameter name
115 std::string const & name() const;
116
117 //! Get the parameter fully-qualified name, aka descriptor, including names of owning Component and all parents
118 std::string descriptor() const;
119
120 //! Get the value of this Parameter
121 boost::python::object get() const;
122
123 //! Set the value of this Parameter
124 /*! Will throw if the new value is not accepted, in which case the old value will remain in the Parameter. */
125 void set(boost::python::object const & newVal);
126
127 //! Get the value as a string
128 std::string const strget() const;
129
130 //! Set the value from a string representation of it
131 /*! @throws std::range_error if the given string cannot be converted to a Parameter value, or the value is invalid
132 according to our valid values spec or rejected by the Parameter's callback (if any). */
133 void strset(std::string const & valstring);
134
135 //! Freeze/unfreeze this parameter, it becomes read-only and will not show up in the help message
136 void freeze(bool doit);
137
138 //! Returns whether parameter is frozen
139 bool frozen() const;
140
141 //! Hide or un-hide a parameter; hidden params will not show up in GUI or help message, but still work normally
142 void hide(bool doit);
143
144 //! Returns whether parameter is hidden
145 bool hidden() const;
146
147 //! Reset this parameter to its default value
148 void reset();
149
150 //! Set the parameter's callback
151 /*! The callback function is called each time one tries to change the value of the parameter. It will also be
152 called one first time, with the current parameter value, as soon as the callback is set.
153
154 The callback should examine the candidate value newval and (1) if it does not like it, throw and with a
155 descriptive message of why the value is rejected, (2) otherwise, it is assumed that the value is accepted and
156 the callback can then allocate resources or do other work with that value (the actual modification of the
157 Parameter object is handled upstream and the callback does not need to worry about it: if it returns without
158 throwing, the proposed value will become the new value of the Parameter). The Parameter is locked-up for
159 writing as long as the callback is running, to avoid destruction of the parameter and/or concurrent parameter
160 value changes by several different threads. Thus, callbacks should try to execute quickly, and should not call
161 set(), etc on the parameter as this will always deadlock (get() is allowed if your callback needs to know the
162 current value of the parameter). */
163 void setCallback(boost::python::object const & cb);
164
165 //! Set valid values that the parameter may take from a list
166 void setValidValues(boost::python::list const & vv);
167
168 private:
169 std::shared_ptr<jevois::python::PyParHelperBase> itsPyPar;
170 };
171
172
173
174} // namespace jevois
175
176// Include implementation details
177#include <jevois/Core/details/PythonParameterImpl.H>
A component of a model hierarchy.
Definition Component.H:182
Wrapper for jevois Parameter in python.
std::string const & name() const
Get the parameter name.
void set(boost::python::object const &newVal)
Set the value of this Parameter.
std::string const strget() const
Get the value as a string.
void strset(std::string const &valstring)
Set the value from a string representation of it.
void hide(bool doit)
Hide or un-hide a parameter; hidden params will not show up in GUI or help message,...
void reset()
Reset this parameter to its default value.
bool frozen() const
Returns whether parameter is frozen.
boost::python::object get() const
Get the value of this Parameter.
void freeze(bool doit)
Freeze/unfreeze this parameter, it becomes read-only and will not show up in the help message.
void setCallback(boost::python::object const &cb)
Set the parameter's callback.
void setValidValues(boost::python::list const &vv)
Set valid values that the parameter may take from a list.
bool hidden() const
Returns whether parameter is hidden.
~PythonParameter()
Destructor. Removes the dynamic parameter from the associated Component.
std::string descriptor() const
Get the parameter fully-qualified name, aka descriptor, including names of owning Component and all p...
Base helper class to allow creation of Parameter in python.
jevois::Component * comp() const
Access the associated C++ Component.
virtual void setValidValues(boost::python::list const &vv)=0
Set valid values that the parameter may take from a list.
virtual void setCallback(boost::python::object const &cb)=0
Set the parameter's callback.
virtual std::shared_ptr< jevois::ParameterBase > par() const =0
Access the associated C++ base Parameter.
virtual ~PyParHelperBase()
Remove param from component.
virtual void set(boost::python::object const &val)=0
Set the value from python object that should contain a value of the correct type.
virtual boost::python::object get()=0
Get the value, typed, then wrapped into python object.
Typed class to allow creation of Parameter in Python.
void setValidValues(boost::python::list const &vv) override
Set valid values that the parameter may take from a list.
virtual ~PyParHelper()
Remove param from component.
std::shared_ptr< jevois::DynamicParameter< T > > itsParam
boost::python::object get() override
Get the value, typed, then wrapped into python object.
void setCallback(boost::python::object const &cb) override
Set the parameter's callback.
void set(boost::python::object const &val) override
Set the value from python object.
PyParHelper(jevois::Component *comp, std::string const &name, std::string const &description, boost::python::object const &defaultValue, jevois::ParameterCategory const &category)
Create and add the parameter to the component.
static std::shared_ptr< jevois::python::PyParHelperBase > create(jevois::Component *comp, std::string const &name, std::string const &description, boost::python::object const &defaultValue, jevois::ParameterCategory const &category)
Create and add the parameter to the component.
boost::python::object itsPyCallback
std::shared_ptr< jevois::ParameterBase > par() const override
Access the associated C++ base parameter, used to forward strget(), strset(), freeze(),...
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2
A category to which multiple ParameterDef definitions can belong.