Commit 529c3c92 authored by Ken Martin's avatar Ken Martin
Browse files

callback cleanups

parent ad5d9892
SOURCE_FILES( Common_SRCS
vtkAbstractMapper
vtkAbstractTransform
vtkCallbackCommand
vtkCell
vtkCell3D
vtkCommand
......@@ -77,6 +78,7 @@ vtkMatrixToLinearTransform
vtkMultiThreader
vtkMutexLock
vtkObject
vtkOldStyleCallbackCommand
vtkOrderedTriangulator
vtkOutputWindow
vtkPerspectiveTransform
......@@ -136,6 +138,7 @@ ABSTRACT_FILES(
vtkAbstractMapper
vtkAbstractTransform
vtkAttributeData
vtkCallbackCommand
vtkCell
vtkCell3D
vtkCommand
......@@ -146,6 +149,7 @@ vtkLinearTransform
vtkImplicitFunction
vtkLocator
vtkObjectFactory
vtkOldStyleCallbackCommand
vtkOverrideInformation
vtkOverrideInformationCollection
vtkPointSet
......@@ -162,7 +166,12 @@ ADD_LIBRARY(vtkCommon Common_SRCS)
IF (WIN32)
SOURCE_FILES(Common_SRCS vtkWin32OutputWindow)
ENDIF (WIN32)
WRAP_EXCLUDE_FILES(vtkLargeInteger)
WRAP_EXCLUDE_FILES(
vtkLargeInteger
vtkCommand
vtkCallbackCommand
vtkOldStyleCallbackCommand
)
# Allow the user to customize their build with some local options
#
......
......@@ -76,13 +76,22 @@ static const char *vtkCommandEventStrings[] = {
"ModifiedEvent",
"WindowLevelEvent",
"NextDataEvent",
"PushDataStartEvent",
NULL
};
//----------------------------------------------------------------
vtkCommand *vtkCommand::New()
{
return new vtkCallbackCommand;
void vtkCommand::Register()
{
this->ReferenceCount++;
}
void vtkCommand::UnRegister()
{
if (--this->ReferenceCount <= 0)
{
delete this;
}
}
const char *vtkCommand::GetStringFromEventId(unsigned long event)
......@@ -130,54 +139,7 @@ unsigned long vtkCommand::GetEventIdFromString(const char *event)
return vtkCommand::NoEvent;
}
//----------------------------------------------------------------
vtkCallbackCommand::vtkCallbackCommand()
{
this->ClientData = NULL;
this->Callback = NULL;
this->ClientDataDeleteCallback = NULL;
}
vtkCallbackCommand::~vtkCallbackCommand()
{
if (this->ClientDataDeleteCallback)
{
this->ClientDataDeleteCallback(this->ClientData);
}
}
void vtkCallbackCommand::Execute(vtkObject *caller, unsigned long event,
void *callData)
{
if (this->Callback)
{
this->Callback(caller, event, this->ClientData, callData);
}
}
//----------------------------------------------------------------
vtkOldStyleCallbackCommand::vtkOldStyleCallbackCommand()
{
this->ClientData = NULL;
this->Callback = NULL;
this->ClientDataDeleteCallback = NULL;
}
vtkOldStyleCallbackCommand::~vtkOldStyleCallbackCommand()
{
if (this->ClientDataDeleteCallback)
{
this->ClientDataDeleteCallback(this->ClientData);
}
}
void vtkOldStyleCallbackCommand::Execute(vtkObject *,unsigned long, void *)
{
if (this->Callback)
{
this->Callback(this->ClientData);
}
}
......@@ -56,11 +56,23 @@ class vtkObject;
class VTK_COMMON_EXPORT vtkCommand
{
public:
vtkCommand() {};
virtual ~vtkCommand() {};
static vtkCommand *New();
void Delete() {delete this;};
void Delete() {
this->UnRegister(); }
// Description:
// Increase the reference count (mark as used by another object).
void Register();
void Register(vtkObject *) {
this->Register(); }
// Description:
// Decrease the reference count (release by another object). This has
// the same effect as invoking Delete() (i.e., it reduces the reference
// count by 1).
void UnRegister();
void UnRegister(vtkObject *) {
this->UnRegister(); }
virtual void Execute(vtkObject *caller, unsigned long, void *callData) = 0;
static const char *GetStringFromEventId(unsigned long event);
static unsigned long GetEventIdFromString(const char *event);
......@@ -102,64 +114,15 @@ public:
ModifiedEvent,
WindowLevelEvent,
NextDataEvent,
PushDataStartEvent,
UserEvent = 1000
};
//ETX
protected:
int ReferenceCount; // Number of uses of this object by other objects
vtkCommand() { this->ReferenceCount = 1;};
virtual ~vtkCommand() {};
};
//BTX - begin tcl exclude
// a good command to use for generic function callbacks
// the function should have the format
// void func(vtkObject *,void *clientdata, void *calldata)
class VTK_COMMON_EXPORT vtkCallbackCommand : public vtkCommand
{
public:
vtkCallbackCommand();
~vtkCallbackCommand();
static vtkCallbackCommand *New()
{return new vtkCallbackCommand;};
void SetClientData(void *cd)
{this->ClientData = cd;};
void SetCallback(void (*f)(vtkObject *, unsigned long, void *, void *))
{this->Callback = f;};
void SetClientDataDeleteCallback(void (*f)(void *))
{this->ClientDataDeleteCallback = f;};
void Execute(vtkObject *caller, unsigned long event, void *callData);
void *ClientData;
void (*Callback)(vtkObject *, unsigned long, void *, void *);
void (*ClientDataDeleteCallback)(void *);
};
// the old style void fund(void *) callbacks
class VTK_COMMON_EXPORT vtkOldStyleCallbackCommand : public vtkCommand
{
public:
vtkOldStyleCallbackCommand();
~vtkOldStyleCallbackCommand();
static vtkOldStyleCallbackCommand *New()
{return new vtkOldStyleCallbackCommand;};
void SetClientData(void *cd)
{this->ClientData = cd;};
void SetCallback(void (*f)(void *))
{this->Callback = f;};
void SetClientDataDeleteCallback(void (*f)(void *))
{this->ClientDataDeleteCallback = f;};
void Execute(vtkObject *,unsigned long, void *);
void *ClientData;
void (*Callback)(void *);
void (*ClientDataDeleteCallback)(void *);
};
//ETX end tcl exclude
#endif /* __vtkCommand_h */
......@@ -86,8 +86,6 @@ extern JNIEXPORT void vtkJavaVoidFuncArgDelete(void *);
class vtkJavaCommand : public vtkCommand
{
public:
vtkJavaCommand();
~vtkJavaCommand();
static vtkJavaCommand *New() { return new vtkJavaCommand; };
void SetGlobalRef(jobject obj) { this->uobj = obj; };
......@@ -99,6 +97,9 @@ public:
JavaVM *vm;
jobject uobj;
jmethodID mid;
protected:
vtkJavaCommand();
~vtkJavaCommand();
};
#endif
......@@ -307,7 +307,7 @@ vtkObject *vtkObject::SafeDownCast(vtkObject *o)
vtkObserver::~vtkObserver()
{
delete this->Command;
this->Command->UnRegister();
}
vtkSubjectHelper::~vtkSubjectHelper()
......@@ -344,6 +344,7 @@ AddObserver(unsigned long event, vtkCommand *cmd)
elem->Event = event;
elem->Command = cmd;
cmd->Register();
elem->Next = NULL;
elem->Tag = this->Count;
this->Count++;
......
......@@ -41,7 +41,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
#include "vtkProcessObject.h"
#include "vtkObjectFactory.h"
#include "vtkCommand.h"
#include "vtkOldStyleCallbackCommand.h"
//-------------------------------------------------------------------------
vtkProcessObject* vtkProcessObject::New()
......@@ -303,31 +303,34 @@ void vtkProcessObject::UpdateProgress(float amount)
// Specify function to be called before object executes.
void vtkProcessObject::SetStartMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->StartTag);
this->StartTag = this->AddObserver(vtkCommand::StartEvent,cbc);
cbc->Delete();
}
// Specify function to be called to show progress of filter
void vtkProcessObject::SetProgressMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->ProgressTag);
this->ProgressTag = this->AddObserver(vtkCommand::ProgressEvent,cbc);
cbc->Delete();
}
// Specify function to be called after object executes.
void vtkProcessObject::SetEndMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->EndTag);
this->EndTag = this->AddObserver(vtkCommand::EndEvent,cbc);
cbc->Delete();
}
......
......@@ -41,7 +41,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "vtkProp.h"
#include "vtkObjectFactory.h"
#include "vtkAssemblyPaths.h"
#include "vtkCommand.h"
#include "vtkOldStyleCallbackCommand.h"
//----------------------------------------------------------------------------
......@@ -85,11 +85,12 @@ vtkProp::~vtkProp()
// e.g., vtkActor) is picked by vtkPicker.
void vtkProp::SetPickMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->PickTag);
this->PickTag = this->AddObserver(vtkCommand::PickEvent,cbc);
cbc->Delete();
}
// Set a method to delete user arguments for PickMethod.
......
......@@ -143,14 +143,15 @@ extern VTK_PYTHON_EXPORT void vtkPythonVoidFuncArgDelete(void *);
class vtkPythonCommand : public vtkCommand
{
public:
vtkPythonCommand();
~vtkPythonCommand();
static vtkPythonCommand *New() { return new vtkPythonCommand; };
void SetObject(PyObject *o);
void Execute(vtkObject *ptr, unsigned long eventtype, void *);
PyObject *obj;
protected:
vtkPythonCommand();
~vtkPythonCommand();
};
......@@ -43,7 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "vtkObject.h"
#include "vtkTclUtil.h"
#include "vtkSetGet.h"
#include "vtkCallbackCommand.h"
vtkTclInterpStruct *vtkGetInterpStruct(Tcl_Interp *interp)
{
......@@ -292,10 +292,11 @@ vtkTclGetObjectFromPointer(Tcl_Interp *interp, void *temp1,
Tcl_SetHashValue(entry,(ClientData)command);
// setup the delete callback
vtkCallbackCommand *cbc = new vtkCallbackCommand;
vtkCallbackCommand *cbc = vtkCallbackCommand::New();
cbc->SetCallback(vtkTclDeleteObjectFromHash);
cbc->SetClientData((void *)as);
as->Tag = temp->AddObserver(vtkCommand::DeleteEvent, cbc);
cbc->Delete();
Tcl_SetResult(interp, (char *)name, TCL_VOLATILE);
}
......@@ -545,10 +546,11 @@ int vtkTclNewInstanceCommand(ClientData cd, Tcl_Interp *interp,
Tcl_SetHashValue(entry,(ClientData)(cs->CommandFunction));
// setup the delete callback
vtkCallbackCommand *cbc = new vtkCallbackCommand;
vtkCallbackCommand *cbc = vtkCallbackCommand::New();
cbc->SetCallback(vtkTclDeleteObjectFromHash);
cbc->SetClientData((void *)as);
as->Tag = ((vtkObject *)temp)->AddObserver(vtkCommand::DeleteEvent, cbc);
cbc->Delete();
Tcl_SetResult(interp, argv[1], TCL_VOLATILE);
return TCL_OK;
......
......@@ -87,8 +87,6 @@ void vtkTclCreateNew(Tcl_Interp *interp, const char *cname,
class vtkTclCommand : public vtkCommand
{
public:
vtkTclCommand();
~vtkTclCommand();
static vtkTclCommand *New() { return new vtkTclCommand; };
void SetStringCommand(const char *arg);
......@@ -98,6 +96,9 @@ public:
char *StringCommand;
Tcl_Interp *Interp;
protected:
vtkTclCommand();
~vtkTclCommand();
};
typedef struct _vtkTclVoidFuncArg
......
......@@ -47,7 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "vtkActor2DCollection.h"
#include "vtkPropCollection.h"
#include "vtkProp.h"
#include "vtkCommand.h"
#include "vtkOldStyleCallbackCommand.h"
// Create a vtkViewport with a black background, a white ambient light,
// two-sided lighting turned on, a viewport of (0,0,1,1), and backface culling
......@@ -321,11 +321,12 @@ int vtkViewport::IsInViewport(int x,int y)
// Function will be called with argument provided.
void vtkViewport::SetStartRenderMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->StartTag);
this->StartTag = this->AddObserver(vtkCommand::StartEvent,cbc);
cbc->Delete();
}
// Set the arg delete method. This is used to free user memory.
......@@ -354,11 +355,12 @@ void vtkViewport::SetEndRenderMethodArgDelete(void (*f)(void *))
// Function will be called with argument provided.
void vtkViewport::SetEndRenderMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->EndTag);
this->EndTag = this->AddObserver(vtkCommand::EndEvent,cbc);
cbc->Delete();
}
void vtkViewport::PrintSelf(ostream& os, vtkIndent indent)
......
......@@ -40,7 +40,7 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
#include "vtkAbstractPicker.h"
#include "vtkObjectFactory.h"
#include "vtkCommand.h"
#include "vtkOldStyleCallbackCommand.h"
// Construct object with initial tolerance of 1/40th of window. There are no
// pick methods and picking is performed from the renderer's actors.
......@@ -86,32 +86,35 @@ void vtkAbstractPicker::Initialize()
// Specify function to be called as picking operation begins.
void vtkAbstractPicker::SetStartPickMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->StartPickTag);
this->StartPickTag = this->AddObserver(vtkCommand::StartPickEvent,cbc);
cbc->Delete();
}
// Specify function to be called when something is picked.
void vtkAbstractPicker::SetPickMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->StartPickTag);
this->StartPickTag = this->AddObserver(vtkCommand::PickEvent,cbc);
cbc->Delete();
}
// Specify function to be called after all picking operations have been
// performed.
void vtkAbstractPicker::SetEndPickMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->StartPickTag);
this->StartPickTag = this->AddObserver(vtkCommand::EndPickEvent,cbc);
cbc->Delete();
}
......
......@@ -119,6 +119,9 @@ void vtkImageViewer2::SetPosition(int a[2])
class vtkImageViewer2Callback : public vtkCommand
{
public:
static vtkImageViewer2Callback *New() {
return new vtkImageViewer2Callback; }
void Execute(vtkObject *caller, unsigned long event, void *callData)
{
if (callData)
......@@ -205,9 +208,10 @@ void vtkImageViewer2::SetupInteractor(vtkRenderWindowInteractor *rwi)
if (!this->InteractorStyle)
{
this->InteractorStyle = vtkInteractorStyleImage::New();
vtkImageViewer2Callback *cbk = new vtkImageViewer2Callback;
vtkImageViewer2Callback *cbk = vtkImageViewer2Callback::New();
cbk->IV = this;
this->InteractorStyle->AddObserver(vtkCommand::WindowLevelEvent,cbk);
cbk->Delete();
}
if (!this->Interactor)
......
......@@ -45,7 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "vtkMath.h"
#include "vtkCellPicker.h"
#include "vtkAssemblyNode.h"
#include "vtkCommand.h"
#include "vtkOldStyleCallbackCommand.h"
//----------------------------------------------------------------------------
vtkInteractorStyle *vtkInteractorStyle::New()
......@@ -118,11 +118,12 @@ vtkInteractorStyle::~vtkInteractorStyle()
// Set the left button pressed method. This method is invoked on a left mouse button press.
void vtkInteractorStyle::SetLeftButtonPressMethod(void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->LeftButtonPressTag);
this->LeftButtonPressTag = this->AddObserver(vtkCommand::LeftButtonPressEvent,cbc);
cbc->Delete();
}
// Called when a void* argument is being discarded. Lets the user free it.
......@@ -140,11 +141,12 @@ void vtkInteractorStyle::SetLeftButtonPressMethodArgDelete(void (*f)(void *))
void vtkInteractorStyle::SetLeftButtonReleaseMethod(void (*f)(void *),
void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->LeftButtonReleaseTag);
this->LeftButtonReleaseTag = this->AddObserver(vtkCommand::LeftButtonReleaseEvent,cbc);
cbc->Delete();
}
// Called when a void* argument is being discarded. Lets the user free it.
......@@ -162,11 +164,12 @@ void vtkInteractorStyle::SetLeftButtonReleaseMethodArgDelete(void (*f)(void *))
void vtkInteractorStyle::SetMiddleButtonPressMethod(void (*f)(void *),
void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->MiddleButtonPressTag);
this->MiddleButtonPressTag = this->AddObserver(vtkCommand::MiddleButtonPressEvent,cbc);
cbc->Delete();
}
// Called when a void* argument is being discarded. Lets the user free it.
......@@ -184,11 +187,12 @@ void vtkInteractorStyle::SetMiddleButtonPressMethodArgDelete(void (*f)(void *))
void vtkInteractorStyle::SetMiddleButtonReleaseMethod(void (*f)(void *),
void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->MiddleButtonReleaseTag);
this->MiddleButtonPressTag = this->AddObserver(vtkCommand::MiddleButtonReleaseEvent,cbc);
cbc->Delete();
}
// Called when a void* argument is being discarded. Lets the user free it.
......@@ -206,11 +210,12 @@ void vtkInteractorStyle::SetMiddleButtonReleaseMethodArgDelete(void (*f)(void *)
void vtkInteractorStyle::SetRightButtonPressMethod(void (*f)(void *),
void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->RightButtonPressTag);
this->RightButtonPressTag = this->AddObserver(vtkCommand::RightButtonPressEvent,cbc);
cbc->Delete();
}
// Called when a void* argument is being discarded. Lets the user free it.
......@@ -228,11 +233,12 @@ void vtkInteractorStyle::SetRightButtonPressMethodArgDelete(void (*f)(void *))
void vtkInteractorStyle::SetRightButtonReleaseMethod(void (*f)(void *),
void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(this->RightButtonReleaseTag);
this->RightButtonReleaseTag = this->AddObserver(vtkCommand::RightButtonReleaseEvent,cbc);
cbc->Delete();
}
// Called when a void* argument is being discarded. Lets the user free it.
......
......@@ -44,7 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "vtkCellPicker.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkObjectFactory.h"
#include "vtkCommand.h"
#include "vtkOldStyleCallbackCommand.h"
//----------------------------------------------------------------------------
......@@ -104,11 +104,12 @@ void vtkInteractorStyleUser::PrintSelf(ostream& os, vtkIndent indent)
void vtkInteractorStyleUser::vtkSetOldCallback(unsigned long &tag, unsigned long event,
void (*f)(void *), void *arg)
{
vtkOldStyleCallbackCommand *cbc = new vtkOldStyleCallbackCommand;
vtkOldStyleCallbackCommand *cbc = vtkOldStyleCallbackCommand::New();
cbc->Callback = f;
cbc->ClientData = arg;
this->RemoveObserver(tag);
tag = this->AddObserver(event,cbc);