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=========================================================================*/
43
44#ifndef vtkObject_h
45#define vtkObject_h
46
47#include "vtkCommonCoreModule.h" // For export macro
48#include "vtkObjectBase.h"
49#include "vtkSetGet.h"
50#include "vtkTimeStamp.h"
51#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
52
53class vtkSubjectHelper;
54class vtkCommand;
55
56class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
57{
58public:
60
65 static vtkObject* New();
66
67#ifdef _WIN32
68 // avoid dll boundary problems
69 void* operator new(size_t tSize);
70 void operator delete(void* p);
71#endif
72
76 virtual void DebugOn();
77
81 virtual void DebugOff();
82
86 bool GetDebug();
87
91 void SetDebug(bool debugFlag);
92
97 static void BreakOnError();
98
105 virtual void Modified();
106
111
118 void PrintSelf(ostream& os, vtkIndent indent) override;
119
121
125 static void SetGlobalWarningDisplay(int val);
130
132
144 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
145 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
146 vtkCommand* GetCommand(unsigned long tag);
148 void RemoveObservers(unsigned long event, vtkCommand*);
149 void RemoveObservers(const char* event, vtkCommand*);
150 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
151 vtkTypeBool HasObserver(const char* event, vtkCommand*);
153
154 void RemoveObserver(unsigned long tag);
155 void RemoveObservers(unsigned long event);
156 void RemoveObservers(const char* event);
157 void RemoveAllObservers(); // remove every last one of them
158 vtkTypeBool HasObserver(unsigned long event);
159 vtkTypeBool HasObserver(const char* event);
160
162
187 template <class U, class T>
188 unsigned long AddObserver(
189 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
190 {
191 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
192 // callable is deleted when the observer is cleaned up (look at
193 // vtkObjectCommandInternal)
194 return this->AddTemplatedObserver(event, callable, priority);
195 }
196 template <class U, class T>
197 unsigned long AddObserver(unsigned long event, U observer,
198 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
199 {
200 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
201 // callable is deleted when the observer is cleaned up (look at
202 // vtkObjectCommandInternal)
203 return this->AddTemplatedObserver(event, callable, priority);
204 }
205
206
208
212 template <class U, class T>
213 unsigned long AddObserver(unsigned long event, U observer,
214 bool (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 }
221
222
224
229 int InvokeEvent(unsigned long event, void* callData);
230 int InvokeEvent(const char* event, void* callData);
232
233 int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
234 int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
235
237
243 virtual void SetObjectName(const std::string& objectName);
244 virtual std::string GetObjectName() const;
246
251 std::string GetObjectDescription() const override;
252
253protected:
255 ~vtkObject() override;
256
257 // See vtkObjectBase.h.
260
261 bool Debug; // Enable debug messages
262 vtkTimeStamp MTime; // Keep track of modification time
263 vtkSubjectHelper* SubjectHelper; // List of observers on this object
264 std::string ObjectName; // Name of this object for reporting
265
267
275 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
278
279private:
280 vtkObject(const vtkObject&) = delete;
281 void operator=(const vtkObject&) = delete;
282
290 class vtkClassMemberCallbackBase
291 {
292 public:
294
297 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
298 virtual ~vtkClassMemberCallbackBase() = default;
300 };
301
303
307 template <class T>
308 class vtkClassMemberHandlerPointer
309 {
310 public:
311 void operator=(vtkObjectBase* o)
312 {
313 // The cast is needed in case "o" has multi-inheritance,
314 // to offset the pointer to get the vtkObjectBase.
315 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
316 {
317 // fallback to just using its vtkObjectBase as-is.
318 this->VoidPointer = o;
319 }
320 this->WeakPointer = o;
321 this->UseWeakPointer = true;
322 }
323 void operator=(void* o)
324 {
325 this->VoidPointer = o;
326 this->WeakPointer = nullptr;
327 this->UseWeakPointer = false;
328 }
329 T* GetPointer()
330 {
331 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
332 {
333 return nullptr;
334 }
335 return static_cast<T*>(this->VoidPointer);
336 }
337
338 private:
339 vtkWeakPointerBase WeakPointer;
340 void* VoidPointer;
341 bool UseWeakPointer;
342 };
344
346
349 template <class T>
350 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
351 {
352 vtkClassMemberHandlerPointer<T> Handler;
353 void (T::*Method1)();
354 void (T::*Method2)(vtkObject*, unsigned long, void*);
355 bool (T::*Method3)(vtkObject*, unsigned long, void*);
356
357 public:
358 vtkClassMemberCallback(T* handler, void (T::*method)())
359 {
360 this->Handler = handler;
361 this->Method1 = method;
362 this->Method2 = nullptr;
363 this->Method3 = nullptr;
364 }
365
366 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
367 {
368 this->Handler = handler;
369 this->Method1 = nullptr;
370 this->Method2 = method;
371 this->Method3 = nullptr;
372 }
373
374 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
375 {
376 this->Handler = handler;
377 this->Method1 = nullptr;
378 this->Method2 = nullptr;
379 this->Method3 = method;
380 }
381 ~vtkClassMemberCallback() override = default;
382
383 // Called when the event is invoked
384 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
385 {
386 T* handler = this->Handler.GetPointer();
387 if (handler)
388 {
389 if (this->Method1)
390 {
391 (handler->*this->Method1)();
392 }
393 else if (this->Method2)
394 {
395 (handler->*this->Method2)(caller, event, calldata);
396 }
397 else if (this->Method3)
398 {
399 return (handler->*this->Method3)(caller, event, calldata);
400 }
401 }
402 return false;
403 }
404 };
406
408
412 void ObjectFinalize() final;
414
416
419 unsigned long AddTemplatedObserver(
420 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
421 // Friend to access AddTemplatedObserver().
424};
425
426#endif
427// VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition vtkCommand.h:395
a simple class to control print indentation
Definition vtkIndent.h:34
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:422
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:263
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:213
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:197
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:262
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:233
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:127
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:264
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:261
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:126
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:234
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:188
vtkCommand * GetCommand(unsigned long tag)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
record modification and/or execution time
int vtkTypeBool
Definition vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:287