| 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 | Channels *OurChannel = new Channels(this);
|
|---|
| 135 | NotificationChannels.insert( std::make_pair(this, OurChannel) );
|
|---|
| 136 | OurChannel->addChannel(Operation1Notify);
|
|---|
| 137 | OurChannel->addChannel(Operation2Notify);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | NotificationObservable::~NotificationObservable()
|
|---|
| 141 | {
|
|---|
| 142 | NotificationChannels.erase(this);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | void NotificationObservable::operation1(){
|
|---|
| 146 | OBSERVE;
|
|---|
| 147 | NOTIFY(Operation1Notify);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | void NotificationObservable::operation2(){
|
|---|
| 151 | OBSERVE;
|
|---|
| 152 | NOTIFY(Operation2Notify);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /*********** NotificationObserver **************/
|
|---|
| 156 |
|
|---|
| 157 | NotificationObserver::NotificationObserver(Notification_ptr notification) :
|
|---|
| 158 | Observer("NotificationObserver"),
|
|---|
| 159 | requestedNotification(notification),
|
|---|
| 160 | wasNotified(false)
|
|---|
| 161 | {}
|
|---|
| 162 |
|
|---|
| 163 | NotificationObserver::~NotificationObserver()
|
|---|
| 164 | {}
|
|---|
| 165 |
|
|---|
| 166 | void NotificationObserver::update(Observable*){}
|
|---|
| 167 | void NotificationObserver::subjectKilled(Observable*){}
|
|---|
| 168 | void 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 |
|
|---|
| 175 | ObservableSet::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 |
|
|---|
| 186 | ObservableSet::~ObservableSet(){
|
|---|
| 187 | set::iterator iter;
|
|---|
| 188 | for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
|
|---|
| 189 | delete (*iter);
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | ObservableSet::iterator ObservableSet::begin(){
|
|---|
| 194 | return iterator(theSet.begin(),this);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | ObservableSet::iterator ObservableSet::end(){
|
|---|
| 198 | return iterator(theSet.end(),this);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /************** ObservableMap ******************/
|
|---|
| 202 |
|
|---|
| 203 | ObservableMap::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 |
|
|---|
| 214 | ObservableMap::~ObservableMap(){
|
|---|
| 215 | set::iterator iter;
|
|---|
| 216 | for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
|
|---|
| 217 | delete iter->second;
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | ObservableMap::iterator ObservableMap::begin(){
|
|---|
| 222 | return iterator(theSet.begin(),this);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | ObservableMap::iterator ObservableMap::end(){
|
|---|
| 226 | return iterator(theSet.end(),this);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|