source: ThirdParty/CodePatterns/src/Observer/unittests/stubs/ObserverStub.cpp@ c8cb0d

Candidate_v1.7.1 stable
Last change on this file since c8cb0d was c8cb0d, checked in by Frederik Heber <frederik.heber@…>, 12 days ago

Streamlines channel creation in Observables.

  • CodePatterns is now version 1.3.4.
  • we no longer need to add the channels manually in the cstor of a class that derives from Observable. Instead, we just need to pass the maximum number of channels (as they are typically enumerated anyway) and they are generated and added.
  • added mutex protection when inserting.
  • adjusted class Relay to forward similar convenience cstors.
  • adjusted all call sites in molecuilder.
  • Property mode set to 100644
File size: 6.2 KB
Line 
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
31UpdateCountObserver::UpdateCountObserver() :
32 Observer("UpdateCountObserver"),
33 updates(0)
34{};
35
36UpdateCountObserver::~UpdateCountObserver()
37{}
38
39void UpdateCountObserver::update(Observable *publisher){
40 updates++;
41}
42
43void UpdateCountObserver::subjectKilled(Observable *publisher) {
44}
45
46/*************** SimpleObservable **************/
47
48SimpleObservable::SimpleObservable() :
49 Observable("SimpleObservable")
50{}
51
52void SimpleObservable::changeMethod() {
53 OBSERVE;
54 int i = 0;
55 i++;
56}
57
58/************** CallObservable *****************/
59
60CallObservable::CallObservable() :
61 Observable("CallObservable")
62{}
63
64void CallObservable::changeMethod1() {
65 OBSERVE;
66 int i = 0;
67 i++;
68}
69
70void CallObservable::changeMethod2() {
71 OBSERVE;
72 int i = 0;
73 i++;
74 changeMethod1();
75}
76
77/************* BlockObservable *****************/
78
79BlockObservable::BlockObservable() :
80 Observable("BlockObservable")
81{}
82
83void BlockObservable::changeMethod1(){
84 OBSERVE;
85 // test if we report correctly as blocked
86 CPPUNIT_ASSERT(isBlocked());
87}
88
89void BlockObservable::changeMethod2(){
90 OBSERVE;
91 internalMethod1();
92 internalMethod2();
93}
94
95void BlockObservable::internalMethod1(){
96 // we did not block, but our caller did...
97 // see if this is found
98 CPPUNIT_ASSERT(isBlocked());
99}
100
101void 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
108void BlockObservable::noChangeMethod(){
109 // No Block introduced here
110 // reported correctely?
111 CPPUNIT_ASSERT(!isBlocked());
112}
113
114/*************** SuperObservable ***************/
115
116SuperObservable::SuperObservable():
117 Observable("SuperObservable")
118{
119 subObservable = new SimpleObservable();
120 subObservable->signOn(this);
121}
122SuperObservable::~SuperObservable(){
123 delete subObservable;
124}
125void SuperObservable::changeMethod() {
126 OBSERVE;
127 int i = 0;
128 i++;
129 subObservable->changeMethod();
130}
131
132/************* NotificationObservable **********/
133
134NotificationObservable::NotificationObservable() :
135 Observable("NotificationObservable", { Operation1Notify, Operation2Notify })
136{}
137
138NotificationObservable::~NotificationObservable()
139{
140 Observable::eraseNotificationChannel(this);
141}
142
143void NotificationObservable::operation1(){
144 OBSERVE;
145 NOTIFY(Operation1Notify);
146}
147
148void NotificationObservable::operation2(){
149 OBSERVE;
150 NOTIFY(Operation2Notify);
151}
152
153/*********** NotificationObserver **************/
154
155NotificationObserver::NotificationObserver(Notification_ptr notification) :
156 Observer("NotificationObserver"),
157 requestedNotification(notification),
158 wasNotified(false)
159{}
160
161NotificationObserver::~NotificationObserver()
162{}
163
164void NotificationObserver::update(Observable*){}
165void NotificationObserver::subjectKilled(Observable*){}
166void NotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
167 CPPUNIT_ASSERT(requestedNotification==notification);
168 wasNotified = true;
169}
170
171/**************** ObservableSet ****************/
172
173ObservableSet::ObservableSet(int _num) :
174 Observable("ObservableCollection"),
175 num(_num)
176{
177 for(int i=0; i<num; ++i){
178 SimpleObservable *content = new SimpleObservable();
179 content->signOn(this);
180 theSet.insert(content);
181 }
182}
183
184ObservableSet::~ObservableSet(){
185 set::iterator iter;
186 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
187 delete (*iter);
188 }
189}
190
191ObservableSet::iterator ObservableSet::begin(){
192 return iterator(theSet.begin(),this);
193}
194
195ObservableSet::iterator ObservableSet::end(){
196 return iterator(theSet.end(),this);
197}
198
199/************** ObservableMap ******************/
200
201ObservableMap::ObservableMap(int _num) :
202 Observable("ObservableCollection"),
203 num(_num)
204{
205 for(int i=0; i<num; ++i){
206 SimpleObservable *content = new SimpleObservable();
207 content->signOn(this);
208 theSet.insert(std::make_pair(i,content));
209 }
210}
211
212ObservableMap::~ObservableMap(){
213 set::iterator iter;
214 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
215 delete iter->second;
216 }
217}
218
219ObservableMap::iterator ObservableMap::begin(){
220 return iterator(theSet.begin(),this);
221}
222
223ObservableMap::iterator ObservableMap::end(){
224 return iterator(theSet.end(),this);
225}
226
227/****************** Relay *********************/
228
229RelayTest::RelayTest() :
230 Relay(std::string("RelayTest"))
231{}
232
233RelayTest::~RelayTest()
234{}
235
236/************ RelayNotification ***************/
237
238RelayNotification::RelayNotification() :
239 Relay(std::string("RelayTest"), { NotificationObservable::Operation1Notify, NotificationObservable::Operation2Notify })
240{}
241
242RelayNotification::~RelayNotification()
243{}
244
245/************ RelayCountObserver ***************/
246
247RelayCountObserver::RelayCountObserver(const Observable * const _relay) :
248 Observer("RelayCountObserver"),
249 updates(0),
250 relay(_relay)
251{}
252
253RelayCountObserver::~RelayCountObserver()
254{}
255
256void RelayCountObserver::update(Observable *publisher){
257 // check that we are not called by the relay itself
258 CPPUNIT_ASSERT(publisher != relay);
259 updates++;
260}
261
262void RelayCountObserver::subjectKilled(Observable *publisher) {
263}
264
265/********* RelayNotificationObserver ***********/
266
267RelayNotificationObserver::RelayNotificationObserver(const Observable * const _relay) :
268 Observer("RelayNotificationObserver"),
269 wasNotified(false),
270 relay(_relay)
271{}
272
273RelayNotificationObserver::~RelayNotificationObserver()
274{}
275
276void RelayNotificationObserver::update(Observable*){}
277void RelayNotificationObserver::subjectKilled(Observable*){}
278void RelayNotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
279 // check that we are not called by the relay itself
280 CPPUNIT_ASSERT(publisher != relay);
281 wasNotified = true;
282}
283
Note: See TracBrowser for help on using the repository browser.