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 =========================================================================*/
50 #ifndef vtkObject_h
51 #define vtkObject_h
52 
53 #include "vtkCommonCoreModule.h" // For export macro
54 #include "vtkObjectBase.h"
55 #include "vtkSetGet.h"
56 #include "vtkTimeStamp.h"
57 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
58 
59 class vtkSubjectHelper;
60 class vtkCommand;
61 
62 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
63 {
64 public:
65  vtkBaseTypeMacro(vtkObject, vtkObjectBase);
66 
71  static vtkObject* New();
72 
73 #ifdef _WIN32
74  // avoid dll boundary problems
75  void* operator new(size_t tSize);
76  void operator delete(void* p);
77 #endif
78 
82  virtual void DebugOn();
83 
87  virtual void DebugOff();
88 
92  bool GetDebug();
93 
97  void SetDebug(bool debugFlag);
98 
103  static void BreakOnError();
104 
111  virtual void Modified();
112 
116  virtual vtkMTimeType GetMTime();
117 
124  void PrintSelf(ostream& os, vtkIndent indent) override;
125 
127 
131  static void SetGlobalWarningDisplay(int val);
134  static int GetGlobalWarningDisplay();
136 
138 
150  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
151  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
152  vtkCommand* GetCommand(unsigned long tag);
153  void RemoveObserver(vtkCommand*);
154  void RemoveObservers(unsigned long event, vtkCommand*);
155  void RemoveObservers(const char* event, vtkCommand*);
156  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
157  vtkTypeBool HasObserver(const char* event, vtkCommand*);
159 
160  void RemoveObserver(unsigned long tag);
161  void RemoveObservers(unsigned long event);
162  void RemoveObservers(const char* event);
163  void RemoveAllObservers(); // remove every last one of them
164  vtkTypeBool HasObserver(unsigned long event);
165  vtkTypeBool HasObserver(const char* event);
166 
168 
193  template <class U, class T>
194  unsigned long AddObserver(
195  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
196  {
197  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
198  // callable is deleted when the observer is cleaned up (look at
199  // vtkObjectCommandInternal)
200  return this->AddTemplatedObserver(event, callable, priority);
201  }
202  template <class U, class T>
203  unsigned long AddObserver(unsigned long event, U observer,
204  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
205  {
206  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
207  // callable is deleted when the observer is cleaned up (look at
208  // vtkObjectCommandInternal)
209  return this->AddTemplatedObserver(event, callable, priority);
210  }
212 
214 
218  template <class U, class T>
219  unsigned long AddObserver(unsigned long event, U observer,
220  bool (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 
235  int InvokeEvent(unsigned long event, void* callData);
236  int InvokeEvent(const char* event, void* callData);
238 
239  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
240  int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
241 
243 
249  virtual void SetObjectName(const std::string& objectName);
250  virtual std::string GetObjectName() const;
252 
257  std::string GetObjectDescription() const override;
258 
259 protected:
260  vtkObject();
261  ~vtkObject() override;
262 
263  // See vtkObjectBase.h.
264  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
265  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
266 
267  bool Debug; // Enable debug messages
268  vtkTimeStamp MTime; // Keep track of modification time
269  vtkSubjectHelper* SubjectHelper; // List of observers on this object
270  std::string ObjectName; // Name of this object for reporting
271 
273 
281  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
282  void InternalReleaseFocus();
284 
285 private:
286  vtkObject(const vtkObject&) = delete;
287  void operator=(const vtkObject&) = delete;
288 
296  class vtkClassMemberCallbackBase
297  {
298  public:
300 
303  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
304  virtual ~vtkClassMemberCallbackBase() = default;
306  };
307 
309 
313  template <class T>
314  class vtkClassMemberHandlerPointer
315  {
316  public:
317  void operator=(vtkObjectBase* o)
318  {
319  // The cast is needed in case "o" has multi-inheritance,
320  // to offset the pointer to get the vtkObjectBase.
321  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
322  {
323  // fallback to just using its vtkObjectBase as-is.
324  this->VoidPointer = o;
325  }
326  this->WeakPointer = o;
327  this->UseWeakPointer = true;
328  }
329  void operator=(void* o)
330  {
331  this->VoidPointer = o;
332  this->WeakPointer = nullptr;
333  this->UseWeakPointer = false;
334  }
335  T* GetPointer()
336  {
337  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
338  {
339  return nullptr;
340  }
341  return static_cast<T*>(this->VoidPointer);
342  }
343 
344  private:
345  vtkWeakPointerBase WeakPointer;
346  void* VoidPointer;
347  bool UseWeakPointer;
348  };
350 
352 
355  template <class T>
356  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
357  {
358  vtkClassMemberHandlerPointer<T> Handler;
359  void (T::*Method1)();
360  void (T::*Method2)(vtkObject*, unsigned long, void*);
361  bool (T::*Method3)(vtkObject*, unsigned long, void*);
362 
363  public:
364  vtkClassMemberCallback(T* handler, void (T::*method)())
365  {
366  this->Handler = handler;
367  this->Method1 = method;
368  this->Method2 = nullptr;
369  this->Method3 = nullptr;
370  }
371 
372  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
373  {
374  this->Handler = handler;
375  this->Method1 = nullptr;
376  this->Method2 = method;
377  this->Method3 = nullptr;
378  }
379 
380  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
381  {
382  this->Handler = handler;
383  this->Method1 = nullptr;
384  this->Method2 = nullptr;
385  this->Method3 = method;
386  }
387  ~vtkClassMemberCallback() override = default;
388 
389  // Called when the event is invoked
390  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
391  {
392  T* handler = this->Handler.GetPointer();
393  if (handler)
394  {
395  if (this->Method1)
396  {
397  (handler->*this->Method1)();
398  }
399  else if (this->Method2)
400  {
401  (handler->*this->Method2)(caller, event, calldata);
402  }
403  else if (this->Method3)
404  {
405  return (handler->*this->Method3)(caller, event, calldata);
406  }
407  }
408  return false;
409  }
410  };
412 
414 
418  void ObjectFinalize() final;
420 
422 
425  unsigned long AddTemplatedObserver(
426  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
427  // Friend to access AddTemplatedObserver().
428  friend class vtkObjectCommandInternal;
430 };
431 
432 #endif
433 // VTK-HeaderTest-Exclude: vtkObject.h
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:203
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
abstract base class for most VTK objects
Definition: vtkObject.h:62
virtual void ObjectFinalize()
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
record modification and/or execution time
Definition: vtkTimeStamp.h:35
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:239
virtual std::string GetObjectDescription() const
The object description printed in messages and PrintSelf output.
virtual void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:133
std::string ObjectName
Definition: vtkObject.h:270
int vtkTypeBool
Definition: vtkABI.h:69
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:219
superclass for callback/observer methods
Definition: vtkCommand.h:394
static void SetGlobalWarningDisplay(int val)
This is a global flag that controls whether any debug, warning or error messages are displayed...
a simple class to control print indentation
Definition: vtkIndent.h:39
virtual void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check)
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:73
virtual void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
int InvokeEvent(const char *event)
Definition: vtkObject.h:240
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:194
bool Debug
Definition: vtkObject.h:267
void operator=(const vtkObjectBase &)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:269
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:132
vtkTimeStamp MTime
Definition: vtkObject.h:268