source: src/Patterns/unittests/stubs/ObserverStub.cpp@ 56d62f

Last change on this file since 56d62f was 56d62f, checked in by Frederik Heber <heber@…>, 15 years ago

Placed various Observer stubs into own module.

  • Library version is now 4:0:1, API version is still 1.0.7.
  • the stubs are also much easier to read when declared additionally in a header file.
  • Property mode set to 100644
File size: 4.6 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
33void UpdateCountObserver::update(Observable *publisher){
34 updates++;
35}
36
37void UpdateCountObserver::subjectKilled(Observable *publisher) {
38}
39
40/*************** SimpleObservable **************/
41
42SimpleObservable::SimpleObservable() :
43 Observable("SimpleObservable")
44{}
45
46void SimpleObservable::changeMethod() {
47 OBSERVE;
48 int i = 0;
49 i++;
50}
51
52/************** CallObservable *****************/
53
54CallObservable::CallObservable() :
55 Observable("CallObservable")
56{}
57
58void CallObservable::changeMethod1() {
59 OBSERVE;
60 int i = 0;
61 i++;
62}
63
64void CallObservable::changeMethod2() {
65 OBSERVE;
66 int i = 0;
67 i++;
68 changeMethod1();
69}
70
71/************* BlockObservable *****************/
72
73BlockObservable::BlockObservable() :
74 Observable("BlockObservable")
75{}
76
77void BlockObservable::changeMethod1(){
78 OBSERVE;
79 // test if we report correctly as blocked
80 CPPUNIT_ASSERT(isBlocked());
81}
82
83void BlockObservable::changeMethod2(){
84 OBSERVE;
85 internalMethod1();
86 internalMethod2();
87}
88
89void BlockObservable::internalMethod1(){
90 // we did not block, but our caller did...
91 // see if this is found
92 CPPUNIT_ASSERT(isBlocked());
93}
94
95void BlockObservable::internalMethod2(){
96 OBSERVE;
97 // Both this method and the caller do block
98 // Does the reporting still work as expected?
99 CPPUNIT_ASSERT(isBlocked());
100}
101
102void BlockObservable::noChangeMethod(){
103 // No Block introduced here
104 // reported correctely?
105 CPPUNIT_ASSERT(!isBlocked());
106}
107
108/*************** SuperObservable ***************/
109
110SuperObservable::SuperObservable():
111 Observable("SuperObservable")
112{
113 subObservable = new SimpleObservable();
114 subObservable->signOn(this);
115}
116SuperObservable::~SuperObservable(){
117 delete subObservable;
118}
119void SuperObservable::changeMethod() {
120 OBSERVE;
121 int i = 0;
122 i++;
123 subObservable->changeMethod();
124}
125
126/************* NotificationObservable **********/
127
128NotificationObservable::NotificationObservable() :
129 Observable("NotificationObservable"),
130 notification1(new Notification(this)),
131 notification2(new Notification(this))
132{}
133
134NotificationObservable::~NotificationObservable(){
135 delete notification1;
136 delete notification2;
137}
138
139void NotificationObservable::operation1(){
140 OBSERVE;
141 NOTIFY(notification1);
142}
143
144void NotificationObservable::operation2(){
145 OBSERVE;
146 NOTIFY(notification2);
147}
148
149/*********** NotificationObserver **************/
150
151NotificationObserver::NotificationObserver(Notification_ptr notification) :
152 Observer("NotificationObserver"),
153 requestedNotification(notification),
154 wasNotified(false)
155{}
156
157void NotificationObserver::update(Observable*){}
158void NotificationObserver::subjectKilled(Observable*){}
159void NotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
160 ASSERT(requestedNotification==notification,"Notification received that was not requested");
161 wasNotified = true;
162}
163
164/**************** ObservableSet ****************/
165
166ObservableSet::ObservableSet(int _num) :
167 Observable("ObservableCollection"),
168 num(_num)
169{
170 for(int i=0; i<num; ++i){
171 SimpleObservable *content = new SimpleObservable();
172 content->signOn(this);
173 theSet.insert(content);
174 }
175}
176
177ObservableSet::~ObservableSet(){
178 set::iterator iter;
179 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
180 delete (*iter);
181 }
182}
183
184ObservableSet::iterator ObservableSet::begin(){
185 return iterator(theSet.begin(),this);
186}
187
188ObservableSet::iterator ObservableSet::end(){
189 return iterator(theSet.end(),this);
190}
191
192/************** ObservableMap ******************/
193
194ObservableMap::ObservableMap(int _num) :
195 Observable("ObservableCollection"),
196 num(_num)
197{
198 for(int i=0; i<num; ++i){
199 SimpleObservable *content = new SimpleObservable();
200 content->signOn(this);
201 theSet.insert(std::make_pair(i,content));
202 }
203}
204
205ObservableMap::~ObservableMap(){
206 set::iterator iter;
207 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
208 delete iter->second;
209 }
210}
211
212ObservableMap::iterator ObservableMap::begin(){
213 return iterator(theSet.begin(),this);
214}
215
216ObservableMap::iterator ObservableMap::end(){
217 return iterator(theSet.end(),this);
218}
219
Note: See TracBrowser for help on using the repository browser.