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

Wrapper class for callable python objects. More...

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

Inheritance diagram for Python::Callable:
Inheritance graph

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 Callableoperator= (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]

Python::Callable::Callable ( )
inline

Construct empty Callable.

◆ Callable() [2/3]

Python::Callable::Callable ( PyObject *  p)
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]

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

Construct Callable from Reference.

This checks if the python object is callable. If this is not the case an exception is thrown.

For implementors:

This will increment the count for the stored reference.

◆ ~Callable()

virtual Python::Callable::~Callable ( )
inlinevirtual

Destructor.

Member Function Documentation

◆ assertCallable()

static void Python::Callable::assertCallable ( PyObject *  p,
const std::string origin 
)
inlinestaticprotected

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

Parameters
pThe PyObject* pointer that should be checked
originA string describing the origin of the error

◆ assertPyObject()

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

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

Parameters
originA string describing the origin of the error

◆ callWithArgumentTuple()

Reference Python::Callable::callWithArgumentTuple ( const Reference args) const
inline

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
argsArguments represented as tuple
Returns
The result of the call

◆ callWithArgumentTupleAndKeywordArgs()

Reference Python::Callable::callWithArgumentTupleAndKeywordArgs ( const Reference args,
const Reference keywordArgs 
) const
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
argsPositional arguments represented as tuple
keywordArgsKeyword arguments represented as dictionary
Returns
The result of the call

◆ get()

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

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
inlineinherited

Check if object has attribute of given name.

Parameters
nameName of attribute

◆ operator PyObject *()

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

template<class... Args>
Reference Python::Callable::operator() ( const Args &...  args) const
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=()

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

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()

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

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
inlineinherited

String representation of this object.

◆ toC() [1/3]

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

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
inlineinherited

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
inlineinherited

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_
mutableprotectedinherited

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