![]() |
Dune-Fufem 2.11-git
|
Wrapper class for callable python objects. More...
#include <dune/fufem/python/callable.hh>

Public Member Functions | |
| Callable () | |
| Construct empty Callable. | |
| Callable (PyObject *p) | |
| Construct Callable from PyObject*. | |
| Callable (const Reference &other) | |
| Construct Callable from Reference. | |
| virtual | ~Callable () |
| Destructor. | |
| virtual Callable & | operator= (const Reference &other) |
| Assignment. | |
| Reference | callWithArgumentTuple (const Reference &args) const |
| Call this Reference with arguments given as tuple. | |
| Reference | callWithArgumentTupleAndKeywordArgs (const Reference &args, const Reference &keywordArgs) const |
| Call this Reference with positional arguments given as tuple and keyword arguments given as dictionary. | |
| template<class... Args> | |
| Reference | operator() (const Args &... args) const |
| Call this object with given arguments. | |
| operator PyObject * () const | |
| Access to stored reference. | |
| template<class T > | |
| void | toC (T &t) const |
| Convert to C object. | |
| template<class T > | |
| std::enable_if< Conversion< T >::useDefaultConstructorConversion, T >::type | toC () const |
| Convert to C object. | |
| template<class T > | |
| std::enable_if< Conversion< T >::useCustomConstructorConversion, T >::type | toC () const |
| Convert to C object. | |
| bool | hasAttr (const std::string &name) const |
| Check if object has attribute of given name. | |
| Reference | get (const std::string &name) const |
| Query attribute of given name. | |
| template<class V > | |
| void | set (const std::string &name, const V &value) |
| Set attribute of given name. | |
| std::string | str () const |
| String representation of this object. | |
Protected Member Functions | |
| void | assertPyObject (const std::string &origin) const |
| Assert that internal PyObject* is not NULL and raise exception otherwise. | |
Static Protected Member Functions | |
| static void | assertCallable (PyObject *p, const std::string &origin) |
| Assert that PyObject* is not NULL and callable and raise exception otherwise. | |
Protected Attributes | |
| PyObject * | p_ |
Detailed Description
Wrapper class for callable python objects.
This class derives from Python::Reference and encapsulates functionality of callable objects.
Constructor & Destructor Documentation
◆ Callable() [1/3]
|
inline |
Construct empty Callable.
◆ Callable() [2/3]
|
inline |
Construct Callable from PyObject*.
Only to be used if you want to extend the interface using the python api.
This forwards to the corresponding constructor of Reference and then checks if the python object is callable. If this is not the case an exception is thrown. As a consequence the reference count of the PyObject will be correctly decreased by ~Reference even if the exception is thrown.
But be careful to always increment the count of a borrowed reference BEFORE calling this constructor. I.e. always use
Callable(Imp::inc(p))
instead of
Imp::inc(Callable(p))
The latter may throw an exception and then decrease the count before it is increased.
◆ Callable() [3/3]
|
inline |
◆ ~Callable()
|
inlinevirtual |
Destructor.
Member Function Documentation
◆ assertCallable()
|
inlinestaticprotected |
Assert that PyObject* is not NULL and callable and raise exception otherwise.
- Parameters
-
p The PyObject* pointer that should be checked origin A string describing the origin of the error
◆ assertPyObject()
|
inlineprotectedinherited |
Assert that internal PyObject* is not NULL and raise exception otherwise.
- Parameters
-
origin A string describing the origin of the error
◆ callWithArgumentTuple()
Call this Reference with arguments given as tuple.
If the Reference represents a function it's called. If the Reference represents an instance its call method is invoked. If the Reference represents a class a constructor is invoked. In any case the arguments are given as a tuple as obtained by the global method Python::makeTuple().
Although the method is const it might change the referred object! This cannot be avoided since the python api does not know about const pointers so we use a mutable pointer internally.
- Parameters
-
args Arguments represented as tuple
- Returns
- The result of the call
◆ callWithArgumentTupleAndKeywordArgs()
|
inline |
Call this Reference with positional arguments given as tuple and keyword arguments given as dictionary.
If the Reference represents a function it's called. If the Reference represents an instance its call method is invoked. If the Reference represents a class a constructor is invoked.
Although the method is const it might change the referred object! This cannot be avoided since the python api does not know about const pointers so we use a mutable pointer internally.
- Parameters
-
args Positional arguments represented as tuple keywordArgs Keyword arguments represented as dictionary
- Returns
- The result of the call
◆ get()
|
inlineinherited |
◆ hasAttr()
|
inlineinherited |
Check if object has attribute of given name.
- Parameters
-
name Name of attribute
◆ operator PyObject *()
|
inlineinherited |
Access to stored reference.
Return stored reference without changing its count. You can use this with all api methods that do not steal references.
- Returns
- A PyObject* representing a borrowed reference
◆ operator()()
|
inline |
Call this object with given arguments.
Convert given arguments to Python-types and pass them as arguments to Python-callable objects. Keyword arguments can be passed using either Python::arg("keyword", value) or "keyword"_a = value. For the latter you have to import the namespace Python::Literals.
- Returns
- Result of the call as Python object.
◆ operator=()
Assignment.
This will check if the python object is callable. If this is not the case an exception is thrown.
Reimplemented from Python::Reference.
◆ set()
|
inlineinherited |
Set attribute of given name.
- Parameters
-
name Name of attribute value New value of attribute
For implementors:
This will not decrease the reference count for the Reference used as argument.
◆ str()
|
inlineinherited |
String representation of this object.
◆ toC() [1/3]
|
inlineinherited |
Convert to C object.
This does not change the reference count. This method uses the default constructor of T.
- Template Parameters
-
T C type for conversion
◆ toC() [2/3]
|
inlineinherited |
Convert to C object.
This does not change the reference count. This method uses a type dependent custom construct.
- Template Parameters
-
T C type for conversion
◆ toC() [3/3]
|
inlineinherited |
Convert to C object.
This does not change the reference count.
- Template Parameters
-
T C type for conversion
- Parameters
-
t Reference to store the result
Member Data Documentation
◆ p_
|
mutableprotectedinherited |
The documentation for this class was generated from the following file:
