VTK  9.3.20240424
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
147#ifndef vtkObject_h
148#define vtkObject_h
149
150#include "vtkCommonCoreModule.h" // For export macro
151#include "vtkObjectBase.h"
152#include "vtkSetGet.h"
153#include "vtkTimeStamp.h"
154#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
155#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
156
157VTK_ABI_NAMESPACE_BEGIN
158class vtkSubjectHelper;
159class vtkCommand;
160
161class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObject : public vtkObjectBase
162{
163public:
165
170 static vtkObject* New();
171
172#ifdef _WIN32
173 // avoid dll boundary problems
174 void* operator new(size_t tSize);
175 void operator delete(void* p);
176#endif
177
181 virtual void DebugOn();
182
186 virtual void DebugOff();
187
192 bool GetDebug();
193
198 void SetDebug(bool debugFlag);
199
204 static void BreakOnError();
205
212 virtual void Modified();
213
217 VTK_MARSHALGETTER(MTime)
218 virtual vtkMTimeType GetMTime();
219
226 void PrintSelf(ostream& os, vtkIndent indent) override;
227
229
233 static void SetGlobalWarningDisplay(vtkTypeBool val);
234 static void GlobalWarningDisplayOn() { vtkObject::SetGlobalWarningDisplay(1); }
238
240
252 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
253 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
254 vtkCommand* GetCommand(unsigned long tag);
256 void RemoveObservers(unsigned long event, vtkCommand*);
257 void RemoveObservers(const char* event, vtkCommand*);
258 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
259 vtkTypeBool HasObserver(const char* event, vtkCommand*);
261
262 void RemoveObserver(unsigned long tag);
263 void RemoveObservers(unsigned long event);
264 void RemoveObservers(const char* event);
265 void RemoveAllObservers(); // remove every last one of them
266 vtkTypeBool HasObserver(unsigned long event);
267 vtkTypeBool HasObserver(const char* event);
268
270
295 template <class U, class T>
296 unsigned long AddObserver(
297 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
298 {
299 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
300 // callable is deleted when the observer is cleaned up (look at
301 // vtkObjectCommandInternal)
302 return this->AddTemplatedObserver(event, callable, priority);
303 }
304 template <class U, class T>
305 unsigned long AddObserver(unsigned long event, U observer,
306 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
307 {
308 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
309 // callable is deleted when the observer is cleaned up (look at
310 // vtkObjectCommandInternal)
311 return this->AddTemplatedObserver(event, callable, priority);
312 }
314
316
320 template <class U, class T>
321 unsigned long AddObserver(unsigned long event, U observer,
322 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
323 {
324 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
325 // callable is deleted when the observer is cleaned up (look at
326 // vtkObjectCommandInternal)
327 return this->AddTemplatedObserver(event, callable, priority);
328 }
330
332
337 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
338 vtkTypeBool InvokeEvent(const char* event, void* callData);
340
341 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
342 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
343
345
351 virtual void SetObjectName(const std::string& objectName);
352 virtual std::string GetObjectName() const;
354
359 std::string GetObjectDescription() const override;
360
361protected:
363 ~vtkObject() override;
364
365 // See vtkObjectBase.h.
368
369 bool Debug; // Enable debug messages
370 vtkTimeStamp MTime; // Keep track of modification time
371 vtkSubjectHelper* SubjectHelper; // List of observers on this object
372 std::string ObjectName; // Name of this object for reporting
373
375
383 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
386
387private:
388 vtkObject(const vtkObject&) = delete;
389 void operator=(const vtkObject&) = delete;
390
398 class vtkClassMemberCallbackBase
399 {
400 public:
402
405 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
406 virtual ~vtkClassMemberCallbackBase() = default;
408 };
409
411
415 template <class T>
416 class vtkClassMemberHandlerPointer
417 {
418 public:
419 void operator=(vtkObjectBase* o)
420 {
421 // The cast is needed in case "o" has multi-inheritance,
422 // to offset the pointer to get the vtkObjectBase.
423 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
424 {
425 // fallback to just using its vtkObjectBase as-is.
426 this->VoidPointer = o;
427 }
428 this->WeakPointer = o;
429 this->UseWeakPointer = true;
430 }
431 void operator=(void* o)
432 {
433 this->VoidPointer = o;
434 this->WeakPointer = nullptr;
435 this->UseWeakPointer = false;
436 }
437 T* GetPointer()
438 {
439 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
440 {
441 return nullptr;
442 }
443 return static_cast<T*>(this->VoidPointer);
444 }
445
446 private:
447 vtkWeakPointerBase WeakPointer;
448 void* VoidPointer;
449 bool UseWeakPointer;
450 };
452
454
457 template <class T>
458 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
459 {
460 vtkClassMemberHandlerPointer<T> Handler;
461 void (T::*Method1)();
462 void (T::*Method2)(vtkObject*, unsigned long, void*);
463 bool (T::*Method3)(vtkObject*, unsigned long, void*);
464
465 public:
466 vtkClassMemberCallback(T* handler, void (T::*method)())
467 {
468 this->Handler = handler;
469 this->Method1 = method;
470 this->Method2 = nullptr;
471 this->Method3 = nullptr;
472 }
473
474 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
475 {
476 this->Handler = handler;
477 this->Method1 = nullptr;
478 this->Method2 = method;
479 this->Method3 = nullptr;
480 }
481
482 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
483 {
484 this->Handler = handler;
485 this->Method1 = nullptr;
486 this->Method2 = nullptr;
487 this->Method3 = method;
488 }
489 ~vtkClassMemberCallback() override = default;
490
491 // Called when the event is invoked
492 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
493 {
494 T* handler = this->Handler.GetPointer();
495 if (handler)
496 {
497 if (this->Method1)
498 {
499 (handler->*this->Method1)();
500 }
501 else if (this->Method2)
502 {
503 (handler->*this->Method2)(caller, event, calldata);
504 }
505 else if (this->Method3)
506 {
507 return (handler->*this->Method3)(caller, event, calldata);
508 }
509 }
510 return false;
511 }
512 };
514
516
520 void ObjectFinalize() final;
522
524
527 unsigned long AddTemplatedObserver(
528 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
529 // Friend to access AddTemplatedObserver().
530 friend class vtkObjectCommandInternal;
532};
533
534VTK_ABI_NAMESPACE_END
535#endif
536// VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition vtkCommand.h:384
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
virtual void ObjectFinalize()
void operator=(const vtkObjectBase &)
abstract base class for most VTK objects
Definition vtkObject.h:162
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 RemoveObservers(const char *event)
vtkSubjectHelper * SubjectHelper
Definition vtkObject.h:371
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:321
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:305
vtkTypeBool InvokeEvent(unsigned long event)
Definition vtkObject.h:341
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:370
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:342
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:235
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:372
bool Debug
Definition vtkObject.h:369
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.
vtkTypeBool HasObserver(const char *event)
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:296
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
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)