VTK  9.3.0
vtkObject.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
44#ifndef vtkObject_h
45#define vtkObject_h
46
47#include "vtkCommonCoreModule.h" // For export macro
48#include "vtkObjectBase.h"
49#include "vtkSetGet.h"
50#include "vtkTimeStamp.h"
51#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
52
53VTK_ABI_NAMESPACE_BEGIN
54class vtkSubjectHelper;
55class vtkCommand;
56
57class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
58{
59public:
61
66 static vtkObject* New();
67
68#ifdef _WIN32
69 // avoid dll boundary problems
70 void* operator new(size_t tSize);
71 void operator delete(void* p);
72#endif
73
77 virtual void DebugOn();
78
82 virtual void DebugOff();
83
87 bool GetDebug();
88
92 void SetDebug(bool debugFlag);
93
98 static void BreakOnError();
99
106 virtual void Modified();
107
112
119 void PrintSelf(ostream& os, vtkIndent indent) override;
120
122
131
133
145 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
146 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
147 vtkCommand* GetCommand(unsigned long tag);
149 void RemoveObservers(unsigned long event, vtkCommand*);
150 void RemoveObservers(const char* event, vtkCommand*);
151 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
152 vtkTypeBool HasObserver(const char* event, vtkCommand*);
154
155 void RemoveObserver(unsigned long tag);
156 void RemoveObservers(unsigned long event);
157 void RemoveObservers(const char* event);
158 void RemoveAllObservers(); // remove every last one of them
159 vtkTypeBool HasObserver(unsigned long event);
160 vtkTypeBool HasObserver(const char* event);
161
163
188 template <class U, class T>
189 unsigned long AddObserver(
190 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
191 {
192 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
193 // callable is deleted when the observer is cleaned up (look at
194 // vtkObjectCommandInternal)
195 return this->AddTemplatedObserver(event, callable, priority);
196 }
197 template <class U, class T>
198 unsigned long AddObserver(unsigned long event, U observer,
199 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
200 {
201 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
202 // callable is deleted when the observer is cleaned up (look at
203 // vtkObjectCommandInternal)
204 return this->AddTemplatedObserver(event, callable, priority);
205 }
207
209
213 template <class U, class T>
214 unsigned long AddObserver(unsigned long event, U observer,
215 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
216 {
217 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
218 // callable is deleted when the observer is cleaned up (look at
219 // vtkObjectCommandInternal)
220 return this->AddTemplatedObserver(event, callable, priority);
221 }
223
225
230 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
231 vtkTypeBool InvokeEvent(const char* event, void* callData);
233
234 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
235 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
236
238
244 virtual void SetObjectName(const std::string& objectName);
245 virtual std::string GetObjectName() const;
247
252 std::string GetObjectDescription() const override;
253
254protected:
256 ~vtkObject() override;
257
258 // See vtkObjectBase.h.
261
262 bool Debug; // Enable debug messages
263 vtkTimeStamp MTime; // Keep track of modification time
264 vtkSubjectHelper* SubjectHelper; // List of observers on this object
265 std::string ObjectName; // Name of this object for reporting
266
268
276 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
279
280private:
281 vtkObject(const vtkObject&) = delete;
282 void operator=(const vtkObject&) = delete;
283
291 class vtkClassMemberCallbackBase
292 {
293 public:
295
298 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
299 virtual ~vtkClassMemberCallbackBase() = default;
301 };
302
304
308 template <class T>
309 class vtkClassMemberHandlerPointer
310 {
311 public:
312 void operator=(vtkObjectBase* o)
313 {
314 // The cast is needed in case "o" has multi-inheritance,
315 // to offset the pointer to get the vtkObjectBase.
316 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
317 {
318 // fallback to just using its vtkObjectBase as-is.
319 this->VoidPointer = o;
320 }
321 this->WeakPointer = o;
322 this->UseWeakPointer = true;
323 }
324 void operator=(void* o)
325 {
326 this->VoidPointer = o;
327 this->WeakPointer = nullptr;
328 this->UseWeakPointer = false;
329 }
330 T* GetPointer()
331 {
332 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
333 {
334 return nullptr;
335 }
336 return static_cast<T*>(this->VoidPointer);
337 }
338
339 private:
340 vtkWeakPointerBase WeakPointer;
341 void* VoidPointer;
342 bool UseWeakPointer;
343 };
345
347
350 template <class T>
351 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
352 {
353 vtkClassMemberHandlerPointer<T> Handler;
354 void (T::*Method1)();
355 void (T::*Method2)(vtkObject*, unsigned long, void*);
356 bool (T::*Method3)(vtkObject*, unsigned long, void*);
357
358 public:
359 vtkClassMemberCallback(T* handler, void (T::*method)())
360 {
361 this->Handler = handler;
362 this->Method1 = method;
363 this->Method2 = nullptr;
364 this->Method3 = nullptr;
365 }
366
367 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
368 {
369 this->Handler = handler;
370 this->Method1 = nullptr;
371 this->Method2 = method;
372 this->Method3 = nullptr;
373 }
374
375 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
376 {
377 this->Handler = handler;
378 this->Method1 = nullptr;
379 this->Method2 = nullptr;
380 this->Method3 = method;
381 }
382 ~vtkClassMemberCallback() override = default;
383
384 // Called when the event is invoked
385 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
386 {
387 T* handler = this->Handler.GetPointer();
388 if (handler)
389 {
390 if (this->Method1)
391 {
392 (handler->*this->Method1)();
393 }
394 else if (this->Method2)
395 {
396 (handler->*this->Method2)(caller, event, calldata);
397 }
398 else if (this->Method3)
399 {
400 return (handler->*this->Method3)(caller, event, calldata);
401 }
402 }
403 return false;
404 }
405 };
407
409
413 void ObjectFinalize() final;
415
417
420 unsigned long AddTemplatedObserver(
421 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
422 // Friend to access AddTemplatedObserver().
423 friend class vtkObjectCommandInternal;
425};
426
427VTK_ABI_NAMESPACE_END
428#endif
429// VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition vtkCommand.h:384
a simple class to control print indentation
Definition vtkIndent.h:38
abstract base class for most VTK objects
virtual void ObjectFinalize()
void operator=(const vtkObjectBase &)
abstract base class for most VTK objects
Definition vtkObject.h:58
vtkBaseTypeMacro(vtkObject, vtkObjectBase)
void InternalReleaseFocus()
These methods allow a command to exclusively grab all events.
virtual void DebugOn()
Turn debugging output on.
vtkTypeBool HasObserver(unsigned long event)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void RemoveObservers(const char *event)
vtkSubjectHelper * SubjectHelper
Definition vtkObject.h:264
std::string GetObjectDescription() const override
The object description printed in messages and PrintSelf output.
static void SetGlobalWarningDisplay(vtkTypeBool val)
This is a global flag that controls whether any debug, warning or error messages are displayed.
unsigned long AddObserver(unsigned long event, U observer, bool(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Allow user to set the AbortFlagOn() with the return value of the callback method.
Definition vtkObject.h:214
vtkTypeBool HasObserver(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
virtual void DebugOff()
Turn debugging output off.
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Overloads to AddObserver that allow developers to add class member functions as callbacks for events.
Definition vtkObject.h:198
vtkTypeBool InvokeEvent(unsigned long event)
Definition vtkObject.h:234
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:263
void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr)
These methods allow a command to exclusively grab all events.
void RemoveObserver(vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
void RemoveObservers(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
virtual std::string GetObjectName() const
Set/get the name of this object for reporting purposes.
void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check) override
void RemoveAllObservers()
void RegisterInternal(vtkObjectBase *, vtkTypeBool check) override
vtkTypeBool InvokeEvent(const char *event)
Definition vtkObject.h:235
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:128
virtual void Modified()
Update the modification time for this object.
vtkTypeBool HasObserver(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkTypeBool InvokeEvent(unsigned long event, void *callData)
This method invokes an event and return whether the event was aborted or not.
std::string ObjectName
Definition vtkObject.h:265
bool Debug
Definition vtkObject.h:262
unsigned long AddObserver(unsigned long event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
void RemoveObservers(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
unsigned long AddObserver(const char *event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
void SetDebug(bool debugFlag)
Set the value of the debug flag.
vtkTypeBool HasObserver(const char *event)
static void BreakOnError()
This method is called when vtkErrorMacro executes.
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:127
bool GetDebug()
Get the value of the debug flag.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
void RemoveObservers(unsigned long event)
virtual void SetObjectName(const std::string &objectName)
Set/get the name of this object for reporting purposes.
void RemoveObserver(unsigned long tag)
static vtkTypeBool GetGlobalWarningDisplay()
This is a global flag that controls whether any debug, warning or error messages are displayed.
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(), float priority=0.0f)
Overloads to AddObserver that allow developers to add class member functions as callbacks for events.
Definition vtkObject.h:189
vtkCommand * GetCommand(unsigned long tag)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkTypeBool InvokeEvent(const char *event, void *callData)
This method invokes an event and return whether the event was aborted or not.
record modification and/or execution time
Non-templated superclass for vtkWeakPointer.
int vtkTypeBool
Definition vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:270