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

Public Member Functions | |
| Reference () | |
| Construct empty Reference. | |
| Reference (PyObject *p) | |
| Construct Reference from PyObject*. | |
| Reference (const Reference &other) | |
| Copy constructor. | |
| virtual | ~Reference () |
| Destructor. | |
| virtual Reference & | operator= (const Reference &other) |
| Assignment. | |
| 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. | |
Protected Attributes | |
| PyObject * | p_ |
Detailed Description
Wrapper class for python objects.
This class wraps all python objects accessed or generated by the dune python interface. It hides all manual reference counting required by the python api from the user.
Since the internal reference counting is interesting if you want to extend the interface, e.g., by introducing conversions for new types, the details about the internal reference counting are documented where necessary. To avoid confusing the user this is marked 'For implementors:'
For implementors:
This class wraps a PyObject* in order to automate reference counting. Most methods of the python api return a PyObject* representing a new reference that is already counted internally. If you want to dispose such a reference you have to call Py_DECREF manually.
This class takes over ownership of such a new reference represented by PyObject* and calls Py_DECREF automatically on destruction. You can simply use Reference instead of PyObject* in all cases where new references are returned.
Furthermore it wraps some global python api methods as member functions.
A note about constness: We can in general not control constness from C correctly. Since the python api only uses non-const pointers we need to store a mutable pointer internally. Constness of the methods of this class is in general only a reasonable indicator. As obvious exception the call() method is marked const, while the python object might change. Even the get() method might change the python object if getattr is overloaded in python.
Constructor & Destructor Documentation
◆ Reference() [1/3]
|
inline |
Construct empty Reference.
◆ Reference() [2/3]
|
inline |
Construct Reference from PyObject*.
Only to be used if you want to extend the interface using the python api.
For implementors:
- Parameters
-
p The pointer that will be owned by the Reference
Reference(p) steals a reference, i.e., it does always assume that the PyObject* p is a new reference as returned by most api methods. The Reference will take ownership i.e. the reference count will not be incremented but decremented on destruction.
Some api methods return borrowed references. In this case use Reference(Imp::inc(p)) to increment the reference count manually before wrapping.
◆ Reference() [3/3]
|
inline |
Copy constructor.
For implementors:
This will increment the count for the stored reference.
◆ ~Reference()
|
inlinevirtual |
Destructor.
For implementors:
This will decrement the count for the stored reference.
Member Function Documentation
◆ assertPyObject()
|
inlineprotected |
Assert that internal PyObject* is not NULL and raise exception otherwise.
- Parameters
-
origin A string describing the origin of the error
◆ get()
|
inline |
◆ hasAttr()
|
inline |
Check if object has attribute of given name.
- Parameters
-
name Name of attribute
◆ operator PyObject *()
|
inline |
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=()
Assignment.
For implementors:
Decrements count for old reference and increments count for new reference.
Reimplemented in Python::Callable, and Python::Module.
◆ set()
|
inline |
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()
|
inline |
String representation of this object.
◆ toC() [1/3]
|
inline |
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]
|
inline |
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]
|
inline |
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_
|
mutableprotected |
The documentation for this class was generated from the following file:
