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