VTK  9.2.6
vtkObject.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkObject.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
69#ifndef vtkObject_h
70#define vtkObject_h
71
72#include "vtkCommonCoreModule.h" // For export macro
73#include "vtkObjectBase.h"
74#include "vtkSetGet.h"
75#include "vtkTimeStamp.h"
76#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
77
78class vtkSubjectHelper;
79class vtkCommand;
80
81class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
82{
83public:
85
90 static vtkObject* New();
91
92#ifdef _WIN32
93 // avoid dll boundary problems
94 void* operator new(size_t tSize);
95 void operator delete(void* p);
96#endif
97
101 virtual void DebugOn();
102
106 virtual void DebugOff();
107
111 bool GetDebug();
112
116 void SetDebug(bool debugFlag);
117
122 static void BreakOnError();
123
130 virtual void Modified();
131
136
143 void PrintSelf(ostream& os, vtkIndent indent) override;
144
146
150 static void SetGlobalWarningDisplay(int val);
155
157
169 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
170 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
171 vtkCommand* GetCommand(unsigned long tag);
173 void RemoveObservers(unsigned long event, vtkCommand*);
174 void RemoveObservers(const char* event, vtkCommand*);
175 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
176 vtkTypeBool HasObserver(const char* event, vtkCommand*);
178
179 void RemoveObserver(unsigned long tag);
180 void RemoveObservers(unsigned long event);
181 void RemoveObservers(const char* event);
182 void RemoveAllObservers(); // remove every last one of them
183 vtkTypeBool HasObserver(unsigned long event);
184 vtkTypeBool HasObserver(const char* event);
185
187
212 template <class U, class T>
213 unsigned long AddObserver(
214 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
215 {
216 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
217 // callable is deleted when the observer is cleaned up (look at
218 // vtkObjectCommandInternal)
219 return this->AddTemplatedObserver(event, callable, priority);
220 }
221 template <class U, class T>
222 unsigned long AddObserver(unsigned long event, U observer,
223 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
224 {
225 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
226 // callable is deleted when the observer is cleaned up (look at
227 // vtkObjectCommandInternal)
228 return this->AddTemplatedObserver(event, callable, priority);
229 }
231
233
237 template <class U, class T>
238 unsigned long AddObserver(unsigned long event, U observer,
239 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
240 {
241 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
242 // callable is deleted when the observer is cleaned up (look at
243 // vtkObjectCommandInternal)
244 return this->AddTemplatedObserver(event, callable, priority);
245 }
247
249
254 int InvokeEvent(unsigned long event, void* callData);
255 int InvokeEvent(const char* event, void* callData);
257
258 int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
259 int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
260
262
268 virtual void SetObjectName(const std::string& objectName);
269 virtual std::string GetObjectName() const;
271
277
278protected:
280 ~vtkObject() override;
281
282 // See vtkObjectBase.h.
285
286 bool Debug; // Enable debug messages
287 vtkTimeStamp MTime; // Keep track of modification time
288 vtkSubjectHelper* SubjectHelper; // List of observers on this object
289 std::string ObjectName; // Name of this object for reporting
290
292
300 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
303
304private:
305 vtkObject(const vtkObject&) = delete;
306 void operator=(const vtkObject&) = delete;
307
315 class vtkClassMemberCallbackBase
316 {
317 public:
319
322 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
323 virtual ~vtkClassMemberCallbackBase() = default;
325 };
326
328
332 template <class T>
333 class vtkClassMemberHandlerPointer
334 {
335 public:
336 void operator=(vtkObjectBase* o)
337 {
338 // The cast is needed in case "o" has multi-inheritance,
339 // to offset the pointer to get the vtkObjectBase.
340 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
341 {
342 // fallback to just using its vtkObjectBase as-is.
343 this->VoidPointer = o;
344 }
345 this->WeakPointer = o;
346 this->UseWeakPointer = true;
347 }
348 void operator=(void* o)
349 {
350 this->VoidPointer = o;
351 this->WeakPointer = nullptr;
352 this->UseWeakPointer = false;
353 }
354 T* GetPointer()
355 {
356 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
357 {
358 return nullptr;
359 }
360 return static_cast<T*>(this->VoidPointer);
361 }
362
363 private:
364 vtkWeakPointerBase WeakPointer;
365 void* VoidPointer;
366 bool UseWeakPointer;
367 };
369
371
374 template <class T>
375 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
376 {
377 vtkClassMemberHandlerPointer<T> Handler;
378 void (T::*Method1)();
379 void (T::*Method2)(vtkObject*, unsigned long, void*);
380 bool (T::*Method3)(vtkObject*, unsigned long, void*);
381
382 public:
383 vtkClassMemberCallback(T* handler, void (T::*method)())
384 {
385 this->Handler = handler;
386 this->Method1 = method;
387 this->Method2 = nullptr;
388 this->Method3 = nullptr;
389 }
390
391 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
392 {
393 this->Handler = handler;
394 this->Method1 = nullptr;
395 this->Method2 = method;
396 this->Method3 = nullptr;
397 }
398
399 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
400 {
401 this->Handler = handler;
402 this->Method1 = nullptr;
403 this->Method2 = nullptr;
404 this->Method3 = method;
405 }
406 ~vtkClassMemberCallback() override = default;
407
408 // Called when the event is invoked
409 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
410 {
411 T* handler = this->Handler.GetPointer();
412 if (handler)
413 {
414 if (this->Method1)
415 {
416 (handler->*this->Method1)();
417 }
418 else if (this->Method2)
419 {
420 (handler->*this->Method2)(caller, event, calldata);
421 }
422 else if (this->Method3)
423 {
424 return (handler->*this->Method3)(caller, event, calldata);
425 }
426 }
427 return false;
428 }
429 };
431
433
437 void ObjectFinalize() final;
439
441
444 unsigned long AddTemplatedObserver(
445 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
446 // Friend to access AddTemplatedObserver().
447 friend class vtkObjectCommandInternal;
449};
450
451#endif
452// VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition: vtkCommand.h:395
a simple class to control print indentation
Definition: vtkIndent.h:119
abstract base class for most VTK objects
Definition: vtkObjectBase.h:74
virtual void ObjectFinalize()
void operator=(const vtkObjectBase &)
abstract base class for most VTK objects
Definition: vtkObject.h:82
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:288
std::string GetObjectDescription() const override
The object description printed in messages and PrintSelf output.
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:238
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:222
~vtkObject() override
vtkTimeStamp MTime
Definition: vtkObject.h:287
void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr)
These methods allow a command to exclusively grab all events.
static void SetGlobalWarningDisplay(int val)
This is a global flag that controls whether any debug, warning or error messages are displayed.
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.
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:258
void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check) override
void RemoveAllObservers()
void RegisterInternal(vtkObjectBase *, vtkTypeBool check) override
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:152
int InvokeEvent(const char *event, void *callData)
This method invokes an event and return whether the event was aborted or not.
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.
std::string ObjectName
Definition: vtkObject.h:289
int InvokeEvent(unsigned long event, void *callData)
This method invokes an event and return whether the event was aborted or not.
bool Debug
Definition: vtkObject.h:286
static int GetGlobalWarningDisplay()
This is a global flag that controls whether any debug, warning or error messages are displayed.
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:151
bool GetDebug()
Get the value of the debug flag.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
void RemoveObservers(unsigned long event)
int InvokeEvent(const char *event)
Definition: vtkObject.h:259
virtual void SetObjectName(const std::string &objectName)
Set/get the name of this object for reporting purposes.
void RemoveObserver(unsigned long tag)
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:213
vtkCommand * GetCommand(unsigned long tag)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
record modification and/or execution time
Definition: vtkTimeStamp.h:55
Non-templated superclass for vtkWeakPointer.
@ priority
Definition: vtkX3D.h:456
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287