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=========================================================================*/
66#ifndef vtkObject_h
67#define vtkObject_h
68
69#include "vtkCommonCoreModule.h" // For export macro
70#include "vtkObjectBase.h"
71#include "vtkSetGet.h"
72#include "vtkTimeStamp.h"
73#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
74
75class vtkSubjectHelper;
76class vtkCommand;
77
78class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
79{
80public:
82
87 static vtkObject* New();
88
89#ifdef _WIN32
90 // avoid dll boundary problems
91 void* operator new(size_t tSize);
92 void operator delete(void* p);
93#endif
94
98 virtual void DebugOn();
99
103 virtual void DebugOff();
104
108 bool GetDebug();
109
113 void SetDebug(bool debugFlag);
114
119 static void BreakOnError();
120
127 virtual void Modified();
128
133
140 void PrintSelf(ostream& os, vtkIndent indent) override;
141
143
147 static void SetGlobalWarningDisplay(int val);
152
154
166 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
167 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
168 vtkCommand* GetCommand(unsigned long tag);
170 void RemoveObservers(unsigned long event, vtkCommand*);
171 void RemoveObservers(const char* event, vtkCommand*);
172 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
173 vtkTypeBool HasObserver(const char* event, vtkCommand*);
175
176 void RemoveObserver(unsigned long tag);
177 void RemoveObservers(unsigned long event);
178 void RemoveObservers(const char* event);
179 void RemoveAllObservers(); // remove every last one of them
180 vtkTypeBool HasObserver(unsigned long event);
181 vtkTypeBool HasObserver(const char* event);
182
184
209 template <class U, class T>
210 unsigned long AddObserver(
211 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
212 {
213 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
214 // callable is deleted when the observer is cleaned up (look at
215 // vtkObjectCommandInternal)
216 return this->AddTemplatedObserver(event, callable, priority);
217 }
218 template <class U, class T>
219 unsigned long AddObserver(unsigned long event, U observer,
220 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
221 {
222 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
223 // callable is deleted when the observer is cleaned up (look at
224 // vtkObjectCommandInternal)
225 return this->AddTemplatedObserver(event, callable, priority);
226 }
228
230
234 template <class U, class T>
235 unsigned long AddObserver(unsigned long event, U observer,
236 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
237 {
238 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
239 // callable is deleted when the observer is cleaned up (look at
240 // vtkObjectCommandInternal)
241 return this->AddTemplatedObserver(event, callable, priority);
242 }
244
246
251 int InvokeEvent(unsigned long event, void* callData);
252 int InvokeEvent(const char* event, void* callData);
254
255 int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
256 int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
257
259
265 virtual void SetObjectName(const std::string& objectName);
266 virtual std::string GetObjectName() const;
268
274
275protected:
277 ~vtkObject() override;
278
279 // See vtkObjectBase.h.
282
283 bool Debug; // Enable debug messages
284 vtkTimeStamp MTime; // Keep track of modification time
285 vtkSubjectHelper* SubjectHelper; // List of observers on this object
286 std::string ObjectName; // Name of this object for reporting
287
289
297 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
300
301private:
302 vtkObject(const vtkObject&) = delete;
303 void operator=(const vtkObject&) = delete;
304
312 class vtkClassMemberCallbackBase
313 {
314 public:
316
319 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
320 virtual ~vtkClassMemberCallbackBase() = default;
322 };
323
325
329 template <class T>
330 class vtkClassMemberHandlerPointer
331 {
332 public:
333 void operator=(vtkObjectBase* o)
334 {
335 // The cast is needed in case "o" has multi-inheritance,
336 // to offset the pointer to get the vtkObjectBase.
337 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
338 {
339 // fallback to just using its vtkObjectBase as-is.
340 this->VoidPointer = o;
341 }
342 this->WeakPointer = o;
343 this->UseWeakPointer = true;
344 }
345 void operator=(void* o)
346 {
347 this->VoidPointer = o;
348 this->WeakPointer = nullptr;
349 this->UseWeakPointer = false;
350 }
351 T* GetPointer()
352 {
353 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
354 {
355 return nullptr;
356 }
357 return static_cast<T*>(this->VoidPointer);
358 }
359
360 private:
361 vtkWeakPointerBase WeakPointer;
362 void* VoidPointer;
363 bool UseWeakPointer;
364 };
366
368
371 template <class T>
372 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
373 {
374 vtkClassMemberHandlerPointer<T> Handler;
375 void (T::*Method1)();
376 void (T::*Method2)(vtkObject*, unsigned long, void*);
377 bool (T::*Method3)(vtkObject*, unsigned long, void*);
378
379 public:
380 vtkClassMemberCallback(T* handler, void (T::*method)())
381 {
382 this->Handler = handler;
383 this->Method1 = method;
384 this->Method2 = nullptr;
385 this->Method3 = nullptr;
386 }
387
388 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
389 {
390 this->Handler = handler;
391 this->Method1 = nullptr;
392 this->Method2 = method;
393 this->Method3 = nullptr;
394 }
395
396 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
397 {
398 this->Handler = handler;
399 this->Method1 = nullptr;
400 this->Method2 = nullptr;
401 this->Method3 = method;
402 }
403 ~vtkClassMemberCallback() override = default;
404
405 // Called when the event is invoked
406 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
407 {
408 T* handler = this->Handler.GetPointer();
409 if (handler)
410 {
411 if (this->Method1)
412 {
413 (handler->*this->Method1)();
414 }
415 else if (this->Method2)
416 {
417 (handler->*this->Method2)(caller, event, calldata);
418 }
419 else if (this->Method3)
420 {
421 return (handler->*this->Method3)(caller, event, calldata);
422 }
423 }
424 return false;
425 }
426 };
428
430
434 void ObjectFinalize() final;
436
438
441 unsigned long AddTemplatedObserver(
442 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
443 // Friend to access AddTemplatedObserver().
444 friend class vtkObjectCommandInternal;
446};
447
448#endif
449// 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:79
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:285
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:235
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:219
~vtkObject() override
vtkTimeStamp MTime
Definition: vtkObject.h:284
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:255
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:149
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:286
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:283
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:148
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:256
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:210
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