a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
PushingObserved.hpp
Go to the documentation of this file.
1/*
2 * PushingObserved.hpp
3 *
4 * Created on: Mar 4, 2012
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 *
7 * This file is part of BOLlib, released under the
8 * GNU General Public License. Please see the accompanying
9 * README.BOLlib.txt file for a full list of files, brief documentation
10 * on how to use these classes, and further details on the license.
11 */
12
13#ifndef PUSHINGOBSERVED_HPP_
14#define PUSHINGOBSERVED_HPP_
15
16#include <list>
17#include "PushedToObserver.hpp"
18
19namespace BOL
20{
21 /* this template class holds a list of PushedToObserver< PushedClass >
22 * pointers & calls respondToObservedSignal( pushedValue ) on them all with
23 * updateObservers( pushedValue ).
24 */
25 template< typename PushedClass >
27 {
28 public:
30 virtual
32
33 virtual void
34 updateObservers( PushedClass const& pushedValue );
35 /* this goes through observerList & either removes the observer if it has
36 * flagged its bool as false, or calls its respondToPush( pushedValue )
37 * otherwise.
38 */
39 virtual void
41 /* this goes through observerList & either removes the observer if it has
42 * flagged its bool as false, or calls its respondToObservedSignal()
43 * otherwise.
44 */
45 virtual void
47 virtual void
49 /* this goes through observerList & removes any observers which have
50 * changed their flags to false, & also removes leavingObserver if found
51 * (1st asking it to discard its bool pointer).
52 */
53 void
55 // this clears observerList after asking all its observers to discard their
56 // bool pointers for this PushingObserved instance.
57
58
59 protected:
60 typedef typename
61 std::pair< PushedToObserver< PushedClass >*, bool > observerWithBool;
62 typedef typename
63 std::list< observerWithBool >::iterator observerWithBoolListIterator;
64 std::list< observerWithBool > observerList;
66 };
67
68
69
70
71
72 template< typename PushedClass >
73 inline
75 observerList(),
76 observerIterator()
77 {
78 // just an initialization list.
79 }
80
81 template< typename PushedClass >
82 inline
84 {
85 removeAllObservers();
86 }
87
88
89 template< typename PushedClass >
90 inline void
92 PushedClass const& pushedValue )
93 /* this goes through observerList & either removes the observer if it has
94 * flagged its bool as false, or calls its respondToPush( pushedValue )
95 * otherwise.
96 */
97 {
98 observerIterator = observerList.begin();
99 while( observerList.end() != observerIterator )
100 {
101 if( observerIterator->second )
102 {
103 observerIterator->first->respondToPush( pushedValue );
104 ++observerIterator;
105 }
106 else
107 {
108 observerIterator = observerList.erase( observerIterator );
109 }
110 }
111 }
112
113 template< typename PushedClass >
114 inline void
116 /* this goes through observerList & either removes the observer if it has
117 * flagged its bool as false, or calls its respondToObservedSignal()
118 * otherwise.
119 */
120 {
121 observerIterator = observerList.begin();
122 while( observerList.end() != observerIterator )
123 {
124 if( observerIterator->second )
125 {
126 observerIterator->first->respondToObservedSignal();
127 ++observerIterator;
128 }
129 else
130 {
131 observerIterator = observerList.erase( observerIterator );
132 }
133 }
134 }
135
136 template< typename PushedClass >
137 inline void
139 PushedToObserver< PushedClass >* const joiningObserver )
140 {
141 observerList.push_back( observerWithBool( joiningObserver,
142 true ) );
143 joiningObserver->acceptFlagFromObserved( &(observerList.back().second) );
144 /* new observers are given a pointer to their associated bools, which
145 * indicate that their observers should be fine for
146 * respondToPush( ... ) calls if the bool is true, but if the bool is
147 * false, this PushingObserved instance knows to remove the observer with
148 * its next pass through the list.
149 */
150 }
151
152 template< typename PushedClass >
153 inline void
155 PushedToObserver< PushedClass >* const leavingObserver )
156 {
157 observerIterator = observerList.begin();
158 while( observerList.end() != observerIterator )
159 {
160 if( !(observerIterator->second) )
161 {
162 observerIterator = observerList.erase( observerIterator );
163 }
164 else if( leavingObserver == observerIterator->first )
165 {
166 leavingObserver->discardFlagFromObserved(
167 &(observerIterator->second) );
168 observerIterator = observerList.erase( observerIterator );
169 }
170 else
171 {
172 ++observerIterator;
173 }
174 }
175 }
176
177 template< typename PushedClass >
178 inline void
180 // this clears observerList after asking all its observers to discard their
181 // bool pointers for this BasicObserved instance.
182 {
183 observerIterator = observerList.begin();
184 while( observerList.end() != observerIterator )
185 {
186 if( observerIterator->second )
187 {
188 observerIterator->first->discardFlagFromObserved(
189 &(observerIterator->second) );
190 }
191 ++observerIterator;
192 }
193 observerList.clear();
194 }
195
196}
197
198#endif /* PUSHINGOBSERVED_HPP_ */
void discardFlagFromObserved(bool *const flagFromObserved)
void acceptFlagFromObserved(bool *const flagFromObserved)
std::list< observerWithBool > observerList
std::list< observerWithBool >::iterator observerWithBoolListIterator
std::pair< PushedToObserver< PushedClass > *, bool > observerWithBool
virtual void registerObserver(PushedToObserver< PushedClass > *const joiningObserver)
virtual void updateObservers(PushedClass const &pushedValue)
virtual void updateObservers()
observerWithBoolListIterator observerIterator
virtual void removeObserver(PushedToObserver< PushedClass > *const leavingObserver)