a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
BasicObserved.hpp
Go to the documentation of this file.
1/*
2 * BasicObserved.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 BASICOBSERVED_HPP_
14#define BASICOBSERVED_HPP_
15
16#include <list>
17#include "BasicObserver.hpp"
18
19namespace BOL
20{
21 // this class holds a list of BasicObserver pointers & calls
22 // respondToObservedSignal() on them all with updateObservers().
24 {
25 public:
27 virtual
29
30 void
32 /* this goes through observerList & either removes the observer if it has
33 * flagged its bool as false, or calls its respondToObservedSignal()
34 * otherwise.
35 */
36 void
37 registerObserver( BasicObserver* const joiningObserver );
38 void
39 removeObserver( BasicObserver* const leavingObserver );
40 /* this goes through observerList & removes any observers which have changed
41 * their flags to false, & also removes leavingObserver if found (1st asking
42 * it to discard its bool pointer).
43 */
44 void
46 // this clears observerList after asking all its observers to discard their
47 // bool pointers for this BasicObserved instance.
48
49
50 protected:
51 std::list< std::pair< BasicObserver*, bool > > observerList;
52 std::list< std::pair< BasicObserver*, bool > >::iterator observerIterator;
53 };
54
55
56
57
58
59 inline void
61 /* this goes through observerList & either removes the observer if it has
62 * flagged its bool as false, or calls its respondToObservedSignal()
63 * otherwise.
64 */
65 {
67 while( observerList.end() != observerIterator )
68 {
69 if( observerIterator->second )
70 {
71 observerIterator->first->respondToObservedSignal();
73 }
74 else
75 {
77 }
78 }
79 }
80
81 inline void
83 {
84 observerList.push_back( std::pair< BasicObserver*, bool >( joiningObserver,
85 true ) );
86 joiningObserver->acceptFlagFromObserved( &(observerList.back().second) );
87 /* new observers are given a pointer to their associated bools, which
88 * indicate that their observers should be fine for
89 * respondToObservedSignal() calls if the bool is true, but if the bool is
90 * false, this BasicObserved instance knows to remove the observer with
91 * its next pass through the list.
92 */
93 }
94
95 inline void
97 /* this goes through observerList & removes any observers which have changed
98 * their flags to false, & also removes leavingObserver if found (1st asking
99 * it to discard its bool pointer).
100 */
101 {
103 while( observerList.end() != observerIterator )
104 {
105 if( !(observerIterator->second) )
106 {
108 }
109 else if( leavingObserver == observerIterator->first )
110 {
111 leavingObserver->discardFlagFromObserved(
112 &(observerIterator->second) );
114 }
115 else
116 {
118 }
119 }
120 }
121
122 inline void
124 // this clears observerList after asking all its observers to discard their
125 // bool pointers for this BasicObserved instance.
126 {
128 while( observerList.end() != observerIterator )
129 {
130 if( observerIterator->second )
131 {
132 observerIterator->first->discardFlagFromObserved(
133 &(observerIterator->second) );
134 }
136 }
137 observerList.clear();
138 }
139
140}
141
142#endif /* BASICOBSERVED_HPP_ */
void registerObserver(BasicObserver *const joiningObserver)
std::list< std::pair< BasicObserver *, bool > > observerList
virtual ~BasicObserved()
void removeObserver(BasicObserver *const leavingObserver)
std::list< std::pair< BasicObserver *, bool > >::iterator observerIterator
void discardFlagFromObserved(bool *const flagFromObserved)
void acceptFlagFromObserved(bool *const flagFromObserved)