Dune-Fufem 2.11-git
Loading...
Searching...
No Matches

Wrapper class for python objects. More...

#include <dune/fufem/python/reference.hh>

Inheritance diagram for Python::Reference:
Inheritance graph

Public Member Functions

 Reference ()
 Construct empty Reference.
 
 Reference (PyObject *p)
 Construct Reference from PyObject*.
 
 Reference (const Reference &other)
 Copy constructor.
 
virtual ~Reference ()
 Destructor.
 
virtual Referenceoperator= (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]

Python::Reference::Reference ( )
inline

Construct empty Reference.

◆ Reference() [2/3]

Python::Reference::Reference ( PyObject *  p)
inline

Construct Reference from PyObject*.

Only to be used if you want to extend the interface using the python api.

For implementors:

Parameters
pThe 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]

Python::Reference::Reference ( const Reference other)
inline

Copy constructor.

For implementors:

This will increment the count for the stored reference.

◆ ~Reference()

virtual Python::Reference::~Reference ( )
inlinevirtual

Destructor.

For implementors:

This will decrement the count for the stored reference.

Member Function Documentation

◆ assertPyObject()

void Python::Reference::assertPyObject ( const std::string origin) const
inlineprotected

Assert that internal PyObject* is not NULL and raise exception otherwise.

Parameters
originA string describing the origin of the error

◆ get()

Reference Python::Reference::get ( const std::string name) const
inline

Query attribute of given name.

Parameters
nameName of attribute
Returns
The attributes value as Reference

For implementors:

The count for the returned Reference is incremented.

◆ hasAttr()

bool Python::Reference::hasAttr ( const std::string name) const
inline

Check if object has attribute of given name.

Parameters
nameName of attribute

◆ operator PyObject *()

Python::Reference::operator PyObject * ( ) const
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=()

virtual Reference & Python::Reference::operator= ( const Reference other)
inlinevirtual

Assignment.

For implementors:

Decrements count for old reference and increments count for new reference.

Reimplemented in Python::Callable, and Python::Module.

◆ set()

template<class V >
void Python::Reference::set ( const std::string name,
const V &  value 
)
inline

Set attribute of given name.

Parameters
nameName of attribute
valueNew value of attribute

For implementors:

This will not decrease the reference count for the Reference used as argument.

◆ str()

std::string Python::Reference::str ( ) const
inline

String representation of this object.

◆ toC() [1/3]

template<class T >
std::enable_if< Conversion< T >::useDefaultConstructorConversion, T >::type Python::Reference::toC ( ) const
inline

Convert to C object.

This does not change the reference count. This method uses the default constructor of T.

Template Parameters
TC type for conversion

◆ toC() [2/3]

template<class T >
std::enable_if< Conversion< T >::useCustomConstructorConversion, T >::type Python::Reference::toC ( ) const
inline

Convert to C object.

This does not change the reference count. This method uses a type dependent custom construct.

Template Parameters
TC type for conversion

◆ toC() [3/3]

template<class T >
void Python::Reference::toC ( T &  t) const
inline

Convert to C object.

This does not change the reference count.

Template Parameters
TC type for conversion
Parameters
tReference to store the result

Member Data Documentation

◆ p_

PyObject* Python::Reference::p_
mutableprotected

The documentation for this class was generated from the following file: