HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Notifier.C
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.C UT library (C++)
7  *
8  * COMMENTS: Notifier that invokes registered functor callbacks whenever
9  * an event notification is triggered.
10  *
11  */
12 
13 #ifndef __UT_Notifier_C__
14 #define __UT_Notifier_C__
15 
16 #include "UT_Notifier.h"
17 #include "UT_NotifierList.h"
18 #include "UT_Assert.h"
19 
20 template <typename EVENT_TYPE>
22  : myNotifyingFlag( false ),
23  myPendingRemovalFlag( false )
24 {
25 }
26 
27 template <typename EVENT_TYPE>
29 {
30  removeAllObservers();
31 }
32 
33 
34 template <typename EVENT_TYPE>
35 int
37 {
38  int i;
39 
40  for( i = 0; i < myObservers.entries(); i ++ )
41  if( myObservers(i).first == &list ) //pointer comparison
42  return i;
43 
44  return -1;
45 }
46 
47 
48 template <typename EVENT_TYPE>
49 bool
51 {
52  return ( 0 <= getObserverIndex( list ));
53 }
54 
55 
56 template <typename EVENT_TYPE>
57 void
59  Callback &callback)
60 {
61  bool already_registered;
62 
63  // check if we already contain callback associated with the list
64  already_registered = hasObserver( list );
65  UT_ASSERT( ! already_registered );
66 
67  // if not registered yet
68  if( ! already_registered )
69  {
70  ObserverPair pair( &list, callback );
71 
72  // add to our own list and add ourseleves to the UT_NotifierList
73  myObservers.append( pair );
74  list.addNotifier( *this );
75  }
76 }
77 
78 
79 template <typename EVENT_TYPE>
80 void
82 {
83  int i;
84 
85  // find an observer
86  i = getObserverIndex( list );
87  if( 0 <= i )
88  {
89  // remove the observer form our list and remove ourseleves form the list
90  list.removeNotifier( *this );
91  if( myNotifyingFlag )
92  {
93  myObservers(i).first = NULL;
94  myPendingRemovalFlag = true;
95  }
96  else
97  {
98  myObservers.removeIndex( i );
99  }
100  }
101  else
102  {
103  UT_ASSERT( ! "no observer to remove" );
104  }
105 }
106 
107 
108 template <typename EVENT_TYPE>
109 void
111 {
112  int i;
113 
114  // descending order so that we do not shift any memory in the array
115  for( i = myObservers.entries(); i -- > 0; )
116  {
117  if( myObservers(i).first )
118  myObservers(i).first->removeNotifier( *this );
119  myObservers.removeIndex( i );
120  }
121 }
122 
123 template <typename EVENT_TYPE>
124 void
126 {
127  int i;
128 
129  // descending order so that we do not shift any memory in the array
130  for( i = myObservers.entries(); i -- > 0; )
131  if( myObservers(i).first == NULL )
132  myObservers.removeIndex( i );
133 
134  // flag that anything that was to be removed has been removed
135  myPendingRemovalFlag = false;
136 }
137 
138 template <typename EVENT_TYPE>
139 void
141 {
142  int i, n;
143  bool old_notifying;
144 
145  // flag that we are in the notification loop
146  old_notifying = myNotifyingFlag;
147  myNotifyingFlag = true;
148 
149  // go through the list and invoke the functor with the event as an argument
150  // NB: we obtain n prior to the iteration so that if any new observer is
151  // added during the loop, they are not notified right away.
152  n = myObservers.entries();
153  for( i = 0; i < n; i++ )
154  if( myObservers(i).first )
155  myObservers(i).second( event );
156 
157  // restore flag (NB this allows recursive notifications)
158  myNotifyingFlag = old_notifying;
159 
160  // if finished notifying, and any observer was removed, then prune the list
161  if( !myNotifyingFlag && myPendingRemovalFlag )
162  pruneObservers();
163 }
164 
165 template <typename EVENT_TYPE>
167 {
168  return myObservers.entries() > 0;
169 }
170 
171 #endif // __UT_Notifier_C__
GLint first
Definition: glcorearb.h:405
virtual void addNotifier(UT_Notifier &notifier)
virtual bool hasObserver(const UT_NotifierList &list) const
Definition: UT_Notifier.C:50
void removeObserver(UT_NotifierList &list) override
Definition: UT_Notifier.C:81
virtual void notifyObservers(EVENT_TYPE event)
Definition: UT_Notifier.C:140
struct _cl_event * event
Definition: glcorearb.h:2961
GLdouble n
Definition: glcorearb.h:2008
~UT_NotifierImpl() override
Definition: UT_Notifier.C:28
virtual void removeNotifier(UT_Notifier &notifier)
bool hasObservers() const
Definition: UT_Notifier.C:166
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
virtual void addObserver(UT_NotifierList &list, Callback &callback)
Definition: UT_Notifier.C:58