VTK  9.5.2
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
143
144#ifndef vtkObject_h
145#define vtkObject_h
146
147#include "vtkCommonCoreModule.h" // For export macro
148#include "vtkObjectBase.h"
149#include "vtkSetGet.h"
150#include "vtkTimeStamp.h"
151#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
152#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
153
154VTK_ABI_NAMESPACE_BEGIN
155class vtkSubjectHelper;
156class vtkCommand;
157
158class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObject : public vtkObjectBase
159{
160public:
162
167 static vtkObject* New();
168
169#ifdef _WIN32
170 // avoid dll boundary problems
171 void* operator new(size_t tSize);
172 void operator delete(void* p);
173#endif
174
178 virtual void DebugOn();
179
183 virtual void DebugOff();
184
189 bool GetDebug();
190
195 void SetDebug(bool debugFlag);
196
201 static void BreakOnError();
202
209 virtual void Modified();
210
216
223 void PrintSelf(ostream& os, vtkIndent indent) override;
224
226
235
237
249 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
250 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
251 vtkCommand* GetCommand(unsigned long tag);
253 void RemoveObservers(unsigned long event, vtkCommand*);
254 void RemoveObservers(const char* event, vtkCommand*);
255 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
256 vtkTypeBool HasObserver(const char* event, vtkCommand*);
258
259 void RemoveObserver(unsigned long tag);
260 void RemoveObservers(unsigned long event);
261 void RemoveObservers(const char* event);
262 void RemoveAllObservers(); // remove every last one of them
263 vtkTypeBool HasObserver(unsigned long event);
264 vtkTypeBool HasObserver(const char* event);
265
267
292 template <class U, class T>
293 unsigned long AddObserver(
294 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
295 {
296 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
297 // callable is deleted when the observer is cleaned up (look at
298 // vtkObjectCommandInternal)
299 return this->AddTemplatedObserver(event, callable, priority);
300 }
301 template <class U, class T>
302 unsigned long AddObserver(unsigned long event, U observer,
303 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
304 {
305 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
306 // callable is deleted when the observer is cleaned up (look at
307 // vtkObjectCommandInternal)
308 return this->AddTemplatedObserver(event, callable, priority);
309 }
310
311
313
317 template <class U, class T>
318 unsigned long AddObserver(unsigned long event, U observer,
319 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
320 {
321 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
322 // callable is deleted when the observer is cleaned up (look at
323 // vtkObjectCommandInternal)
324 return this->AddTemplatedObserver(event, callable, priority);
325 }
326
327
329
334 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
335 vtkTypeBool InvokeEvent(const char* event, void* callData);
337
338 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
339 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
340
342
348 virtual void SetObjectName(const std::string& objectName);
349 virtual std::string GetObjectName() const;
351
356 std::string GetObjectDescription() const override;
357
358protected:
360 ~vtkObject() override;
361
362 // See vtkObjectBase.h.
365
366 bool Debug; // Enable debug messages
367 vtkTimeStamp MTime; // Keep track of modification time
368 vtkSubjectHelper* SubjectHelper; // List of observers on this object
369 std::string ObjectName; // Name of this object for reporting
370
372
380 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
383
384private:
385 vtkObject(const vtkObject&) = delete;
386 void operator=(const vtkObject&) = delete;
387
395 class vtkClassMemberCallbackBase
396 {
397 public:
399
402 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
403 virtual ~vtkClassMemberCallbackBase() = default;
405 };
406
408
412 template <class T>
413 class vtkClassMemberHandlerPointer
414 {
415 public:
416 void operator=(vtkObjectBase* o)
417 {
418 // The cast is needed in case "o" has multi-inheritance,
419 // to offset the pointer to get the vtkObjectBase.
420 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
421 {
422 // fallback to just using its vtkObjectBase as-is.
423 this->VoidPointer = o;
424 }
425 this->WeakPointer = o;
426 this->UseWeakPointer = true;
427 }
428 void operator=(void* o)
429 {
430 this->VoidPointer = o;
431 this->WeakPointer = nullptr;
432 this->UseWeakPointer = false;
433 }
434 T* GetPointer()
435 {
436 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
437 {
438 return nullptr;
439 }
440 return static_cast<T*>(this->VoidPointer);
441 }
442
443 private:
444 vtkWeakPointerBase WeakPointer;
445 void* VoidPointer;
446 bool UseWeakPointer;
447 };
449
451
454 template <class T>
455 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
456 {
457 vtkClassMemberHandlerPointer<T> Handler;
458 void (T::*Method1)();
459 void (T::*Method2)(vtkObject*, unsigned long, void*);
460 bool (T::*Method3)(vtkObject*, unsigned long, void*);
461
462 public:
463 vtkClassMemberCallback(T* handler, void (T::*method)())
464 {
465 this->Handler = handler;
466 this->Method1 = method;
467 this->Method2 = nullptr;
468 this->Method3 = nullptr;
469 }
470
471 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
472 {
473 this->Handler = handler;
474 this->Method1 = nullptr;
475 this->Method2 = method;
476 this->Method3 = nullptr;
477 }
478
479 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
480 {
481 this->Handler = handler;
482 this->Method1 = nullptr;
483 this->Method2 = nullptr;
484 this->Method3 = method;
485 }
486 ~vtkClassMemberCallback() override = default;
487
488 // Called when the event is invoked
489 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
490 {
491 T* handler = this->Handler.GetPointer();
492 if (handler)
493 {
494 if (this->Method1)
495 {
496 (handler->*this->Method1)();
497 }
498 else if (this->Method2)
499 {
500 (handler->*this->Method2)(caller, event, calldata);
501 }
502 else if (this->Method3)
503 {
504 return (handler->*this->Method3)(caller, event, calldata);
505 }
506 }
507 return false;
508 }
509 };
511
513
517 void ObjectFinalize() final;
519
521
524 unsigned long AddTemplatedObserver(
525 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
526 // Friend to access AddTemplatedObserver().
529};
530
531VTK_ABI_NAMESPACE_END
532#endif
533// 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 &)
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)
friend class vtkObjectCommandInternal
Called by templated variants of AddObserver.
Definition vtkObject.h:527
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:368
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:318
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:302
vtkTypeBool InvokeEvent(unsigned long event)
Definition vtkObject.h:338
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:367
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:339
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:232
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:369
bool Debug
Definition vtkObject.h:366
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:231
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:293
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
int vtkTypeBool
Definition vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:287
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)