PythonQt
Public Member Functions | Protected Member Functions | List of all members
PythonQtObjectPtr Class Reference

a smart pointer that stores a PyObject pointer and that handles reference counting automatically More...

#include <PythonQtObjectPtr.h>

Public Member Functions

 PythonQtObjectPtr ()
 
 PythonQtObjectPtr (const PythonQtObjectPtr &p)
 
 PythonQtObjectPtr (PythonQtObjectPtr &&p) noexcept
 rvalue copy constructor, does not need any incref/decref. More...
 
 PythonQtObjectPtr (PythonQtSafeObjectPtr &&p)
 rvalue copy constructor, does not need any incref/decref. More...
 
 PythonQtObjectPtr (const QVariant &variant)
 If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count. More...
 
 PythonQtObjectPtr (PyObject *o)
 
 ~PythonQtObjectPtr ()
 
bool fromVariant (const QVariant &variant)
 If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count. More...
 
QVariant toVariant ()
 Returns a PythonQtSafeObjectPtr as a QVariant. It does not return a PythonQtObjectPtr, because that would be unsafe regarding the GIL. More...
 
QVariant toLocalVariant ()
 Returns a PythonQtObjectPtr as a QVariant. Only use this when you know that the variant is only used locally and not stored outside of the current GIL scope. More...
 
PythonQtObjectPtroperator= (const PythonQtObjectPtr &p)
 
PythonQtObjectPtroperator= (PythonQtObjectPtr &&p) noexcept
 rvalue assignment operator that steals the reference from p More...
 
PythonQtObjectPtroperator= (PythonQtSafeObjectPtr &&p)
 rvalue assignment operator that steals the reference from p More...
 
PythonQtObjectPtroperator= (PyObject *o)
 
PythonQtObjectPtroperator= (const QVariant &variant)
 
bool operator== (const PythonQtObjectPtr &p) const
 
bool operator!= (const PythonQtObjectPtr &p) const
 
bool operator== (PyObject *p) const
 
bool operator!= (PyObject *p) const
 
bool isNull () const
 
PyObjectoperator-> () const
 
PyObjectoperator* () const
 
 operator PyObject * () const
 
void setNewRef (PyObject *o)
 sets the object and passes the ownership (stealing the reference, in Python slang) More...
 
PyObjectobject () const
 
QVariant evalScript (const QString &script, int start=Py_file_input)
 evaluates the given script code in the context of this object and returns the result value More...
 
QVariant evalCode (PyObject *pycode)
 
void evalFile (const QString &filename)
 evaluates the given code in the context More...
 
void addObject (const QString &name, QObject *object)
 add the given object to the module as a variable with name (it can be removed via clearVariable) More...
 
void addVariable (const QString &name, const QVariant &v)
 add the given variable to the module More...
 
void removeVariable (const QString &name)
 remove the given variable More...
 
QVariant getVariable (const QString &name)
 get the variable with the name of the module, returns an invalid QVariant on error More...
 
