vtkMemberFunctionCommand.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
3 // SPDX-License-Identifier: BSD-3-Clause
43 #ifndef vtkMemberFunctionCommand_h
44 #define vtkMemberFunctionCommand_h
45 
46 #include "vtkCommand.h"
47 
48 template <class ClassT>
50 {
52 
53 public:
55 
56  const char* GetClassNameInternal() const override { return "vtkMemberFunctionCommand"; }
57 
58  static ThisT* SafeDownCast(vtkObjectBase* o) { return dynamic_cast<ThisT*>(o); }
59 
60  static ThisT* New() { return new ThisT(); }
61 
62  void PrintSelf(ostream& os, vtkIndent indent) override
63  {
64  this->Superclass::PrintSelf(os, indent);
65  }
66 
68 
72  void SetCallback(ClassT& object, void (ClassT::*method)())
73  {
74  this->Object = &object;
75  this->Method = method;
76  }
78 
79  void SetCallback(ClassT& object, void (ClassT::*method2)(vtkObject*, unsigned long, void*))
80  {
81  this->Object = &object;
82  this->Method2 = method2;
83  }
84 
85  void Execute(vtkObject* caller, unsigned long event, void* calldata) override
86  {
87  if (this->Object && this->Method)
88  {
89  (this->Object->*this->Method)();
90  }
91  if (this->Object && this->Method2)
92  {
93  (this->Object->*this->Method2)(caller, event, calldata);
94  }
95  }
96  void Reset()
97  {
98  this->Object = nullptr;
99  this->Method2 = nullptr;
100  this->Method = nullptr;
101  }
102 
103 private:
105  {
106  this->Object = nullptr;
107  this->Method = nullptr;
108  this->Method2 = nullptr;
109  }
110 
111  ~vtkMemberFunctionCommand() override = default;
112 
113  ClassT* Object;
114  void (ClassT::*Method)();
115  void (ClassT::*Method2)(vtkObject* caller, unsigned long event, void* calldata);
116 
118  void operator=(const vtkMemberFunctionCommand&) = delete;
119 };
120 
136 template <class ClassT>
138  ClassT& object, void (ClassT::*method)())
139 {
141  result->SetCallback(object, method);
142  return result;
143 }
144 
145 template <class ClassT>
147  ClassT& object, void (ClassT::*method)(vtkObject*, unsigned long, void*))
148 {
150  result->SetCallback(object, method);
151  return result;
152 }
153 
154 #endif
155 //-----------------------------------------------------------------------------
156 // VTK-HeaderTest-Exclude: vtkMemberFunctionCommand.h
void SetCallback(ClassT &object, void(ClassT::*method)())
Set which class instance and member function will be called when a VTK event is received.
static ThisT * SafeDownCast(vtkObjectBase *o)
const char * GetClassNameInternal() const override
void SetCallback(ClassT &object, void(ClassT::*method2)(vtkObject *, unsigned long, void *))
void PrintSelf(ostream &os, vtkIndent indent) override
virtual void PrintSelf(ostream &os, vtkIndent indent)
vtkMemberFunctionCommand< ClassT > * vtkMakeMemberFunctionCommand(ClassT &object, void(ClassT::*method)())
Convenience function for creating vtkMemberFunctionCommand instances that automatically deduces its a...
void Execute(vtkObject *caller, unsigned long event, void *calldata) override
Call a class member method in response to a VTK event.