VTK  9.5.2
vtkObject.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
34
35#ifndef vtkObject_h
36#define vtkObject_h
37
38#include "vtkCommonCoreModule.h" // For export macro
39#include "vtkObjectBase.h"
40#include "vtkSetGet.h"
41#include "vtkTimeStamp.h"
42#include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
43#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
44
45VTK_ABI_NAMESPACE_BEGIN
46class vtkSubjectHelper;
47class vtkCommand;
48
49class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObject : public vtkObjectBase
50{
51public:
53
58 static vtkObject* New();
59
60#ifdef _WIN32
61 // avoid dll boundary problems
62 void* operator new(size_t tSize);
63 void operator delete(void* p);
64#endif
65
69 virtual void DebugOn();
70
74 virtual void DebugOff();
75
80 bool GetDebug();
81
86 void SetDebug(bool debugFlag);
87
92 static void BreakOnError();
93
100 virtual void Modified();
101
107
114 void PrintSelf(ostream& os, vtkIndent indent) override;
115
117
126
128
140 unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
141 unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
142 vtkCommand* GetCommand(unsigned long tag);
144 void RemoveObservers(unsigned long event, vtkCommand*);
145 void RemoveObservers(const char* event, vtkCommand*);
146 vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
147 vtkTypeBool HasObserver(const char* event, vtkCommand*);
149
150 void RemoveObserver(unsigned long tag);
151 void RemoveObservers(unsigned long event);
152 void RemoveObservers(const char* event);
153 void RemoveAllObservers(); // remove every last one of them
154 vtkTypeBool HasObserver(unsigned long event);
155 vtkTypeBool HasObserver(const char* event);
156
158
183 template <class U, class T>
184 unsigned long AddObserver(
185 unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
186 {
187 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
188 // callable is deleted when the observer is cleaned up (look at
189 // vtkObjectCommandInternal)
190 return this->AddTemplatedObserver(event, callable, priority);
191 }
192 template <class U, class T>
193 unsigned long AddObserver(unsigned long event, U observer,
194 void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
195 {
196 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
197 // callable is deleted when the observer is cleaned up (look at
198 // vtkObjectCommandInternal)
199 return this->AddTemplatedObserver(event, callable, priority);
200 }
201
202
204
208 template <class U, class T>
209 unsigned long AddObserver(unsigned long event, U observer,
210 bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
211 {
212 vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
213 // callable is deleted when the observer is cleaned up (look at
214 // vtkObjectCommandInternal)
215 return this->AddTemplatedObserver(event, callable, priority);
216 }
217
218
220
225 vtkTypeBool InvokeEvent(unsigned long event, void* callData);
226 vtkTypeBool InvokeEvent(const char* event, void* callData);
228
229 vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
230 vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
231
233
239 virtual void SetObjectName(const std::string& objectName);
240 virtual std::string GetObjectName() const;
242
247 std::string GetObjectDescription() const override;
248
249protected:
251 ~vtkObject() override;
252
253 // See vtkObjectBase.h.
256
257 bool Debug; // Enable debug messages
258 vtkTimeStamp MTime; // Keep track of modification time
259 vtkSubjectHelper* SubjectHelper; // List of observers on this object
260 std::string ObjectName; // Name of this object for reporting
261
263
271 void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
274
275private:
276 vtkObject(const vtkObject&) = delete;
277 void operator=(const vtkObject&) = delete;
278
286 class vtkClassMemberCallbackBase
287 {
288 public:
290
293 virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
294 virtual ~vtkClassMemberCallbackBase() = default;
296 };
297
299
303 template <class T>
304 class vtkClassMemberHandlerPointer
305 {
306 public:
307 void operator=(vtkObjectBase* o)
308 {
309 // The cast is needed in case "o" has multi-inheritance,
310 // to offset the pointer to get the vtkObjectBase.
311 if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
312 {
313 // fallback to just using its vtkObjectBase as-is.
314 this->VoidPointer = o;
315 }
316 this->WeakPointer = o;
317 this->UseWeakPointer = true;
318 }
319 void operator=(void* o)
320 {
321 this->VoidPointer = o;
322 this->WeakPointer = nullptr;
323 this->UseWeakPointer = false;
324 }
325 T* GetPointer()
326 {
327 if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
328 {
329 return nullptr;
330 }
331 return static_cast<T*>(this->VoidPointer);
332 }
333
334 private:
335 vtkWeakPointerBase WeakPointer;
336 void* VoidPointer;
337 bool UseWeakPointer;
338 };
340
342
345 template <class T>
346 class vtkClassMemberCallback : public vtkClassMemberCallbackBase
347 {
348 vtkClassMemberHandlerPointer<T> Handler;
349 void (T::*Method1)();
350 void (T::*Method2)(vtkObject*, unsigned long, void*);
351 bool (T::*Method3)(vtkObject*, unsigned long, void*);
352
353 public:
354 vtkClassMemberCallback(T* handler, void (T::*method)())
355 {
356 this->Handler = handler;
357 this->Method1 = method;
358 this->Method2 = nullptr;
359 this->Method3 = nullptr;
360 }
361
362 vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
363 {
364 this->Handler = handler;
365 this->Method1 = nullptr;
366 this->Method2 = method;
367 this->Method3 = nullptr;
368 }
369
370 vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
371 {
372 this->Handler = handler;
373 this->Method1 = nullptr;
374 this->Method2 = nullptr;
375 this->Method3 = method;
376 }
377 ~vtkClassMemberCallback() override = default;
378
379 // Called when the event is invoked
380 bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
381 {
382 T* handler = this->Handler.GetPointer();
383 if (handler)
384 {
385 if (this->Method1)
386 {
387 (handler->*this->Method1)();
388 }
389 else if (this->Method2)
390 {
391 (handler->*this->Method2)(caller, event, calldata);
392 }
393 else if (this->Method3)
394 {
395 return (handler->*this->Method3)(caller, event, calldata);
396 }
397 }
398 return false;
399 }
400 };
402
404
408 void ObjectFinalize() final;
410
412
415 unsigned long AddTemplatedObserver(
416 unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
417 // Friend to access AddTemplatedObserver().
420};
421
422VTK_ABI_NAMESPACE_END
423#endif
424// VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition vtkCommand.h:384
a simple class to control print indentation
Definition vtkIndent.h:29
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:418
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:259
std::string GetObjectDescription() const override
The object description printed in messages and PrintSelf output.
static void SetGlobalWarningDisplay(vtkTypeBool val)
This is a global flag that controls whether any debug, warning or error messages are displayed.
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:209
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:193
vtkTypeBool InvokeEvent(unsigned long event)
Definition vtkObject.h:229
~vtkObject() override
vtkTimeStamp MTime
Definition vtkObject.h:258
void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr)
These methods allow a command to exclusively grab all events.
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.
void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check) override
void RemoveAllObservers()
void RegisterInternal(vtkObjectBase *, vtkTypeBool check) override
vtkTypeBool InvokeEvent(const char *event)
Definition vtkObject.h:230
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition vtkObject.h:123
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.
vtkTypeBool InvokeEvent(unsigned long event, void *callData)
This method invokes an event and return whether the event was aborted or not.
std::string ObjectName
Definition vtkObject.h:260
bool Debug
Definition vtkObject.h:257
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:122
bool GetDebug()
Get the value of the debug flag.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
void RemoveObservers(unsigned long event)
virtual void SetObjectName(const std::string &objectName)
Set/get the name of this object for reporting purposes.
void RemoveObserver(unsigned long tag)
static vtkTypeBool GetGlobalWarningDisplay()
This is a global flag that controls whether any debug, warning or error messages are displayed.
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:184
vtkCommand * GetCommand(unsigned long tag)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkTypeBool InvokeEvent(const char *event, void *callData)
This method invokes an event and return whether the event was aborted or not.
record modification and/or execution time
int vtkTypeBool
Definition vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:287
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)