VTK  9.1.0
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=========================================================================*/
60#ifndef vtkObject_h
61#define vtkObject_h
62
63#include "vtkCommonCoreModule.h" // For export macro
64#include "vtkObjectBase.h"
65#include "vtkSetGet.h"
66#include "vtkTimeStamp.h"
67#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
68
69class vtkSubjectHelper;
70class vtkCommand;
71
72class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
73{
74public:
76
81 static vtkObject* New();
82
83#ifdef _WIN32
84 // avoid dll boundary problems
85 void* operator new(size_t tSize);
86 void operator delete(void* p);
87#endif
88
92 virtual void DebugOn();
93
97 virtual void DebugOff();
98
102 bool GetDebug();
103
107 void SetDebug(bool debugFlag);
108
113 static void BreakOnError();
114
121 virtual void Modified();
122
127
134 void PrintSelf(ostream& os, vtkIndent indent) override;
135
137
141 static void SetGlobalWarningDisplay(int val);
146
148
160 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
161 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
162 vtkCommand* GetCommand(unsigned long tag);
164 void RemoveObservers(unsigned long event, vtkCommand*);
165 void RemoveObservers(const char* event, vtkCommand*);
166 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
167 vtkTypeBool HasObserver(const char* event, vtkCommand*);
169
170 void RemoveObserver(unsigned long tag);
171 void RemoveObservers(unsigned long event);
172 void RemoveObservers(const char* event);
173 void RemoveAllObservers(); // remove every last one of them
174 vtkTypeBool HasObserver(unsigned long event);
175 vtkTypeBool HasObserver(const char* event);
176
178
203 template <class U, class T>
204 unsigned long AddObserver(
205 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
206 {
207 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
208 // callable is deleted when the observer is cleaned up (look at
209 // vtkObjectCommandInternal)
210 return this->AddTemplatedObserver(event, callable, priority);
211 }
212 template <class U, class T>
213 unsigned long AddObserver(unsigned long event, U observer,
214 void (T::*callback)(vtkObject*, unsigned long, void*), 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 }
222
224
228 template <class U, class T>
229 unsigned long AddObserver(unsigned long event, U observer,
230 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
231 {
232 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
233 // callable is deleted when the observer is cleaned up (look at
234 // vtkObjectCommandInternal)
235 return this->AddTemplatedObserver(event, callable, priority);
236 }
238
240
245 int InvokeEvent(unsigned long event, void* callData);
246 int InvokeEvent(const char* event, void* callData);
248
249 int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
250 int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
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
265
273 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
276
277private:
278 vtkObject(const vtkObject&) = delete;
279 void operator=(const vtkObject&) = delete;
280
288 class vtkClassMemberCallbackBase
289 {
290 public:
292
295 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
296 virtual ~vtkClassMemberCallbackBase() = default;
298 };
299
301
305 template <class T>
306 class vtkClassMemberHandlerPointer
307 {
308 public:
309 void operator=(vtkObjectBase* o)
310 {
311 // The cast is needed in case "o" has multi-inheritance,
312 // to offset the pointer to get the vtkObjectBase.
313 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
314 {
315 // fallback to just using its vtkObjectBase as-is.
316 this->VoidPointer = o;
317 }
318 this->WeakPointer = o;
319 this->UseWeakPointer = true;
320 }
321 void operator=(void* o)
322 {
323 this->VoidPointer = o;
324 this->WeakPointer = nullptr;
325 this->UseWeakPointer = false;
326 }
327 T* GetPointer()
328 {
329 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
330 {
331 return nullptr;
332 }
333 return static_cast<T*>(this->VoidPointer);
334 }
335
336 private:
337 vtkWeakPointerBase WeakPointer;
338 void* VoidPointer;
339 bool UseWeakPointer;
340 };
342
344
347 template <class T>
348 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
349 {
350 vtkClassMemberHandlerPointer<T> Handler;
351 void (T::*Method1)();
352 void (T::*Method2)(vtkObject*, unsigned long, void*);
353 bool (T::*Method3)(vtkObject*, unsigned long, void*);
354
355 public:
356 vtkClassMemberCallback(T* handler, void (T::*method)())
357 {
358 this->Handler = handler;
359 this->Method1 = method;
360 this->Method2 = nullptr;
361 this->Method3 = nullptr;
362 }
363
364 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
365 {
366 this->Handler = handler;
367 this->Method1 = nullptr;
368 this->Method2 = method;
369 this->Method3 = nullptr;
370 }
371
372 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
373 {
374 this->Handler = handler;
375 this->Method1 = nullptr;
376 this->Method2 = nullptr;
377 this->Method3 = method;
378 }
379 ~vtkClassMemberCallback() override = default;
380
381 // Called when the event is invoked
382 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
383 {
384 T* handler = this->Handler.GetPointer();
385 if (handler)
386 {
387 if (this->Method1)
388 {
389 (handler->*this->Method1)();
390 }
391 else if (this->Method2)
392 {
393 (handler->*this->Method2)(caller, event, calldata);
394 }
395 else if (this->Method3)
396 {
397 return (handler->*this->Method3)(caller, event, calldata);
398 }
399 }
400 return false;
401 }
402 };
404
406
409 unsigned long AddTemplatedObserver(
410 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
411 // Friend to access AddTemplatedObserver().
412 friend class vtkObjectCommandInternal;
414};
415
416#endif
417// VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition: vtkCommand.h:394
a simple class to control print indentation
Definition: vtkIndent.h:113
abstract base class for most VTK objects
Definition: vtkObjectBase.h:70
void operator=(const vtkObjectBase &)
abstract base class for most VTK objects
Definition: vtkObject.h:73
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:262
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:229
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:213
~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.
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.
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:249
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:143
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.
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:260
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:142
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:250
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:204
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:52
Non-templated superclass for vtkWeakPointer.
@ priority
Definition: vtkX3D.h:456
int vtkTypeBool
Definition: vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287