Slicer  5.1
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkEventBroker.h
Go to the documentation of this file.
1 /*=auto=========================================================================
2 
3  Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
4 
5  See COPYRIGHT.txt
6  or http://www.slicer.org/copyright/copyright.txt for details.
7 
8  Program: 3D Slicer
9  Module: $RCSfile: vtkEventBroker.h,v $
10  Date: $Date: 2006/01/08 04:48:05 $
11  Version: $Revision: 1.45 $
12 
13 =========================================================================auto=*/
14 
15 #ifndef __vtkEventBroker_h
16 #define __vtkEventBroker_h
17 
18 // MRML includes
19 #include "vtkMRML.h"
20 
21 // VTK includes
22 #include <vtkObject.h>
23 class vtkTimerLog;
24 
25 // STD includes
26 #include <deque>
27 #include <vector>
28 #include <set>
29 #include <map>
30 #include <fstream>
31 
32 class vtkCollection;
33 class vtkCallbackCommand;
34 class vtkObservation;
35 
44 //
49 class VTK_MRML_EXPORT vtkEventBroker : public vtkObject
50 {
51 public:
52  vtkTypeMacro(vtkEventBroker, vtkObject);
53  void PrintSelf(ostream& os, vtkIndent indent) override;
54 
55  typedef std::set< vtkObservation * > ObservationVector;
56 
59  static vtkEventBroker* GetInstance();
60 
67  static vtkEventBroker* New();
68 
75  static void Callback(vtkObject *caller,
76  unsigned long eid, void *clientData, void *callData);
77 
82  vtkObservation *AddObservation (vtkObject *subject, unsigned long event, vtkObject *observer, vtkCallbackCommand *notify, float priority=0.0f);
83 
87  vtkObservation *AddObservation (vtkObject *subject, const char *event, const char *script);
88 
91  void RemoveObservation (vtkObservation *observation);
92 
97  void RemoveObservations (ObservationVector observations);
98  void RemoveObservations (vtkObject *observer);
99  void RemoveObservations (vtkObject *subject, vtkObject *observer);
100  void RemoveObservations (vtkObject *subject, unsigned long event, vtkObject *observer);
101  void RemoveObservations (vtkObject *subject, unsigned long event, vtkObject *observer, vtkCallbackCommand *notify);
102  void RemoveObservationsForSubjectByTag (vtkObject *subject, unsigned long tag);
104  ObservationVector GetSubjectObservations(vtkObject *subject);
109  ObservationVector GetObservations (vtkObject *subject,
110  unsigned long event = 0,
111  vtkObject *observer = nullptr,
112  vtkCallbackCommand *notify = nullptr,
113  unsigned int maxReturnedObservations = 0);
115  bool GetObservationExist (vtkObject *subject,
116  unsigned long event = 0,
117  vtkObject *observer = nullptr,
118  vtkCallbackCommand *notify = nullptr);
119  ObservationVector GetObservationsForSubjectByTag (vtkObject *subject, unsigned long tag);
120 
125  vtkCollection *GetObservationsForSubject (vtkObject *subject);
126  vtkCollection *GetObservationsForObserver (vtkObject *observer);
127  vtkCollection *GetObservationsForCallback (vtkCallbackCommand* callback);
128 
131  int GetNumberOfObservations();
132  vtkObservation *GetNthObservation(int n);
133 
136  void ProcessEvent (vtkObservation *observation, vtkObject *caller, unsigned long eid, void *callData);
137 
141  vtkBooleanMacro (EventLogging, int);
142  vtkSetMacro (EventLogging, int);
143  vtkGetMacro (EventLogging, int);
144 
148  vtkSetMacro (EventNestingLevel, int);
149  vtkGetMacro (EventNestingLevel, int);
150 
153  vtkSetStringMacro (LogFileName);
154  vtkGetStringMacro (LogFileName);
155 
158  virtual void SetTimerLog(vtkTimerLog* timerLog);
159  vtkGetObjectMacro (TimerLog, vtkTimerLog);
160 
163  void OpenLogFile ();
164  void CloseLogFile ();
165 
169  void LogEvent (vtkObservation *observation);
170 
174  int GenerateGraphFile ( const char *graphFile );
175 
176 
182  enum EventMode {
184  Asynchronous
185  };
186  vtkGetMacro(EventMode, int);
187  void SetEventMode(int eventMode)
188  {
189  if (eventMode != this->EventMode)
190  {
191  this->EventMode = eventMode;
192  this->ProcessEventQueue();
193  this->Modified();
194  }
195  };
196 
199  const char * GetEventModeAsString() {
200  if (this->EventMode == vtkEventBroker::Synchronous) return ("Synchronous");
201  if (this->EventMode == vtkEventBroker::Asynchronous) return ("Asynchronous");
202  return "Undefined";
203  }
204 
205 
207 
216  void QueueObservation (vtkObservation *observation, unsigned long eid,
217  void *callData);
218  int GetNumberOfQueuedObservations ();
219  vtkObservation *GetNthQueuedObservation (int n);
220  vtkObservation *DequeueObservation ();
221  void InvokeObservation (vtkObservation *observation, unsigned long eid,
222  void *callData);
223  void ProcessEventQueue ();
224 
232  vtkBooleanMacro (CompressCallData, int);
233  vtkGetMacro (CompressCallData, int);
234  vtkSetMacro (CompressCallData, int);
235 
238  void SetScriptHandler ( void (*scriptHandler) (const char* script, void *clientData), void *clientData )
239  {
240  this->ScriptHandler = scriptHandler;
241  this->ScriptHandlerClientData = clientData;
242  }
243 
248  virtual bool RequestModified(vtkObject* object);
249 
252  virtual void SetRequestModifiedCallback(vtkCallbackCommand* callback);
253  vtkGetObjectMacro(RequestModifiedCallback, vtkCallbackCommand);
254 
255 protected:
256  vtkEventBroker();
257  ~vtkEventBroker() override;
259  void operator=(const vtkEventBroker&);
260 
263  static void classInitialize();
264  static void classFinalize();
265 
272  void AttachObservation (vtkObservation *observation);
273  void DetachObservation (vtkObservation *observation);
274 
277 
278 
280  typedef std::map< vtkObject*, ObservationVector > ObjectToObservationVectorMap;
281 
285 
287  std::deque< vtkObservation * > EventQueue;
288 
289  void (*ScriptHandler) (const char* script, void* clientData);
291 
294  char *LogFileName;
295  vtkTimerLog *TimerLog;
296 
299 
300  std::ofstream LogFile;
301 
302  vtkCallbackCommand* RequestModifiedCallback;
303 
304 private:
308  void DetachObservations();
310  friend class vtkObservation;
311 };
312 
314 class VTK_MRML_EXPORT vtkEventBrokerInitialize
315 {
316 public:
318 
321 private:
322  static unsigned int Count;
323 };
324 
329 
330 #endif
void SetScriptHandler(void(*scriptHandler)(const char *script, void *clientData), void *clientData)
Sets the method pointer to be used for processing script observations
std::ofstream LogFile
vtkTimerLog * TimerLog
ObjectToObservationVectorMap SubjectMap
maps to manage quick lookup by object
static vtkEventBrokerInitialize vtkEventBrokerInitializer
Stores information about the relationship between a Subject and an Observer.
Class that manages adding and deleting of observers with events.
vtkCallbackCommand * RequestModifiedCallback
std::map< vtkObject *, ObservationVector > ObjectToObservationVectorMap
void * ScriptHandlerClientData
void SetEventModeToAsynchronous()
ObjectToObservationVectorMap ObserverMap
std::set< vtkObservation *> ObservationVector
vtkEventBrokerInitialize Self
const char * GetEventModeAsString()
std::deque< vtkObservation *> EventQueue
The event queue of triggered but not-yet-invoked observations.
void SetEventModeToSynchronous()
Utility class to make sure qSlicerModuleManager is initialized before it is used. ...
void SetEventMode(int eventMode)
vtkEventBroker Self