VTK  9.6.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
37
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#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
47
48VTK_ABI_NAMESPACE_BEGIN
49class vtkSubjectHelper;
50class vtkCommand;
51
52class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObject : public vtkObjectBase
53{
54public:
56
61 static vtkObject* New();
62
63#ifdef _WIN32
64 // avoid dll boundary problems
65 void* operator new(size_t tSize);
66 void operator delete(void* p);
67#endif
68
72 virtual void DebugOn();
73
77 virtual void DebugOff();
78
83 bool GetDebug();
84
89 void SetDebug(bool debugFlag);
90
95 static void BreakOnError();
96
103 virtual void Modified();
104
110
117 void PrintSelf(ostream& os, vtkIndent indent) override;
118
120
129
131
143 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
144 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
145 vtkCommand* GetCommand(unsigned long tag);
147 void RemoveObservers(unsigned long event, vtkCommand*);
148 void RemoveObservers(const char* event, vtkCommand*);
149 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
150 vtkTypeBool HasObserver(const char* event, vtkCommand*);
152
153 void RemoveObserver(unsigned long tag);
154 void RemoveObservers(unsigned long event);
155 void RemoveObservers(const char* event);
156 void RemoveAllObservers(); // remove every last one of them
157 vtkTypeBool HasObserver(unsigned long event);
158 vtkTypeBool HasObserver(const char* event);
159
161
186 template <class U, class T>
187 unsigned long AddObserver(
188 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
189 {
190 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
191 // callable is deleted when the observer is cleaned up (look at
192 // vtkObjectCommandInternal)
193 return this->AddTemplatedObserver(event, callable, priority);
194 }
195 template <class U, class T>
196 unsigned long AddObserver(unsigned long event, U observer,
197 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
198 {
199 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
200 // callable is deleted when the observer is cleaned up (look at
201 // vtkObjectCommandInternal)
202 return this->AddTemplatedObserver(event, callable, priority);
203 }
204
205
207
211 template <class U, class T>
212 unsigned long AddObserver(unsigned long event, U observer,
213 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
214 {
215 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
216 // callable is deleted when the observer is cleaned up (look at
217 // vtkObjectCommandInternal)
218 return this->AddTemplatedObserver(event, callable, priority);
219 }
220
221
223
228 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
229 vtkTypeBool InvokeEvent(const char* event, void* callData);
231
232 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
233 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
234
236
242 virtual void SetObjectName(const std::string& objectName);
243 virtual std::string GetObjectName() const;
245
250 std::string GetObjectDescription() const override;
251
252protected:
254 ~vtkObject() override;
255
256 // See vtkObjectBase.h.
259
260 bool Debug; // Enable debug messages
261 vtkTimeStamp MTime; // Keep track of modification time
262 vtkSubjectHelper* SubjectHelper; // List of observers on this object
263 std::string ObjectName; // Name of this object for reporting
264
266
274 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
277
278private:
279 vtkObject(const vtkObject&) = delete;
280 void operator=(const vtkObject&) = delete;
281
289 class vtkClassMemberCallbackBase
290 {
291 public:
293
296 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
297 virtual ~vtkClassMemberCallbackBase() = default;
299 };
300
302
306 template <class T>
307 class vtkClassMemberHandlerPointer
308 {
309 public:
310 void operator=(vtkObjectBase* o)
311 {
312 // The cast is needed in case "o" has multi-inheritance,
313 // to offset the pointer to get the vtkObjectBase.
314 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
315 {
316 // fallback to just using its vtkObjectBase as-is.
317 this->VoidPointer = o;
318 }
319 this->WeakPointer = o;
320 this->UseWeakPointer = true;
321 }
322 void operator=(void* o)
323 {
324 this->VoidPointer = o;
325 this->WeakPointer = nullptr;
326 this->UseWeakPointer = false;
327 }
328 T* GetPointer()
329 {
330 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
331 {
332 return nullptr;
333 }
334 return static_cast<T*>(this->VoidPointer);
335 }
336
337 private:
338 vtkWeakPointerBase WeakPointer;
339 void* VoidPointer;
340 bool UseWeakPointer;
341 };
343
345
348 template <class T>
349 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
350 {
351 vtkClassMemberHandlerPointer<T> Handler;
352 void (T::*Method1)();
353 void (T::*Method2)(vtkObject*, unsigned long, void*);
354 bool (T::*Method3)(vtkObject*, unsigned long, void*);
355
356 public:
357 vtkClassMemberCallback(T* handler, void (T::*method)())
358 {
359 this->Handler = handler;
360 this->Method1 = method;
361 this->Method2 = nullptr;
362 this->Method3 = nullptr;
363 }
364
365 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
366 {
367 this->Handler = handler;
368 this->Method1 = nullptr;
369 this->Method2 = method;
370 this->Method3 = nullptr;
371 }
372
373 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
374 {
375 this->Handler = handler;
376 this->Method1 = nullptr;
377 this->Method2 = nullptr;
378 this->Method3 = method;
379 }
380 ~vtkClassMemberCallback() override = default;
381
382 // Called when the event is invoked
383 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
384 {
385 T* handler = this->Handler.GetPointer();
386 if (handler)
387 {
388 if (this->Method1)
389 {
390 (handler->*this->Method1)();
391 }
392 else if (this->Method2)
393 {
394 (handler->*this->Method2)(caller, event, calldata);
395 }
396 else if (this->Method3)
397 {
398 return (handler->*this->Method3)(caller, event, calldata);
399 }
400 }
401 return false;
402 }
403 };
405
407
411 void ObjectFinalize() final;
413
415
418 unsigned long AddTemplatedObserver(
419 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
420 // Friend to access AddTemplatedObserver().
423};
424
425VTK_ABI_NAMESPACE_END
426#endif
427// 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 &)
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:421
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:262
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:212
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:196
vtkTypeBool InvokeEvent(unsigned long event)
Definition vtkObject.h:232
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:261
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:233
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:126
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:263
bool Debug
Definition vtkObject.h:260
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:125
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:187
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:323
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)