| 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 | 
 | 
|---|
| 28 | UpdateCountObserver::UpdateCountObserver() :
 | 
|---|
| 29 |   Observer("UpdateCountObserver"),
 | 
|---|
| 30 |   updates(0)
 | 
|---|
| 31 | {};
 | 
|---|
| 32 | 
 | 
|---|
| 33 | void UpdateCountObserver::update(Observable *publisher){
 | 
|---|
| 34 |   updates++;
 | 
|---|
| 35 | }
 | 
|---|
| 36 | 
 | 
|---|
| 37 | void UpdateCountObserver::subjectKilled(Observable *publisher) {
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /*************** SimpleObservable **************/
 | 
|---|
| 41 | 
 | 
|---|
| 42 | SimpleObservable::SimpleObservable() :
 | 
|---|
| 43 |   Observable("SimpleObservable")
 | 
|---|
| 44 | {}
 | 
|---|
| 45 | 
 | 
|---|
| 46 | void SimpleObservable::changeMethod() {
 | 
|---|
| 47 |   OBSERVE;
 | 
|---|
| 48 |   int i = 0;
 | 
|---|
| 49 |   i++;
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /************** CallObservable *****************/
 | 
|---|
| 53 | 
 | 
|---|
| 54 | CallObservable::CallObservable() :
 | 
|---|
| 55 |   Observable("CallObservable")
 | 
|---|
| 56 | {}
 | 
|---|
| 57 | 
 | 
|---|
| 58 | void CallObservable::changeMethod1() {
 | 
|---|
| 59 |   OBSERVE;
 | 
|---|
| 60 |   int i = 0;
 | 
|---|
| 61 |   i++;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | void CallObservable::changeMethod2() {
 | 
|---|
| 65 |   OBSERVE;
 | 
|---|
| 66 |   int i = 0;
 | 
|---|
| 67 |   i++;
 | 
|---|
| 68 |   changeMethod1();
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /************* BlockObservable *****************/
 | 
|---|
| 72 | 
 | 
|---|
| 73 | BlockObservable::BlockObservable() :
 | 
|---|
| 74 |   Observable("BlockObservable")
 | 
|---|
| 75 | {}
 | 
|---|
| 76 | 
 | 
|---|
| 77 | void BlockObservable::changeMethod1(){
 | 
|---|
| 78 |   OBSERVE;
 | 
|---|
| 79 |   // test if we report correctly as blocked
 | 
|---|
| 80 |   CPPUNIT_ASSERT(isBlocked());
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | void BlockObservable::changeMethod2(){
 | 
|---|
| 84 |   OBSERVE;
 | 
|---|
| 85 |   internalMethod1();
 | 
|---|
| 86 |   internalMethod2();
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | void BlockObservable::internalMethod1(){
 | 
|---|
| 90 |   // we did not block, but our caller did...
 | 
|---|
| 91 |   // see if this is found
 | 
|---|
| 92 |   CPPUNIT_ASSERT(isBlocked());
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | void 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 | 
 | 
|---|
| 102 | void BlockObservable::noChangeMethod(){
 | 
|---|
| 103 |   // No Block introduced here
 | 
|---|
| 104 |   // reported correctely?
 | 
|---|
| 105 |   CPPUNIT_ASSERT(!isBlocked());
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | /*************** SuperObservable ***************/
 | 
|---|
| 109 | 
 | 
|---|
| 110 | SuperObservable::SuperObservable():
 | 
|---|
| 111 |   Observable("SuperObservable")
 | 
|---|
| 112 | {
 | 
|---|
| 113 |   subObservable = new SimpleObservable();
 | 
|---|
| 114 |   subObservable->signOn(this);
 | 
|---|
| 115 | }
 | 
|---|
| 116 | SuperObservable::~SuperObservable(){
 | 
|---|
| 117 |   delete subObservable;
 | 
|---|
| 118 | }
 | 
|---|
| 119 | void SuperObservable::changeMethod() {
 | 
|---|
| 120 |   OBSERVE;
 | 
|---|
| 121 |   int i = 0;
 | 
|---|
| 122 |   i++;
 | 
|---|
| 123 |   subObservable->changeMethod();
 | 
|---|
| 124 | }
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /************* NotificationObservable **********/
 | 
|---|
| 127 | 
 | 
|---|
| 128 | NotificationObservable::NotificationObservable() :
 | 
|---|
| 129 |   Observable("NotificationObservable"),
 | 
|---|
| 130 |   notification1(new Notification(this)),
 | 
|---|
| 131 |   notification2(new Notification(this))
 | 
|---|
| 132 | {}
 | 
|---|
| 133 | 
 | 
|---|
| 134 | NotificationObservable::~NotificationObservable(){
 | 
|---|
| 135 |   delete notification1;
 | 
|---|
| 136 |   delete notification2;
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | void NotificationObservable::operation1(){
 | 
|---|
| 140 |   OBSERVE;
 | 
|---|
| 141 |   NOTIFY(notification1);
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | void NotificationObservable::operation2(){
 | 
|---|
| 145 |   OBSERVE;
 | 
|---|
| 146 |   NOTIFY(notification2);
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | /*********** NotificationObserver **************/
 | 
|---|
| 150 | 
 | 
|---|
| 151 | NotificationObserver::NotificationObserver(Notification_ptr notification) :
 | 
|---|
| 152 |   Observer("NotificationObserver"),
 | 
|---|
| 153 |   requestedNotification(notification),
 | 
|---|
| 154 |   wasNotified(false)
 | 
|---|
| 155 | {}
 | 
|---|
| 156 | 
 | 
|---|
| 157 | void NotificationObserver::update(Observable*){}
 | 
|---|
| 158 | void NotificationObserver::subjectKilled(Observable*){}
 | 
|---|
| 159 | void 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 | 
 | 
|---|
| 166 | ObservableSet::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 | 
 | 
|---|
| 177 | ObservableSet::~ObservableSet(){
 | 
|---|
| 178 |   set::iterator iter;
 | 
|---|
| 179 |   for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
 | 
|---|
| 180 |     delete (*iter);
 | 
|---|
| 181 |   }
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | ObservableSet::iterator ObservableSet::begin(){
 | 
|---|
| 185 |   return iterator(theSet.begin(),this);
 | 
|---|
| 186 | }
 | 
|---|
| 187 | 
 | 
|---|
| 188 | ObservableSet::iterator ObservableSet::end(){
 | 
|---|
| 189 |   return iterator(theSet.end(),this);
 | 
|---|
| 190 | }
 | 
|---|
| 191 | 
 | 
|---|
| 192 | /************** ObservableMap ******************/
 | 
|---|
| 193 | 
 | 
|---|
| 194 | ObservableMap::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 | 
 | 
|---|
| 205 | ObservableMap::~ObservableMap(){
 | 
|---|
| 206 |   set::iterator iter;
 | 
|---|
| 207 |   for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
 | 
|---|
| 208 |     delete iter->second;
 | 
|---|
| 209 |   }
 | 
|---|
| 210 | }
 | 
|---|
| 211 | 
 | 
|---|
| 212 | ObservableMap::iterator ObservableMap::begin(){
 | 
|---|
| 213 |   return iterator(theSet.begin(),this);
 | 
|---|
| 214 | }
 | 
|---|
| 215 | 
 | 
|---|
| 216 | ObservableMap::iterator ObservableMap::end(){
 | 
|---|
| 217 |   return iterator(theSet.end(),this);
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|