VTK: Common/Core/vtkObject.h Source File

| VTK 9.3.1

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
47 #ifndef vtkObject_h
48 #define vtkObject_h
49
50 #include "vtkCommonCoreModule.h" // For export macro
51 #include "vtkObjectBase.h"
52 #include "vtkSetGet.h"
53 #include "vtkTimeStamp.h"
54 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
55
56 VTK_ABI_NAMESPACE_BEGIN
57 class vtkSubjectHelper;
58 class vtkCommand;
59
60 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
61 {
62 public:
63 vtkBaseTypeMacro(vtkObject, vtkObjectBase);
64
69 static vtkObject* New();
70
71 #ifdef _WIN32
72 // avoid dll boundary problems
73 void* operator new(size_t tSize);
74 void operator delete(void* p);
75 #endif
76
80 virtual void DebugOn();
81
85 virtual void DebugOff();
86
90 bool GetDebug();
91
95 void SetDebug(bool debugFlag);
96
101 static void BreakOnError();
102
109 virtual void Modified();
110
114 virtual vtkMTimeType GetMTime();
115
122 void PrintSelf(ostream& os, vtkIndent indent) override;
123
125
129 static void SetGlobalWarningDisplay(vtkTypeBool val);
130 static void GlobalWarningDisplayOn() { vtkObject::SetGlobalWarningDisplay(1); }
131 static void GlobalWarningDisplayOff() { vtkObject::SetGlobalWarningDisplay(0); }
132 static vtkTypeBool GetGlobalWarningDisplay();
134
136
148 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
149 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
150 vtkCommand* GetCommand(unsigned long tag);
151 void RemoveObserver(vtkCommand*);
152 void RemoveObservers(unsigned long event, vtkCommand*);
153 void RemoveObservers(const char* event, vtkCommand*);
154 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
155 vtkTypeBool HasObserver(const char* event, vtkCommand*);
157
158 void RemoveObserver(unsigned long tag);
159 void RemoveObservers(unsigned long event);
160 void RemoveObservers(const char* event);
161 void RemoveAllObservers(); // remove every last one of them
162 vtkTypeBool HasObserver(unsigned long event);
163 vtkTypeBool HasObserver(const char* event);
164
166
191 template <class U, class T>
192 unsigned long AddObserver(
193 unsigned long event, U observer, void (T::*callback)(), 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 }
200 template <class U, class T>
201 unsigned long AddObserver(unsigned long event, U observer,
202 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
203 {
204 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
205 // callable is deleted when the observer is cleaned up (look at
206 // vtkObjectCommandInternal)
207 return this->AddTemplatedObserver(event, callable, priority);
208 }
210
212
216 template <class U, class T>
217 unsigned long AddObserver(unsigned long event, U observer,
218 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
219 {
220 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
221 // callable is deleted when the observer is cleaned up (look at
222 // vtkObjectCommandInternal)
223 return this->AddTemplatedObserver(event, callable, priority);
224 }
226
228
233 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
234 vtkTypeBool InvokeEvent(const char* event, void* callData);
236
237 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
238 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
239
241
247 virtual void SetObjectName(const std::string& objectName);
248 virtual std::string GetObjectName() const;
250
255 std::string GetObjectDescription() const override;
256
257 protected:
258 vtkObject();
259 ~vtkObject() override;
260
261 // See vtkObjectBase.h.
262 void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
263 void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
264
265 bool Debug; // Enable debug messages
266 vtkTimeStamp MTime; // Keep track of modification time
267 vtkSubjectHelper* SubjectHelper; // List of observers on this object
268 std::string ObjectName; // Name of this object for reporting
269
271
279 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
280 void InternalReleaseFocus();
282
283 private:
284 vtkObject(const vtkObject&) = delete;
285 void operator=(const vtkObject&) = delete;
286
294 class vtkClassMemberCallbackBase
295 {
296 public:
298
301 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
302 virtual ~vtkClassMemberCallbackBase() = default;
304 };
305
307
311 template <class T>
312 class vtkClassMemberHandlerPointer
313 {
314 public:
315 void operator=(vtkObjectBase* o)
316 {
317 // The cast is needed in case "o" has multi-inheritance,
318 // to offset the pointer to get the vtkObjectBase.
319 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
320 {
321 // fallback to just using its vtkObjectBase as-is.
322 this->VoidPointer = o;
323 }
324 this->WeakPointer = o;
325 this->UseWeakPointer = true;
326 }
327 void operator=(void* o)
328 {
329 this->VoidPointer = o;
330 this->WeakPointer = nullptr;
331 this->UseWeakPointer = false;
332 }
333 T* GetPointer()
334 {
335 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
336 {
337 return nullptr;
338 }
339 return static_cast<T*>(this->VoidPointer);
340 }
341
342 private:
343 vtkWeakPointerBase WeakPointer;
344 void* VoidPointer;
345 bool UseWeakPointer;
346 };
348
350
353 template <class T>
354 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
355 {
356 vtkClassMemberHandlerPointer<T> Handler;
357 void (T::*Method1)();
358 void (T::*Method2)(vtkObject*, unsigned long, void*);
359 bool (T::*Method3)(vtkObject*, unsigned long, void*);
360
361 public:
362 vtkClassMemberCallback(T* handler, void (T::*method)())
363 {
364 this->Handler = handler;
365 this->Method1 = method;
366 this->Method2 = nullptr;
367 this->Method3 = nullptr;
368 }
369
370 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
371 {
372 this->Handler = handler;
373 this->Method1 = nullptr;
374 this->Method2 = method;
375 this->Method3 = nullptr;
376 }
377
378 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
379 {
380 this->Handler = handler;
381 this->Method1 = nullptr;
382 this->Method2 = nullptr;
383 this->Method3 = method;
384 }
385 ~vtkClassMemberCallback() override = default;
386
387 // Called when the event is invoked
388 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
389 {
390 T* handler = this->Handler.GetPointer();
391 if (handler)
392 {
393 if (this->Method1)
394 {
395 (handler->*this->Method1)();
396 }
397 else if (this->Method2)
398 {
399 (handler->*this->Method2)(caller, event, calldata);
400 }
401 else if (this->Method3)
402 {
403 return (handler->*this->Method3)(caller, event, calldata);
404 }
405 }
406 return false;
407 }
408 };
410
412
416 void ObjectFinalize() final;
418
420
423 unsigned long AddTemplatedObserver(
424 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
425 // Friend to access AddTemplatedObserver().
426 friend class vtkObjectCommandInternal;
428 };
429
430 VTK_ABI_NAMESPACE_END
431 #endif
432 // VTK-HeaderTest-Exclude: vtkObject.h
vtkCommand
superclass for callback/observer methods
Definition: vtkCommand.h:384
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:38
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:72
vtkObjectBase::ObjectFinalize
virtual void ObjectFinalize()
vtkObjectBase::operator=
void operator=(const vtkObjectBase &)
Definition: vtkObjectBase.h:332
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:61
vtkObject::vtkBaseTypeMacro
vtkBaseTypeMacro(vtkObject, vtkObjectBase)
vtkObject::InternalReleaseFocus
void InternalReleaseFocus()
These methods allow a command to exclusively grab all events.
vtkObject::GetCommand
vtkCommand * GetCommand(unsigned long tag)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::DebugOn
virtual void DebugOn()
Turn debugging output on.
vtkObject::HasObserver
vtkTypeBool HasObserver(unsigned long event)
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkObject::RemoveObservers
void RemoveObservers(const char *event)
vtkObject::SubjectHelper
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:267
vtkObject::GetObjectDescription
std::string GetObjectDescription() const override
The object description printed in messages and PrintSelf output.
vtkObject::SetGlobalWarningDisplay
static void SetGlobalWarningDisplay(vtkTypeBool val)
This is a global flag that controls whether any debug, warning or error messages are displayed.
vtkObject::AddObserver
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:217
vtkObject::HasObserver
vtkTypeBool HasObserver(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::DebugOff
virtual void DebugOff()
Turn debugging output off.
vtkObject::AddObserver
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:201
vtkObject::InvokeEvent
vtkTypeBool InvokeEvent(unsigned long event)
Definition: vtkObject.h:237
vtkObject::~vtkObject
~vtkObject() override
vtkObject::MTime
vtkTimeStamp MTime
Definition: vtkObject.h:266
vtkObject::InternalGrabFocus
void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr)
These methods allow a command to exclusively grab all events.
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkObject::RemoveObserver
void RemoveObserver(vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::RemoveObservers
void RemoveObservers(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::GetObjectName
virtual std::string GetObjectName() const
Set/get the name of this object for reporting purposes.
vtkObject::UnRegisterInternal
void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check) override
vtkObject::RemoveAllObservers
void RemoveAllObservers()
vtkObject::RegisterInternal
void RegisterInternal(vtkObjectBase *, vtkTypeBool check) override
vtkObject::InvokeEvent
vtkTypeBool InvokeEvent(const char *event)
Definition: vtkObject.h:238
vtkObject::GlobalWarningDisplayOff
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:131
vtkObject::Modified
virtual void Modified()
Update the modification time for this object.
vtkObject::HasObserver
vtkTypeBool HasObserver(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::InvokeEvent
vtkTypeBool InvokeEvent(unsigned long event, void *callData)
This method invokes an event and return whether the event was aborted or not.
vtkObject::ObjectName
std::string ObjectName
Definition: vtkObject.h:268
vtkObject::Debug
bool Debug
Definition: vtkObject.h:265
vtkObject::AddObserver
unsigned long AddObserver(unsigned long event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::RemoveObservers
void RemoveObservers(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::AddObserver
unsigned long AddObserver(const char *event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::SetDebug
void SetDebug(bool debugFlag)
Set the value of the debug flag.
vtkObject::HasObserver
vtkTypeBool HasObserver(const char *event)
vtkObject::BreakOnError
static void BreakOnError()
This method is called when vtkErrorMacro executes.
vtkObject::GlobalWarningDisplayOn
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:130
vtkObject::GetDebug
bool GetDebug()
Get the value of the debug flag.
vtkObject::GetMTime
virtual vtkMTimeType GetMTime()
Return this object's modified time.
vtkObject::RemoveObservers
void RemoveObservers(unsigned long event)
vtkObject::vtkObject
vtkObject()
vtkObject::SetObjectName
virtual void SetObjectName(const std::string &objectName)
Set/get the name of this object for reporting purposes.
vtkObject::RemoveObserver
void RemoveObserver(unsigned long tag)
vtkObject::GetGlobalWarningDisplay
static vtkTypeBool GetGlobalWarningDisplay()
This is a global flag that controls whether any debug, warning or error messages are displayed.
vtkObject::AddObserver
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:192
vtkObject::InvokeEvent
vtkTypeBool InvokeEvent(const char *event, void *callData)
This method invokes an event and return whether the event was aborted or not.
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:34
vtkWeakPointerBase
Non-templated superclass for vtkWeakPointer.
Definition: vtkWeakPointerBase.h:23
vtkX3D::priority
@ priority
Definition: vtkX3D.h:450
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:64
vtkObjectBase.h
vtkTimeStamp.h
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
vtkWeakPointerBase.h