source: src/Patterns/unittests/stubs/ObserverStub.cpp@ e7352a5

Last change on this file since e7352a5 was 74e0f7, checked in by Frederik Heber <heber@…>, 14 years ago

Extended Observables by NotificationChannels member variable.

  • new class Channels that aggregates Notifications.
  • NOTIFY now just takes a channel number.
  • may or may not be instantiated for a given Observable.
  • Observable just needs to add enum of all desired channels and addChannel on each of these.
  • ObserverUnitTest has been adapted.
  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * ObserverStub.cpp
10 *
11 * Created on: Jan 19, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include <cppunit/extensions/HelperMacros.h>
21
22#include "Assert.hpp"
23
24#include "ObserverStub.hpp"
25
26/************ UpdateCountObserver **************/
27
28UpdateCountObserver::UpdateCountObserver() :
29 Observer("UpdateCountObserver"),
30 updates(0)
31{};
32
33UpdateCountObserver::~UpdateCountObserver()
34{}
35
36void UpdateCountObserver::update(Observable *publisher){
37 updates++;
38}
39
40void UpdateCountObserver::subjectKilled(Observable *publisher) {
41}
42
43/*************** SimpleObservable **************/
44
45SimpleObservable::SimpleObservable() :
46 Observable("SimpleObservable")
47{}
48
49void SimpleObservable::changeMethod() {
50 OBSERVE;
51 int i = 0;
52 i++;
53}
54
55/************** CallObservable *****************/
56
57CallObservable::CallObservable() :
58 Observable("CallObservable")
59{}
60
61void CallObservable::changeMethod1() {
62 OBSERVE;
63 int i = 0;
64 i++;
65}
66
67void CallObservable::changeMethod2() {
68 OBSERVE;
69 int i = 0;
70 i++;
71 changeMethod1();
72}
73
74/************* BlockObservable *****************/
75
76BlockObservable::BlockObservable() :
77 Observable("BlockObservable")
78{}
79
80void BlockObservable::changeMethod1(){
81 OBSERVE;
82 // test if we report correctly as blocked
83 CPPUNIT_ASSERT(isBlocked());
84}
85
86void BlockObservable::changeMethod2(){
87 OBSERVE;
88 internalMethod1();
89 internalMethod2();
90}
91
92void BlockObservable::internalMethod1(){
93 // we did not block, but our caller did...
94 // see if this is found
95 CPPUNIT_ASSERT(isBlocked());
96}
97
98void BlockObservable::internalMethod2(){
99 OBSERVE;
100 // Both this method and the caller do block
101 // Does the reporting still work as expected?
102 CPPUNIT_ASSERT(isBlocked());
103}
104
105void BlockObservable::noChangeMethod(){
106 // No Block introduced here
107 // reported correctely?
108 CPPUNIT_ASSERT(!isBlocked());
109}
110
111/*************** SuperObservable ***************/
112
113SuperObservable::SuperObservable():
114 Observable("SuperObservable")
115{
116 subObservable = new SimpleObservable();
117 subObservable->signOn(this);
118}
119SuperObservable::~SuperObservable(){
120 delete subObservable;
121}
122void SuperObservable::changeMethod() {
123 OBSERVE;
124 int i = 0;
125 i++;
126 subObservable->changeMethod();
127}
128
129/************* NotificationObservable **********/
130
131NotificationObservable::NotificationObservable() :
132 Observable("NotificationObservable")
133{
134 NotificationChannels = new Channels(this);
135 NotificationChannels->addChannel(Operation1Notify);
136 NotificationChannels->addChannel(Operation2Notify);
137}
138
139NotificationObservable::~NotificationObservable()
140{
141 delete NotificationChannels;
142}
143
144void NotificationObservable::operation1(){
145 OBSERVE;
146 NOTIFY(Operation1Notify);
147}
148
149void NotificationObservable::operation2(){
150 OBSERVE;
151 NOTIFY(Operation2Notify);
152}
153
154/*********** NotificationObserver **************/
155
156NotificationObserver::NotificationObserver(Notification_ptr notification) :
157 Observer("NotificationObserver"),
158 requestedNotification(notification),
159 wasNotified(false)
160{}
161
162NotificationObserver::~NotificationObserver()
163{}
164
165void NotificationObserver::update(Observable*){}
166void NotificationObserver::subjectKilled(Observable*){}
167void NotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
168 ASSERT(requestedNotification==notification,"Notification received that was not requested");
169 wasNotified = true;
170}
171
172/**************** ObservableSet ****************/
173
174ObservableSet::ObservableSet(int _num) :
175 Observable("ObservableCollection"),
176 num(_num)
177{
178 for(int i=0; i<num; ++i){
179 SimpleObservable *content = new SimpleObservable();
180 content->signOn(this);
181 theSet.insert(content);
182 }
183}
184
185ObservableSet::~ObservableSet(){
186 set::iterator iter;
187 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
188 delete (*iter);
189 }
190}
191
192ObservableSet::iterator ObservableSet::begin(){
193 return iterator(theSet.begin(),this);
194}
195
196ObservableSet::iterator ObservableSet::end(){
197 return iterator(theSet.end(),this);
198}
199
200/************** ObservableMap ******************/
201
202ObservableMap::ObservableMap(int _num) :
203 Observable("ObservableCollection"),
204 num(_num)
205{
206 for(int i=0; i<num; ++i){
207 SimpleObservable *content = new SimpleObservable();
208 content->signOn(this);
209 theSet.insert(std::make_pair(i,content));
210 }
211}
212
213ObservableMap::~ObservableMap(){
214 set::iterator iter;
215 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
216 delete iter->second;
217 }
218}
219
220ObservableMap::iterator ObservableMap::begin(){
221 return iterator(theSet.begin(),this);
222}
223
224ObservableMap::iterator ObservableMap::end(){
225 return iterator(theSet.end(),this);
226}
227
Note: See TracBrowser for help on using the repository browser.