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