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
35#ifndef vtkObject_h
36#define vtkObject_h
37
38#include "vtkCommonCoreModule.h" // For export macro
39#include "vtkObjectBase.h"
40#include "vtkSetGet.h"
41#include "vtkTimeStamp.h"
42#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
43
44VTK_ABI_NAMESPACE_BEGIN
45class vtkSubjectHelper;
46class vtkCommand;
47
48class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
49{
50public:
52
57 static vtkObject* New();
58
59#ifdef _WIN32
60 // avoid dll boundary problems
61 void* operator new(size_t tSize);
62 void operator delete(void* p);
63#endif
64
68 virtual void DebugOn();
69
73 virtual void DebugOff();
74
78 bool GetDebug();
79
83 void SetDebug(bool debugFlag);
84
89 static void BreakOnError();
90
97 virtual void Modified();
98
103
110 void PrintSelf(ostream& os, vtkIndent indent) override;
111
113
122
124
136 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
137 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
138 vtkCommand* GetCommand(unsigned long tag);
140 void RemoveObservers(unsigned long event, vtkCommand*);
141 void RemoveObservers(const char* event, vtkCommand*);
142 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
143 vtkTypeBool HasObserver(const char* event, vtkCommand*);
145
146 void RemoveObserver(unsigned long tag);
147 void RemoveObservers(unsigned long event);
148 void RemoveObservers(const char* event);
149 void RemoveAllObservers(); // remove every last one of them
150 vtkTypeBool HasObserver(unsigned long event);
151 vtkTypeBool HasObserver(const char* event);
152
154
179 template <class U, class T>
180 unsigned long AddObserver(
181 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
182 {
183 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
184 // callable is deleted when the observer is cleaned up (look at
185 // vtkObjectCommandInternal)
186 return this->AddTemplatedObserver(event, callable, priority);
187 }
188 template <class U, class T>
189 unsigned long AddObserver(unsigned long event, U observer,
190 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
191 {
192 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
193 // callable is deleted when the observer is cleaned up (look at
194 // vtkObjectCommandInternal)
195 return this->AddTemplatedObserver(event, callable, priority);
196 }
198
200
204 template <class U, class T>
205 unsigned long AddObserver(unsigned long event, U observer,
206 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
207 {
208 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
209 // callable is deleted when the observer is cleaned up (look at
210 // vtkObjectCommandInternal)
211 return this->AddTemplatedObserver(event, callable, priority);
212 }
214
216
221 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
222 vtkTypeBool InvokeEvent(const char* event, void* callData);
224
225 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
226 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
227
229
235 virtual void SetObjectName(const std::string& objectName);
236 virtual std::string GetObjectName() const;
238
243 std::string GetObjectDescription() const override;
244
245protected:
247 ~vtkObject() override;
248
249 // See vtkObjectBase.h.
252
253 bool Debug; // Enable debug messages
254 vtkTimeStamp MTime; // Keep track of modification time
255 vtkSubjectHelper* SubjectHelper; // List of observers on this object
256 std::string ObjectName; // Name of this object for reporting
257
259
267 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
270
271private:
272 vtkObject(const vtkObject&) = delete;
273 void operator=(const vtkObject&) = delete;
274
282 class vtkClassMemberCallbackBase
283 {
284 public:
286
289 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
290 virtual ~vtkClassMemberCallbackBase() = default;
292 };
293
295
299 template <class T>
300 class vtkClassMemberHandlerPointer
301 {
302 public:
303 void operator=(vtkObjectBase* o)
304 {
305 // The cast is needed in case "o" has multi-inheritance,
306 // to offset the pointer to get the vtkObjectBase.
307 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
308 {
309 // fallback to just using its vtkObjectBase as-is.
310 this->VoidPointer = o;
311 }
312 this->WeakPointer = o;
313 this->UseWeakPointer = true;
314 }
315 void operator=(void* o)
316 {
317 this->VoidPointer = o;
318 this->WeakPointer = nullptr;
319 this->UseWeakPointer = false;
320 }
321 T* GetPointer()
322 {
323 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
324 {
325 return nullptr;
326 }
327 return static_cast<T*>(this->VoidPointer);
328 }
329
330 private:
331 vtkWeakPointerBase WeakPointer;
332 void* VoidPointer;
333 bool UseWeakPointer;
334 };
336
338
341 template <class T>
342 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
343 {
344 vtkClassMemberHandlerPointer<T> Handler;
345 void (T::*Method1)();
346 void (T::*Method2)(vtkObject*, unsigned long, void*);
347 bool (T::*Method3)(vtkObject*, unsigned long, void*);
348
349 public:
350 vtkClassMemberCallback(T* handler, void (T::*method)())
351 {
352 this->Handler = handler;
353 this->Method1 = method;
354 this->Method2 = nullptr;
355 this->Method3 = nullptr;
356 }
357
358 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
359 {
360 this->Handler = handler;
361 this->Method1 = nullptr;
362 this->Method2 = method;
363 this->Method3 = nullptr;
364 }
365
366 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
367 {
368 this->Handler = handler;
369 this->Method1 = nullptr;
370 this->Method2 = nullptr;
371 this->Method3 = method;
372 }
373 ~vtkClassMemberCallback() override = default;
374
375 // Called when the event is invoked
376 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
377 {
378 T* handler = this->Handler.GetPointer();
379 if (handler)
380 {
381 if (this->Method1)
382 {
383 (handler->*this->Method1)();
384 }
385 else if (this->Method2)
386 {
387 (handler->*this->Method2)(caller, event, calldata);
388 }
389 else if (this->Method3)
390 {
391 return (handler->*this->Method3)(caller, event, calldata);
392 }
393 }
394 return false;
395 }
396 };
398
400
404 void ObjectFinalize() final;
406
408
411 unsigned long AddTemplatedObserver(
412 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
413 // Friend to access AddTemplatedObserver().
414 friend class vtkObjectCommandInternal;
416};
417
418VTK_ABI_NAMESPACE_END
419#endif
420// 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:49
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:255
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:205
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:189
vtkTypeBool InvokeEvent(unsigned long event)
Definition vtkObject.h:225
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:254
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:226
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:119
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:256
bool Debug
Definition vtkObject.h:253
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:118
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:180
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