Base::PyHandle< HandledType > Class Template Reference

The PyHandler class This class is the base class of all FreeCAD classes which exports into the python space. More...

#include <PyExport.h>

Public Member Functions

PyObjectgetPyObject () const
 returns the type as PyObject More...
 
bool IsNull () const
 Test if it not handles something. More...
 
bool IsValid () const
 Test if it handles something. More...
 
HandledType & operator* ()
 dereference operators More...
 
const HandledType & operator* () const
 dereference operators More...
 
HandledType * operator-> ()
 dereference operators More...
 
const HandledType * operator-> () const
 dereference operators More...
 
bool operator< (const PyHandle< HandledType > &other) const
 lower operator needed for sorting in maps and sets More...
 
PyHandle< HandledType > & operator= (const PyHandle< HandledType > &other)
 
PyHandle< HandledType > & operator= (HandledType *other)
 
bool operator== (const PyHandle< HandledType > &other) const
 equal operator More...
 
 PyHandle (const PyHandle< HandledType > &ToHandle)
 Copy constructor. More...
 
 PyHandle (HandledType *ToHandle=0L)
 pointer and default constructor the good way would be not using pointer instead using a overwritten new operator in the HandledType class! But is not easy to enforce! More...
 
 ~PyHandle ()
 destructor Release the reference count which cause, if was the last one, the referenced object to destruct! More...
 

Detailed Description

template<class HandledType>
class Base::PyHandle< HandledType >

The PyHandler class This class is the base class of all FreeCAD classes which exports into the python space.

This class handles the creation referencing of the python export object.

Remarks
GetPyObject() returns the associated Python object to any C++ subclasses. As we cannot determine for sure if we can increment the returned Python object from outside of GetPyObject() we have specified that GetPyObject() does already the increment of the reference counter if needed.

E.g. if GetPyObject() always returns a new Python object then no increment is necessary, because at construction time the reference counter is already set to 1. If the Python interpreter stores this object pointer into a local variable and destroys this variable then the reference counter gets decremented (to 0) and the object gets destroyed automatically. In case we didn't make this specification and increment the Python object from outside once again then the reference counter would be set to 2 and there would be no chance to destroy the object again.

The other case is that we have a member variable in our C++ class that holds the Python object then we either can create this Python in the constructor or create it the first time when GetPyObject() gets called. In the destructor then we must decrement the Python object to avoid a memory leak while GetPyObject() then increments the Python object every time it gets called.

Remarks
One big consequence of this specification is that the programmer must know whether the Python interpreter gets the Python object or not. If the interpreter gets the object then it decrements the counter later on when the internal variable is freed. In case the interpreter doesn't get this object then the programmer must do the decrement on their own.
Note
To not to undermine this specification the programmer must make sure to get the Python object always via GetPyObject().
See also
PyHandle @ Python Object handle class Using pointers on classes derived from PyObjectBase would be potentionaly dangerous because you would have to take care of the reference counting of python by your self. Hence this class was designed. It takes care of references and as long as a object of this class exists the handled class get not destructed. That means a PyObjectBase derived object you can only destruct by destructing all FCPyHandle and all python references on it!
PyObjectBase

Constructor & Destructor Documentation

◆ PyHandle() [1/2]

template<class HandledType >
Base::PyHandle< HandledType >::PyHandle ( HandledType *  ToHandle = 0L)

pointer and default constructor the good way would be not using pointer instead using a overwritten new operator in the HandledType class! But is not easy to enforce!

◆ PyHandle() [2/2]

template<class HandledType >
Base::PyHandle< HandledType >::PyHandle ( const PyHandle< HandledType > &  ToHandle)

Copy constructor.

◆ ~PyHandle()

template<class HandledType >
Base::PyHandle< HandledType >::~PyHandle ( )

destructor Release the reference count which cause, if was the last one, the referenced object to destruct!

Member Function Documentation

◆ getPyObject()

template<class HandledType >
PyObject * Base::PyHandle< HandledType >::getPyObject ( void  ) const

◆ IsNull()

template<class HandledType >
bool Base::PyHandle< HandledType >::IsNull ( ) const

Test if it not handles something.

◆ IsValid()

template<class HandledType >
bool Base::PyHandle< HandledType >::IsValid ( ) const

Test if it handles something.

◆ operator*() [1/2]

template<class HandledType >
HandledType & Base::PyHandle< HandledType >::operator* ( )

dereference operators

◆ operator*() [2/2]

template<class HandledType >
const HandledType & Base::PyHandle< HandledType >::operator* ( ) const

dereference operators

◆ operator->() [1/2]

template<class HandledType >
HandledType * Base::PyHandle< HandledType >::operator-> ( )

dereference operators

◆ operator->() [2/2]

template<class HandledType >
const HandledType * Base::PyHandle< HandledType >::operator-> ( ) const

dereference operators

◆ operator<()

template<class HandledType >
bool Base::PyHandle< HandledType >::operator< ( const PyHandle< HandledType > &  other) const

lower operator needed for sorting in maps and sets

◆ operator=() [1/2]

template<class HandledType >
PyHandle< HandledType > & Base::PyHandle< HandledType >::operator= ( const PyHandle< HandledType > &  other)

◆ operator=() [2/2]

template<class HandledType >
PyHandle< HandledType > & Base::PyHandle< HandledType >::operator= ( HandledType *  other)

◆ operator==()

template<class HandledType >
bool Base::PyHandle< HandledType >::operator== ( const PyHandle< HandledType > &  other) const

equal operator


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