I am using SWIG to create a Python interface to allow plugins for my C++ program.
I have some classes that could look like this:
class TStdFunc
{
public:
void SetColor(unsigned);
unsigned GetColor() const;
bool IsValid() const;
};
An object can be deleted by the user. This actually just moves it to an undo stack so the user can undo the operation. However when an object has been "deleted" I don't want a plugin to change it.
I have therefore created a typemap like this:
%typemap(check) TStdFunc*
{
if(!$1->IsValid())
SWIG_exception_fail(SWIG_RuntimeError, "Element is not valid");
}
This works great. If a plugin has stored a reference to the object and tries to call SetColor() or GetColor() after the object has been "deleted" I get an exception.
However the check is also added to the function _wrap_delete_TStdFunc created by SWIG. So when the object is destroyed from Python, I also get an exception. So how do I get a check in all functions except the delete function? Can I disable the typemap for the delete function?
PS. This is of course a simplified explanation. The objects are actually wrapped with boost::shared_ptr, but I don't think that matters.
I found a solution by accident. Adding this after the class definition will remove the check from the destructor while keeping it in all other functions.
%extend TStdFunc
{
%typemap(check) TStdFunc* ""
}
I assume this works because %extend reopens the class definition and the destructor code is generated when the definition of the class is finished.