Michele De Stefano's C++ Utilities
|
This is a simple wrapper around the PyObject* datatype. More...
#include <mds_utils/python/obj.hpp>
Classes | |
class | ProxyAttr |
Proxy class for managing attribute access. More... | |
Public Member Functions | |
Obj () | |
Default constructor. | |
Obj (const Obj &rhs) | |
The copy-constructor. More... | |
Obj (Obj &&rhs) | |
Move constructor. More... | |
Obj (ProxyAttr &&rhs) | |
Move constructor from ProxyAttr objects. More... | |
Obj (PyObject *po) | |
Construct from a Python object. More... | |
Obj & | operator= (const Obj &rhs) |
Standard assignment. | |
Obj & | operator= (Obj &&rhs) |
Move assignment. | |
virtual Obj & | operator= (PyObject *po) |
Assignment from a Python object. More... | |
template<class T > | |
Obj & | operator= (const T &val) |
Assignment from a value. More... | |
virtual | ~Obj () |
Destructor. | |
PyObject * | getPyObject () const |
Returns the underlying PyObject. More... | |
operator PyObject * () const | |
Automatic type conversion towards a Python object. More... | |
virtual PyObject * | transfer () |
Returns the Python object with transferred ownership. More... | |
virtual void | incref () |
Increments the reference count using Py_XINCREF. More... | |
virtual void | decref () |
Decrements the reference count using Py_XDECREF. | |
void | reset () |
Resets the object to the state given by the default constructor. | |
void | get_ownership () |
Used in place of incref, when the wrapped PyObject* was increfed already. | |
bool | has_attr (const std::string &name) const |
Tests if the object has a particular attribute. | |
ProxyAttr | attr (const std::string &name) |
Retrieves an attribute. More... | |
bool | is_callable () const |
Checks if the object is callable. More... | |
Obj | operator() () |
Calling operator. More... | |
Obj | operator() (const Obj &args) |
Calling operator, with positional arguments. More... | |
Obj | operator() (const Obj &args, const Obj &kw) |
Calling operator, with positional and keyword arguments. More... | |
Protected Member Functions | |
void | check_callable () |
Checks if the object is callable. More... | |
void | check_call_result (PyObject *result) |
Checks if the result of the object call was successful. More... | |
Protected Attributes | |
size_t | decref_on_destroy |
PyObject * | m_po |
Underlying pointer to the wrapped Python object. | |
This is a simple wrapper around the PyObject* datatype.
It automatically manages reference counting when needed and offers easy access to methods and attributes. Reference counting can be handled explicitly through the provided methods. The destructor automatically knows when it has to decrement the reference count. When not stated otherwise, the default behavior is to increment the reference count of the object.
|
inline |
|
inline |
Move constructor.
rhs | The right-hand-side of the move. |
Definition at line 222 of file obj.hpp.
References get_ownership().
|
inline |
|
inline |
|
inline |
Retrieves an attribute.
If the attribute is not found, it throws an exception.
name | The attribute name. |
Definition at line 407 of file obj.hpp.
Referenced by mds_utils::python::ublas::get(), and mds_utils::python::ublas::to_python().
|
inlineprotected |
Checks if the result of the object call was successful.
Throws an exception if the call was unsuccessful.
Definition at line 189 of file obj.hpp.
Referenced by operator()().
|
inlineprotected |
Checks if the object is callable.
Throws an exception if the object is not callable.
Definition at line 175 of file obj.hpp.
References is_callable().
Referenced by operator()().
|
inline |
Returns the underlying PyObject.
It does not transfer ownership.
Definition at line 326 of file obj.hpp.
References m_po.
Referenced by boost::numeric::ublas::NumPy1DArray< T >::assign_temporary(), boost::numeric::ublas::NumPy1DArray< T >::decref(), boost::numeric::ublas::NumPy1DArray< T >::getPyArrayObject(), boost::numeric::ublas::NumPy1DArray< T >::incref(), boost::numeric::ublas::NumPy1DArray< T >::NumPy1DArray(), boost::numeric::ublas::NumPy1DArray< T >::operator=(), and boost::numeric::ublas::NumPy1DArray< T >::transfer().
|
inlinevirtual |
Increments the reference count using Py_XINCREF.
Reimplemented in boost::numeric::ublas::NumPy1DArray< T >.
Definition at line 363 of file obj.hpp.
References decref_on_destroy.
Referenced by mds_utils::python::Dictionary::get_item(), Obj(), operator=(), and mds_utils::python::Sequence_Base< List >::set().
|
inline |
Checks if the object is callable.
Definition at line 420 of file obj.hpp.
Referenced by check_callable().
|
inline |
|
inline |
Calling operator.
Definition at line 435 of file obj.hpp.
References check_call_result(), and check_callable().
Calling operator, with positional arguments.
args | Tuple of positional arguments. Setting args to the default-constructed Tuple will produce the same effect as calling operator()(). |
Definition at line 467 of file obj.hpp.
References check_call_result(), and check_callable().
Calling operator, with positional and keyword arguments.
args | Tuple of positional arguments. It cannot be default-constructed Obj (because it cannot wrap a NULL pointer). When positional arguments are not needed, pass an empty Tuple (i.e. a Tuple of length 0). |
kw | Dictionary of keyworkd arguments. It can be the default constructed Dictionary (i.e. a Dictionary wrapping a NULL object) if keyword arguments are not needed. |
Definition at line 504 of file obj.hpp.
References check_call_result(), check_callable(), Obj(), and mds_utils::python::to_python().
|
inlinevirtual |
Assignment from a Python object.
po | Pointer to the Python object. |
po
pointer. You have to do it manually, if needed, on the Obj instance. In Python terminology, the reference to po
is borrowed.Reimplemented in boost::numeric::ublas::NumPy1DArray< T >.
Definition at line 285 of file obj.hpp.
References reset().
|
inline |
Assignment from a value.
This method uses the mds_utils::python::to_python function. Providing the proper overload for mds_utils::python::to_python allows this function to properly work for any type.
val | The value to assign. |
Definition at line 308 of file obj.hpp.
References get_ownership(), reset(), and mds_utils::python::to_python().
|
inlinevirtual |
Returns the Python object with transferred ownership.
Reimplemented in boost::numeric::ublas::NumPy1DArray< T >.
Definition at line 350 of file obj.hpp.
References reset().
Referenced by operator=(), and mds_utils::python::ublas::to_python().
|
protected |
Counts the number of times this object has to be decrefed on destroy.
Definition at line 84 of file obj.hpp.
Referenced by boost::numeric::ublas::NumPy1DArray< T >::decref(), decref(), get_ownership(), boost::numeric::ublas::NumPy1DArray< T >::incref(), and incref().