| [084729c] | 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 "CodePatterns/Assert.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | #include "ObserverStub.hpp"
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | #include "CodePatterns/Observer/Channels.hpp"
 | 
|---|
 | 27 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | /************ UpdateCountObserver **************/
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | UpdateCountObserver::UpdateCountObserver() :
 | 
|---|
 | 32 |   Observer("UpdateCountObserver"),
 | 
|---|
 | 33 |   updates(0)
 | 
|---|
 | 34 | {};
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | UpdateCountObserver::~UpdateCountObserver()
 | 
|---|
 | 37 | {}
 | 
|---|
 | 38 | 
 | 
|---|
 | 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() :
 | 
|---|
 | 135 |   Observable("NotificationObservable")
 | 
|---|
 | 136 | {
 | 
|---|
 | 137 |   Channels *OurChannel = new Channels();
 | 
|---|
 | 138 |   Observable::insertNotificationChannel( std::make_pair(this, OurChannel) );
 | 
|---|
 | 139 |   OurChannel->addChannel(Operation1Notify);
 | 
|---|
 | 140 |   OurChannel->addChannel(Operation2Notify);
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | NotificationObservable::~NotificationObservable()
 | 
|---|
 | 144 | {
 | 
|---|
 | 145 |   Observable::eraseNotificationChannel(this);
 | 
|---|
 | 146 | }
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 | void NotificationObservable::operation1(){
 | 
|---|
 | 149 |   OBSERVE;
 | 
|---|
 | 150 |   NOTIFY(Operation1Notify);
 | 
|---|
 | 151 | }
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 | void NotificationObservable::operation2(){
 | 
|---|
 | 154 |   OBSERVE;
 | 
|---|
 | 155 |   NOTIFY(Operation2Notify);
 | 
|---|
 | 156 | }
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | /*********** NotificationObserver **************/
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 | NotificationObserver::NotificationObserver(Notification_ptr notification) :
 | 
|---|
 | 161 |   Observer("NotificationObserver"),
 | 
|---|
 | 162 |   requestedNotification(notification),
 | 
|---|
 | 163 |   wasNotified(false)
 | 
|---|
 | 164 | {}
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 | NotificationObserver::~NotificationObserver()
 | 
|---|
 | 167 | {}
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 | void NotificationObserver::update(Observable*){}
 | 
|---|
 | 170 | void NotificationObserver::subjectKilled(Observable*){}
 | 
|---|
 | 171 | void NotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
 | 
|---|
 | 172 |   CPPUNIT_ASSERT(requestedNotification==notification);
 | 
|---|
 | 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 | 
 | 
|---|
 | 232 | /****************** Relay *********************/
 | 
|---|
 | 233 | 
 | 
|---|
 | 234 | RelayTest::RelayTest() :
 | 
|---|
 | 235 |     Relay(std::string("RelayTest"))
 | 
|---|
 | 236 | {}
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 | RelayTest::~RelayTest()
 | 
|---|
 | 239 | {}
 | 
|---|
 | 240 | 
 | 
|---|
 | 241 | /************ RelayNotification ***************/
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 | RelayNotification::RelayNotification() :
 | 
|---|
 | 244 |     Relay(std::string("RelayTest"))
 | 
|---|
 | 245 | {
 | 
|---|
 | 246 |   Channels *OurChannel = new Channels();
 | 
|---|
 | 247 |   Observable::insertNotificationChannel( std::make_pair(this, OurChannel) );
 | 
|---|
 | 248 |   OurChannel->addChannel(NotificationObservable::Operation1Notify);
 | 
|---|
 | 249 |   OurChannel->addChannel(NotificationObservable::Operation2Notify);
 | 
|---|
 | 250 | }
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 | RelayNotification::~RelayNotification()
 | 
|---|
 | 253 | {}
 | 
|---|
 | 254 | 
 | 
|---|
 | 255 | /************ RelayCountObserver ***************/
 | 
|---|
 | 256 | 
 | 
|---|
 | 257 | RelayCountObserver::RelayCountObserver(const Observable * const _relay) :
 | 
|---|
 | 258 |     Observer("RelayCountObserver"),
 | 
|---|
 | 259 |     updates(0),
 | 
|---|
 | 260 |     relay(_relay)
 | 
|---|
 | 261 | {}
 | 
|---|
 | 262 | 
 | 
|---|
 | 263 | RelayCountObserver::~RelayCountObserver()
 | 
|---|
 | 264 | {}
 | 
|---|
 | 265 | 
 | 
|---|
 | 266 | void RelayCountObserver::update(Observable *publisher){
 | 
|---|
 | 267 |   // check that we are not called by the relay itself
 | 
|---|
 | 268 |   CPPUNIT_ASSERT(publisher != relay);
 | 
|---|
 | 269 |   updates++;
 | 
|---|
 | 270 | }
 | 
|---|
 | 271 | 
 | 
|---|
 | 272 | void RelayCountObserver::subjectKilled(Observable *publisher) {
 | 
|---|
 | 273 | }
 | 
|---|
 | 274 | 
 | 
|---|
 | 275 | /********* RelayNotificationObserver ***********/
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 | RelayNotificationObserver::RelayNotificationObserver(const Observable * const _relay) :
 | 
|---|
 | 278 |   Observer("RelayNotificationObserver"),
 | 
|---|
 | 279 |   wasNotified(false),
 | 
|---|
 | 280 |   relay(_relay)
 | 
|---|
 | 281 | {}
 | 
|---|
 | 282 | 
 | 
|---|
 | 283 | RelayNotificationObserver::~RelayNotificationObserver()
 | 
|---|
 | 284 | {}
 | 
|---|
 | 285 | 
 | 
|---|
 | 286 | void RelayNotificationObserver::update(Observable*){}
 | 
|---|
 | 287 | void RelayNotificationObserver::subjectKilled(Observable*){}
 | 
|---|
 | 288 | void RelayNotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
 | 
|---|
 | 289 |   // check that we are not called by the relay itself
 | 
|---|
 | 290 |   CPPUNIT_ASSERT(publisher != relay);
 | 
|---|
 | 291 |   wasNotified = true;
 | 
|---|
 | 292 | }
 | 
|---|
 | 293 | 
 | 
|---|