Adonthell  0.4
py_object Class Reference

Python object class. More...

#include <py_object.h>

Public Member Functions

 py_object ()
 Default constructor. More...
 
 ~py_object ()
 Destructor. More...
 
void clear ()
 Resets the script to it's post-constructor state. More...
 
PyObject creation
bool create_instance (string file, string classname, PyObject *args=NULL)
 Creates an instance of a Python class. More...
 
bool reload_instance (string file, string classname, PyObject *args=NULL)
 Similar to create_instance, except that it will reload the module from disk, in case it has been changed in the meantime. More...
 
PyObject method calling
PyObject * call_method_ret (const string &name, PyObject *args=NULL) const
 Call a method of this object. More...
 
void call_method (const string &name, PyObject *args=NULL) const
 Call a method of this object. More...
 
void run (PyObject *args=NULL)
 Calls the run () method of this object. More...
 
PyObject member access
bool has_attribute (const std::string &name)
 Tests whether the object contains a certain attribute (i.e. More...
 
PyObject * get_attribute (const string &name) const
 Returns a new reference to an attribute of this object. More...
 
s_int32 get_attribute_int (const string &name)
 Returns the given attribute as integer value. More...
 
string get_attribute_string (const string &name)
 Returns the given attribute as string value. More...
 
void set_attribute (const string &name, PyObject *value)
 Assign a new attribute to the module, overriding an existing attribute of the same name. More...
 
void set_attribute_int (const string &name, s_int32 value)
 Assign a new integer attribute to the module, overriding an existing attribute of the same name. More...
 
void set_attribute_string (const string &name, const string &value)
 Assign a new string attribute to the module, overriding an existing attribute of the same name. More...
 

Member access

PyObject * Instance
 The python class instance wrapped by py_object. More...
 
PyObject * get_instance (const bool &incref=true) const
 Direct access to the instance object. More...
 
std::string class_name () const
 Returns the class name of this object. More...
 
std::string file_name () const
 Returns the file name of this object. More...
 

Detailed Description

Python object class.

Use this class to create instances of Python classes contained in Python modules, then control their execution. You can pass an argument tuple to the class constructor and to any method you want to run. It is further possible to access and change attributes of the Python instance.

Definition at line 45 of file py_object.h.

Constructor & Destructor Documentation

◆ py_object()

py_object::py_object ( )

Default constructor.

Definition at line 33 of file py_object.cc.

◆ ~py_object()

py_object::~py_object ( )

Destructor.

Definition at line 40 of file py_object.cc.

Member Function Documentation

◆ clear()

void py_object::clear ( )

Resets the script to it's post-constructor state.

Definition at line 46 of file py_object.cc.

◆ create_instance()

bool py_object::create_instance ( string  file,
string  classname,
PyObject *  args = NULL 
)

Creates an instance of a Python class.

Parameters
filefile name of the module to use.
classnamename of the class to import.
argsPython tuple containing the arguments to pass to the Python class constructor.

Definition at line 57 of file py_object.cc.

◆ reload_instance()

bool py_object::reload_instance ( string  file,
string  classname,
PyObject *  args = NULL 
)

Similar to create_instance, except that it will reload the module from disk, in case it has been changed in the meantime.

Mainly interesting for script development or tools like dlgedit.

Parameters
filefile name of the module to use.
classnamename of the class to import.
argsPython tuple containing the arguments to pass to the Python class constructor.

Definition at line 68 of file py_object.cc.

◆ call_method_ret()

PyObject * py_object::call_method_ret ( const string &  name,
PyObject *  args = NULL 
) const

Call a method of this object.

Parameters
namename of the method to call.
argsPython tuple containing the arguments to pass to the method.
Returns
the return value of the method as PyObject. Needs to be Py_DECREF'd when no longer needed.

Definition at line 112 of file py_object.cc.

◆ call_method()

void py_object::call_method ( const string &  name,
PyObject *  args = NULL 
) const
inline

Call a method of this object.

Parameters
namename of the method to call.
argsPython tuple containing the arguments to pass to the method.

Definition at line 113 of file py_object.h.

◆ run()

void py_object::run ( PyObject *  args = NULL)
inline

Calls the run () method of this object.

Equivalent to call_method ("run", args);

Parameters
argsPython tuple containing the arguments to pass to the method.

Definition at line 125 of file py_object.h.

◆ has_attribute()

bool py_object::has_attribute ( const std::string &  name)

Tests whether the object contains a certain attribute (i.e.

method or variable).

Parameters
nameName of the attribute to test for
Returns
true if the attribute exists, false otherwise.

Definition at line 134 of file py_object.cc.

◆ get_attribute()

PyObject * py_object::get_attribute ( const string &  name) const

Returns a new reference to an attribute of this object.

Parameters
nameName of the attribute to access
Returns
New reference to the attribute or NULL on error

Definition at line 143 of file py_object.cc.

◆ get_attribute_int()

s_int32 py_object::get_attribute_int ( const string &  name)

Returns the given attribute as integer value.

Parameters
nameName of the attribute to access
Returns
An integer.

Definition at line 152 of file py_object.cc.

◆ get_attribute_string()

string py_object::get_attribute_string ( const string &  name)

Returns the given attribute as string value.

Parameters
nameName of the attribute to access
Returns
A string.

Definition at line 169 of file py_object.cc.

◆ set_attribute()

void py_object::set_attribute ( const string &  name,
PyObject *  value 
)

Assign a new attribute to the module, overriding an existing attribute of the same name.

Parameters
nameThe attribute's name
valueThe attribute's value

Definition at line 186 of file py_object.cc.

◆ set_attribute_int()

void py_object::set_attribute_int ( const string &  name,
s_int32  value 
)

Assign a new integer attribute to the module, overriding an existing attribute of the same name.

Parameters
nameThe attribute's name
valueThe attribute's value

Definition at line 194 of file py_object.cc.

◆ set_attribute_string()

void py_object::set_attribute_string ( const string &  name,
const string &  value 
)

Assign a new string attribute to the module, overriding an existing attribute of the same name.

Parameters
nameThe attribute's name
valueThe attribute's value

Definition at line 209 of file py_object.cc.

◆ get_instance()

PyObject* py_object::get_instance ( const bool &  incref = true) const
inline

Direct access to the instance object.

The default behaviour is to increase the instance's reference count, so that this method can be safely called from Python scripts.

Parameters
increfwhether to increase the reference count.
Returns
the Python class instance.

Definition at line 208 of file py_object.h.

◆ class_name()

std::string py_object::class_name ( ) const
inline

Returns the class name of this object.

This is the name of the wrapped Python class.

Returns
class name of this object.

Definition at line 223 of file py_object.h.

◆ file_name()

std::string py_object::file_name ( ) const
inline

Returns the file name of this object.

This is the name of the Python module containing the wrapped class.

Returns
fi�e name of this object.

Definition at line 234 of file py_object.h.

Member Data Documentation

◆ Instance

PyObject* py_object::Instance
protected

The python class instance wrapped by py_object.

Definition at line 244 of file py_object.h.


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