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

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

NotificationChannels is now static entity.

  • this is in line with the other static entities on Observable that are intended to save memory (before we needed 8 bytes for the pointer in each and every Observable instances, whether it had channels or not.)
  • Modified NOTIFY macro to accomodate for the changes.
  • enhanced documentation of class Notifications.
  • 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 Channels *OurChannel = new Channels(this);
135 NotificationChannels.insert( std::make_pair(this, OurChannel) );
136 OurChannel->addChannel(Operation1Notify);
137 OurChannel->addChannel(Operation2Notify);
138}
139
140NotificationObservable::~NotificationObservable()
141{
142 NotificationChannels.erase(this);
143}
144
145void NotificationObservable::operation1(){
146 OBSERVE;
147 NOTIFY(Operation1Notify);
148}
149
150void NotificationObservable::operation2(){
151 OBSERVE;
152 NOTIFY(Operation2Notify);
153}
154
155/*********** NotificationObserver **************/
156
157NotificationObserver::NotificationObserver(Notification_ptr notification) :
158 Observer("NotificationObserver"),
159 requestedNotification(notification),
160 wasNotified(false)
161{}
162
163NotificationObserver::~NotificationObserver()
164{}
165
166void NotificationObserver::update(Observable*){}
167void NotificationObserver::subjectKilled(Observable*){}
168void NotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
169 ASSERT(requestedNotification==notification,"Notification received that was not requested");
170 wasNotified = true;
171}
172
173/**************** ObservableSet ****************/
174
175ObservableSet::ObservableSet(int _num) :
176 Observable("ObservableCollection"),
177 num(_num)
178{
179 for(int i=0; i<num; ++i){
180 SimpleObservable *content = new SimpleObservable();
181 content->signOn(this);
182 theSet.insert(content);
183 }
184}
185
186ObservableSet::~ObservableSet(){
187 set::iterator iter;
188 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
189 delete (*iter);
190 }
191}
192
193ObservableSet::iterator ObservableSet::begin(){
194 return iterator(theSet.begin(),this);
195}
196
197ObservableSet::iterator ObservableSet::end(){
198 return iterator(theSet.end(),this);
199}
200
201/************** ObservableMap ******************/
202
203ObservableMap::ObservableMap(int _num) :
204 Observable("ObservableCollection"),
205 num(_num)
206{
207 for(int i=0; i<num; ++i){
208 SimpleObservable *content = new SimpleObservable();
209 content->signOn(this);
210 theSet.insert(std::make_pair(i,content));
211 }
212}
213
214ObservableMap::~ObservableMap(){
215 set::iterator iter;
216 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
217 delete iter->second;
218 }
219}
220
221ObservableMap::iterator ObservableMap::begin(){
222 return iterator(theSet.begin(),this);
223}
224
225ObservableMap::iterator ObservableMap::end(){
226 return iterator(theSet.end(),this);
227}
228
Note: See TracBrowser for help on using the repository browser.