HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Notifier.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Notifier.h UT library (C++)
7  *
8  * COMMENTS: Notifier that invokes registered functor callbacks whenever
9  * an event notification is triggered.
10  *
11  *
12  *
13  * HOW TO SET UP THE NOTIFIER AND THE CALLBACK:
14  *
15  * 1) decide what kind of event is passed from UT_NotifierImpl
16  * to the callback method. It can be an int, an enumerated
17  * type, or a class reference.
18  * For example,
19  * OP_Event &
20  *
21  * 2) in the observer class, write a method that handles events
22  * For example,
23  * void OP_SomeObserver::someHandler( OP_Event &event ) {}
24  *
25  * 3) in the observer class, declare the UT_NotifierList instance
26  * For example in .h,
27  * OP_SomeObserver
28  * {
29  * ...
30  * UT_NotifierList myNotifierList;
31  * ...
32  * };
33  *
34  * 4) in the emmitter class, instantiate the UT_NotifierImpl
35  * with <OP_Event &> as the type, and provide the accessor to it
36  * For example,
37  * OP_SomeEventEmmiter
38  * {
39  * UT_NotifierImpl<OP_Event&> &getNotifier()
40  * { return myNotifier; }
41  * ...
42  * private:
43  * UT_NotifierImpl<OP_Event &> myNotifier;
44  * };
45  *
46  * 5) in the observer class, create a functor that contains
47  * the callback method, and register the functor with notifier
48  * For example,
49  * void
50  * OP_SomeObserver::registerCallback( OP_SomeEventEmmiter *
51  * someEventEmitter )
52  * {
53  * UT_Functor1<void, OP_Event &>
54  * functor( this, &OP_SomeObserver::someHandler );
55  *
56  * someEventEmmiter->getEventNotifier().addObserver(
57  * myNotifierList, functor );
58  * }
59  *
60  * 6) whenever an event should be emitted, call notifiyObservers()
61  * on the notifier
62  * For example,
63  * void
64  * OP_SomeEventEmitter::someMethod()
65  * {
66  * ...
67  * OP_Event event( whatever, things, it, takes );
68  * myNotifier.notifyObservers( event );
69  * ...
70  * }
71  *
72  */
73 
74 #ifndef __UT_Notifier_h__
75 #define __UT_Notifier_h__
76 
77 #include "UT_API.h"
78 #include "UT_Array.h"
79 #include "UT_Functor.h"
80 #include "UT_NonCopyable.h"
81 #include <SYS/SYS_Types.h>
82 
83 #include <utility>
84 
85 // declare UT_NotifierList for the use in UT_NotifierImpl
86 class UT_NotifierList;
87 
88 // a pure virtual base class to refer to by the notifier list
90 {
91 public:
93  virtual ~UT_Notifier() {};
94 
96 
97  virtual void removeObserver( UT_NotifierList & ) = 0;
98 };
99 
100 // define UT_NotifierImpl that handles the distribution of the notifications
101 template <typename EVENT_TYPE>
103 {
104 public:
105  UT_NotifierImpl();
106  ~UT_NotifierImpl() override;
107 
108  // a definition of the callback functor
110 
111 
112  // ----------------------------------------------------------------------
113  // adds a callback functor to this notifier
114  // INPUTS:
115  // list - a list of notifiers the caller expressed interest in
116  // (which should not contain this notifier yet)
117  // callback - a functor invoked as a notification of an event
118  // OUTPUTS:
119  // list - the list is updated to include this notifier.
120  // If the list contained this notifier already, the
121  // callback is not registered (i.e., the old callback
122  // prevails)
123  // ----------------------------------------------------------------------
124  virtual void addObserver( UT_NotifierList &list, Callback &callback);
125 
126  // ----------------------------------------------------------------------
127  // removes a callback functor from this notifier
128  // INPUTS:
129  // list - a list of notifiers the caller expressed interest in
130  // (which should also contain this notifier)
131  // OUTPUTS:
132  // list - this notifier is removed from the list. If this notifier
133  // is not found in the list, no action is taken.
134  // ----------------------------------------------------------------------
135  void removeObserver( UT_NotifierList &list ) override;
136 
137  // ----------------------------------------------------------------------
138  // checks if this notifier has an observer registered
139  // INPUTS:
140  // list - a list based on which we check the observer registration
141  // RETURNS:
142  // true if the list was used to add an observer to this notifer, and
143  // the observer is still registered.
144  // false if this notifier has no callback functor was registered
145  // with the list.
146  // ----------------------------------------------------------------------
147  virtual bool hasObserver( const UT_NotifierList &list ) const;
148 
149  // ----------------------------------------------------------------------
150  // invokes all regisered functors using the event as argument
151  // INPUTS:
152  // event - an event passed to the functors
153  // NB:
154  // non-const because it needs to invoke a non-const functor, and if
155  // this method is const then only const functor is returned, which in
156  // turn tries to invoke operator() for which only non-const version
157  // is available
158  // ----------------------------------------------------------------------
159  virtual void notifyObservers( EVENT_TYPE event );
160 
161  /// Returns the amount of memory owned by this UT_NotifierImpl
162  int64 getMemoryUsage(bool inclusive) const
163  {
164  int64 mem = inclusive ? sizeof(*this) : 0;
165  mem += myObservers.getMemoryUsage(false);
166  for (exint i = 0; i < myObservers.entries(); ++i)
167  mem += myObservers(i).second.getMemoryUsage(false);
168  return mem;
169  }
170 
171  // ----------------------------------------------------------------------
172  // return true if the notifier has observers
173  // INPUTS:
174  // none
175  // ----------------------------------------------------------------------
176  bool hasObservers() const;
177 
178 private:
179  // ----------------------------------------------------------------------
180  // finds the index to the observer list that contains the argument
181  // (i.e., the notifier list)
182  // INPUTS:
183  // list - a list based on which we check the observer registration
184  // RETURNS:
185  // index to the observer element containing "list"
186  // with the list.
187  // ----------------------------------------------------------------------
188  int getObserverIndex( const UT_NotifierList &list ) const;
189 
190  // ----------------------------------------------------------------------
191  // removes all observers
192  // ----------------------------------------------------------------------
193  void removeAllObservers();
194 
195  // ----------------------------------------------------------------------
196  // iterates through the whole list of observers and removes any entries
197  // that have NULL as their list member of the pair (i.e., entries that are
198  // marked for deletion)
199  // ----------------------------------------------------------------------
200  void pruneObservers();
201 
202 private:
203  // define a type that we are storing
204  typedef std::pair< UT_NotifierList*, Callback> ObserverPair;
205 
206  // list of registered callbacks to invoke during the distribution
207  // of notifications. The observers is an array of pairs. The pair
208  // contains a list pointer based on which we recognize observer,
209  // and the callback functor which is invoked as the notification.
210  UT_Array< ObserverPair > myObservers;
211 
212  // some state flags
213  bool myNotifyingFlag; // currently notifying observers
214  bool myPendingRemovalFlag; // list contains observers to remove
215 };
216 
217 // include the templated implementation
218 #include "UT_Notifier.C"
219 
220 #endif // __UT_Notifier_h__
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
int64 exint
Definition: SYS_Types.h:125
#define UT_API
Definition: UT_API.h:14
struct _cl_event * event
Definition: glcorearb.h:2961
virtual ~UT_Notifier()
Definition: UT_Notifier.h:93
UT_Functor1< void, EVENT_TYPE > Callback
Definition: UT_Notifier.h:109
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
int64 getMemoryUsage(bool inclusive) const
Returns the amount of memory owned by this UT_NotifierImpl.
Definition: UT_Notifier.h:162