QVariant call (const QString &callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
 call the given python object (in the scope of the current object), returns the result converted to a QVariant More...
 
QVariant call (const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
 call the contained python object directly, returns the result converted to a QVariant More...
 
PyObjecttakeObject ()
 

Protected Member Functions

void setObject (PyObject *o)
 

Detailed Description

a smart pointer that stores a PyObject pointer and that handles reference counting automatically

Definition at line 56 of file PythonQtObjectPtr.h.

Constructor & Destructor Documentation

◆ PythonQtObjectPtr() [1/6]

PythonQtObjectPtr::PythonQtObjectPtr ( )
inline

Definition at line 59 of file PythonQtObjectPtr.h.

59 :_object(nullptr) {}

◆ PythonQtObjectPtr() [2/6]

PythonQtObjectPtr::PythonQtObjectPtr ( const PythonQtObjectPtr p)
inline

Definition at line 61 of file PythonQtObjectPtr.h.

62  :_object(nullptr) {
63  setObject(p.object());
64  }
void setObject(PyObject *o)
PyObject * object() const

References object().

◆ PythonQtObjectPtr() [3/6]

PythonQtObjectPtr::PythonQtObjectPtr ( PythonQtObjectPtr &&  p)
inlinenoexcept

rvalue copy constructor, does not need any incref/decref.

Definition at line 67 of file PythonQtObjectPtr.h.

68  :_object(p.takeObject()) {
69  }
PyObject * takeObject()

◆ PythonQtObjectPtr() [4/6]

PythonQtObjectPtr::PythonQtObjectPtr ( PythonQtSafeObjectPtr &&  p)

rvalue copy constructor, does not need any incref/decref.

◆ PythonQtObjectPtr() [5/6]

PythonQtObjectPtr::PythonQtObjectPtr ( const QVariant &  variant)
inline

If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count.

Definition at line 75 of file PythonQtObjectPtr.h.

75  :_object(nullptr) {
76  fromVariant(variant);
77  }
bool fromVariant(const QVariant &variant)
If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference...

◆ PythonQtObjectPtr() [6/6]

PythonQtObjectPtr::PythonQtObjectPtr ( PyObject o)

◆ ~PythonQtObjectPtr()

PythonQtObjectPtr::~PythonQtObjectPtr ( )

Member Function Documentation

◆ addObject()

void PythonQtObjectPtr::addObject ( const QString &  name,
QObject *  object 
)

add the given object to the module as a variable with name (it can be removed via clearVariable)

◆ addVariable()

void PythonQtObjectPtr::addVariable ( const QString &  name,
const QVariant &  v 
)

add the given variable to the module

◆ call() [1/2]

QVariant PythonQtObjectPtr::call ( const QString &  callable,
const QVariantList &  args = QVariantList(),
const QVariantMap &  kwargs = QVariantMap() 
)

call the given python object (in the scope of the current object), returns the result converted to a QVariant

◆ call() [2/2]

QVariant PythonQtObjectPtr::call ( const QVariantList &  args = QVariantList(),
const QVariantMap &  kwargs = QVariantMap() 
)

call the contained python object directly, returns the result converted to a QVariant

◆ evalCode()

QVariant PythonQtObjectPtr::evalCode ( PyObject pycode)

evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string) If pycode is NULL, a python error is printed.

◆ evalFile()

void PythonQtObjectPtr::evalFile ( const QString &  filename)

evaluates the given code in the context

◆ evalScript()

QVariant PythonQtObjectPtr::evalScript ( const QString &  script,
int  start = Py_file_input 
)

evaluates the given script code in the context of this object and returns the result value

◆ fromVariant()

bool PythonQtObjectPtr::fromVariant ( const QVariant &  variant)

If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count.

◆ getVariable()

QVariant PythonQtObjectPtr::getVariable ( const QString &  name)

get the variable with the name of the module, returns an invalid QVariant on error

◆ isNull()

bool PythonQtObjectPtr::isNull ( ) const
inline

Definition at line 137 of file PythonQtObjectPtr.h.

137 { return !object(); }

◆ object()

PyObject* PythonQtObjectPtr::object ( ) const
inline

Definition at line 148 of file PythonQtObjectPtr.h.

148  {
149  return _object;
150  }

Referenced by operator=(), PythonQtSafeObjectPtr::operator=(), operator==(), PythonQtObjectPtr(), and PythonQtSafeObjectPtr::PythonQtSafeObjectPtr().

◆ operator PyObject *()

PythonQtObjectPtr::operator PyObject * ( ) const
inline

Definition at line 143 of file PythonQtObjectPtr.h.

143 { return object(); }

◆ operator!=() [1/2]

bool PythonQtObjectPtr::operator!= ( const PythonQtObjectPtr p) const
inline

Definition at line 125 of file PythonQtObjectPtr.h.

125  {
126  return !( *this == p );
127  }

◆ operator!=() [2/2]

bool PythonQtObjectPtr::operator!= ( PyObject p) const
inline

Definition at line 133 of file PythonQtObjectPtr.h.

133  {
134  return object() != p;
135  }

◆ operator*()

PyObject& PythonQtObjectPtr::operator* ( ) const
inline

Definition at line 141 of file PythonQtObjectPtr.h.

141 { return *( object() ); }

◆ operator->()

PyObject* PythonQtObjectPtr::operator-> ( ) const
inline

Definition at line 139 of file PythonQtObjectPtr.h.

139 { return object(); }

◆ operator=() [1/5]

PythonQtObjectPtr& PythonQtObjectPtr::operator= ( const PythonQtObjectPtr p)
inline

Definition at line 92 of file PythonQtObjectPtr.h.

92  {
93  setObject(p.object());
94  return *this;
95  }

References object().

◆ operator=() [2/5]

PythonQtObjectPtr& PythonQtObjectPtr::operator= ( const QVariant &  variant)
inline

Definition at line 115 of file PythonQtObjectPtr.h.

115  {
116  fromVariant(variant);
117  return *this;
118  }

◆ operator=() [3/5]

PythonQtObjectPtr& PythonQtObjectPtr::operator= ( PyObject o)
inline

Definition at line 109 of file PythonQtObjectPtr.h.

109  {
110  setObject(o);
111  return *this;
112  }

◆ operator=() [4/5]

PythonQtObjectPtr& PythonQtObjectPtr::operator= ( PythonQtObjectPtr &&  p)
inlinenoexcept

rvalue assignment operator that steals the reference from p

Definition at line 98 of file PythonQtObjectPtr.h.

98  {
99  if (_object) {
100  setObject(nullptr);
101  }
102  _object = p.takeObject();
103  return *this;
104  }

◆ operator=() [5/5]

PythonQtObjectPtr& PythonQtObjectPtr::operator= ( PythonQtSafeObjectPtr &&  p)

rvalue assignment operator that steals the reference from p

◆ operator==() [1/2]

bool PythonQtObjectPtr::operator== ( const PythonQtObjectPtr p) const
inline

Definition at line 121 of file PythonQtObjectPtr.h.

121  {
122  return object() == p.object();
123  }

References object().

◆ operator==() [2/2]

bool PythonQtObjectPtr::operator== ( PyObject p) const
inline

Definition at line 129 of file PythonQtObjectPtr.h.

129  {
130  return object() == p;
131  }

◆ removeVariable()

void PythonQtObjectPtr::removeVariable ( const QString &  name)

remove the given variable

◆ setNewRef()

void PythonQtObjectPtr::setNewRef ( PyObject o)

sets the object and passes the ownership (stealing the reference, in Python slang)

◆ setObject()

void PythonQtObjectPtr::setObject ( PyObject o)
protected

◆ takeObject()

PyObject* PythonQtObjectPtr::takeObject ( )
inline

takes the object from the pointer, leaving the pointer empty. the caller has to take care about the decref of the taken object!

Definition at line 182 of file PythonQtObjectPtr.h.

182  {
183  PyObject* o = _object;
184  _object = nullptr;
185  return o;
186  }
struct _object PyObject

◆ toLocalVariant()

QVariant PythonQtObjectPtr::toLocalVariant ( )

Returns a PythonQtObjectPtr as a QVariant. Only use this when you know that the variant is only used locally and not stored outside of the current GIL scope.

◆ toVariant()

QVariant PythonQtObjectPtr::toVariant ( )

Returns a PythonQtSafeObjectPtr as a QVariant. It does not return a PythonQtObjectPtr, because that would be unsafe regarding the GIL.


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