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
38#ifndef vtkObject_h
39#define vtkObject_h
40
41#include "vtkCommonCoreModule.h" // For export macro
42#include "vtkObjectBase.h"
43#include "vtkSetGet.h"
44#include "vtkTimeStamp.h"
45#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
46
47VTK_ABI_NAMESPACE_BEGIN
48class vtkSubjectHelper;
49class vtkCommand;
50
51class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
52{
53public:
55
60 static vtkObject* New();
61
62#ifdef _WIN32
63 // avoid dll boundary problems
64 void* operator new(size_t tSize);
65 void operator delete(void* p);
66#endif
67
71 virtual void DebugOn();
72
76 virtual void DebugOff();
77
81 bool GetDebug();
82
86 void SetDebug(bool debugFlag);
87
92 static void BreakOnError();
93
100 virtual void Modified();
101
106
113 void PrintSelf(ostream& os, vtkIndent indent) override;
114
116
125
127
139 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
140 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
141 vtkCommand* GetCommand(unsigned long tag);
143 void RemoveObservers(unsigned long event, vtkCommand*);
144 void RemoveObservers(const char* event, vtkCommand*);
145 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
146 vtkTypeBool HasObserver(const char* event, vtkCommand*);
148
149 void RemoveObserver(unsigned long tag);
150 void RemoveObservers(unsigned long event);
151 void RemoveObservers(const char* event);
152 void RemoveAllObservers(); // remove every last one of them
153 vtkTypeBool HasObserver(unsigned long event);
154 vtkTypeBool HasObserver(const char* event);
155
157
182 template <class U, class T>
183 unsigned long AddObserver(
184 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
185 {
186 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
187 // callable is deleted when the observer is cleaned up (look at
188 // vtkObjectCommandInternal)
189 return this->AddTemplatedObserver(event, callable, priority);
190 }
191 template <class U, class T>
192 unsigned long AddObserver(unsigned long event, U observer,
193 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
194 {
195 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
196 // callable is deleted when the observer is cleaned up (look at
197 // vtkObjectCommandInternal)
198 return this->AddTemplatedObserver(event, callable, priority);
199 }
201
203
207 template <class U, class T>
208 unsigned long AddObserver(unsigned long event, U observer,
209 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
210 {
211 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
212 // callable is deleted when the observer is cleaned up (look at
213 // vtkObjectCommandInternal)
214 return this->AddTemplatedObserver(event, callable, priority);
215 }
217
219
224 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
225 vtkTypeBool InvokeEvent(const char* event, void* callData);
227
228 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
229 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
230
232
238 virtual void SetObjectName(const std::string& objectName);
239 virtual std::string GetObjectName() const;
241
246 std::string GetObjectDescription() const override;
247
248protected:
250 ~vtkObject() override;
251
252 // See vtkObjectBase.h.
255
256 bool Debug; // Enable debug messages
257 vtkTimeStamp MTime; // Keep track of modification time
258 vtkSubjectHelper* SubjectHelper; // List of observers on this object
259 std::string ObjectName; // Name of this object for reporting
260
262
270 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
273
274private:
275 vtkObject(const vtkObject&) = delete;
276 void operator=(const vtkObject&) = delete;
277
285 class vtkClassMemberCallbackBase
286 {
287 public:
289
292 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
293 virtual ~vtkClassMemberCallbackBase() = default;
295 };
296
298
302 template <class T>
303 class vtkClassMemberHandlerPointer
304 {
305 public:
306 void operator=(vtkObjectBase* o)
307 {
308 // The cast is needed in case "o" has multi-inheritance,
309 // to offset the pointer to get the vtkObjectBase.
310 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
311 {
312 // fallback to just using its vtkObjectBase as-is.
313 this->VoidPointer = o;
314 }
315 this->WeakPointer = o;
316 this->UseWeakPointer = true;
317 }
318 void operator=(void* o)
319 {
320 this->VoidPointer = o;
321 this->WeakPointer = nullptr;
322 this->UseWeakPointer = false;
323 }
324 T* GetPointer()
325 {
326 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
327 {
328 return nullptr;
329 }
330 return static_cast<T*>(this->VoidPointer);
331 }
332
333 private:
334 vtkWeakPointerBase WeakPointer;
335 void* VoidPointer;
336 bool UseWeakPointer;
337 };
339
341
344 template <class T>
345 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
346 {
347 vtkClassMemberHandlerPointer<T> Handler;
348 void (T::*Method1)();
349 void (T::*Method2)(vtkObject*, unsigned long, void*);
350 bool (T::*Method3)(vtkObject*, unsigned long, void*);
351
352 public:
353 vtkClassMemberCallback(T* handler, void (T::*method)())
354 {
355 this->Handler = handler;
356 this->Method1 = method;
357 this->Method2 = nullptr;
358 this->Method3 = nullptr;
359 }
360
361 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
362 {
363 this->Handler = handler;
364 this->Method1 = nullptr;
365 this->Method2 = method;
366 this->Method3 = nullptr;
367 }
368
369 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
370 {
371 this->Handler = handler;
372 this->Method1 = nullptr;
373 this->Method2 = nullptr;
374 this->Method3 = method;
375 }
376 ~vtkClassMemberCallback() override = default;
377
378 // Called when the event is invoked
379 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
380 {
381 T* handler = this->Handler.GetPointer();
382 if (handler)
383 {
384 if (this->Method1)
385 {
386 (handler->*this->Method1)();
387 }
388 else if (this->Method2)
389 {
390 (handler->*this->Method2)(caller, event, calldata);
391 }
392 else if (this->Method3)
393 {
394 return (handler->*this->Method3)(caller, event, calldata);
395 }
396 }
397 return false;
398 }
399 };
401
403
407 void ObjectFinalize() final;
409
411
414 unsigned long AddTemplatedObserver(
415 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
416 // Friend to access AddTemplatedObserver().
417 friend class vtkObjectCommandInternal;
419};
420
421VTK_ABI_NAMESPACE_END
422#endif
423// VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition vtkCommand.h:384
a simple class to control print indentation
Definition vtkIndent.h:29
abstract base class for most VTK objects
virtual void ObjectFinalize()
void operator=(const vtkObjectBase &)
abstract base class for most VTK objects
Definition vtkObject.h:52
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:258
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:208
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:192
vtkTypeBool InvokeEvent(unsigned long event)
Definition vtkObject.h:228
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:257
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:229
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:122
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:259
bool Debug
Definition vtkObject.h:256
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:121
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:183
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