Changeset 74e0f7
- Timestamp:
- Sep 1, 2011, 11:07:47 AM (14 years ago)
- Children:
- 9e776f
- Parents:
- 451f17
- git-author:
- Frederik Heber <heber@…> (09/01/11 11:02:49)
- git-committer:
- Frederik Heber <heber@…> (09/01/11 11:07:47)
- Location:
- src/Patterns
- Files:
-
- 5 edited
-
Observer.cpp (modified) (3 diffs)
-
Observer.hpp (modified) (6 diffs)
-
unittests/ObserverUnitTest.cpp (modified) (2 diffs)
-
unittests/stubs/ObserverStub.cpp (modified) (3 diffs)
-
unittests/stubs/ObserverStub.hpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Patterns/Observer.cpp
r451f17 r74e0f7 271 271 } 272 272 273 Notification_ptr Observable::getChannel(size_t no) const 274 { 275 ASSERT(NotificationChannels != NULL, 276 "Observable::getChannel() - observable has no channels."); 277 return NotificationChannels->getChannel(no); 278 } 279 273 280 /** Handles sub-observables that just got killed 274 281 * when an sub-observerable dies we usually don't need to do anything … … 350 357 351 358 Notification::Notification(Observable *_owner) : 352 owner(_owner) 359 owner(_owner), channelno(-1) 360 {} 361 362 Notification::Notification(Observable *_owner, size_t _channelno) : 363 owner(_owner), channelno(_channelno) 353 364 {} 354 365 … … 369 380 } 370 381 } 382 383 Channels::Channels(Observable *_owner) : 384 owner(_owner) 385 {} 386 387 Channels::~Channels() 388 { 389 // free all present Notifications 390 for(NotificationTypetoRefMap::iterator iter = ChannelMap.begin(); 391 !ChannelMap.empty(); iter = ChannelMap.begin()) { 392 delete iter->second; 393 ChannelMap.erase(iter); 394 } 395 } 396 397 void Channels::addChannel(size_t no) 398 { 399 NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no); 400 ASSERT(iter == ChannelMap.end(), 401 "Channels::addChannel() - channel "+toString(int(no))+" is already present in ChannelMap."); 402 ChannelMap.insert( std::make_pair(no, new Notification(owner, no)) ); 403 } 404 405 void Channels::removeChannel(size_t no) 406 { 407 NotificationTypetoRefMap::iterator iter = ChannelMap.find(no); 408 ASSERT(iter != ChannelMap.end(), 409 "Channels::removeChannel() - channel "+toString(int(no))+" not present in ChannelMap."); 410 delete iter->second; 411 ChannelMap.erase(iter); 412 } 413 414 Notification_ptr Channels::getChannel(size_t no) const 415 { 416 NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no); 417 ASSERT(iter != ChannelMap.end(), 418 "Channels::getChannel() - channel "+toString(int(no))+" not present in ChannelMap."); 419 return iter->second; 420 } 421 422 size_t Channels::getType(Notification_ptr channel) const 423 { 424 return channel->getChannelNo(); 425 } 426 371 427 372 428 #ifdef LOG_OBSERVER -
src/Patterns/Observer.hpp
r451f17 r74e0f7 40 40 #endif 41 41 42 class Channels; 43 class Notification; 42 44 class Observable; 43 class Notification;44 45 45 46 // Pointers to notifications are used for unique identification … … 145 146 virtual bool isBlocked(); 146 147 148 Notification_ptr getChannel(size_t no) const; 149 147 150 protected: 148 151 virtual void update(Observable *publisher); … … 164 167 165 168 static void enque_notification_internal(Observable *publisher, Notification_ptr notification); 169 170 Channels *NotificationChannels; 166 171 167 172 private: … … 192 197 }; 193 198 199 /** Notifications are sort of new channels of an Observable. 200 * Via the NOTIFY() macro updates can be transmitted in a specific channel. 201 * Observers can subscribe to Notification in much the same way as they can to 202 * the Observable itself. Usually, Notifications 203 */ 194 204 class Notification { 195 205 friend class Observable; 206 friend class Channels; 196 207 public: 197 208 Notification(Observable *_owner); 209 Notification(Observable *_owner, size_t _channelno); 198 210 virtual ~Notification(); 211 212 size_t getChannelNo() const { return channelno; } 199 213 protected: 200 214 … … 206 220 Observable * const owner; 207 221 std::set<Observer*> targets; 222 size_t channelno; 223 }; 224 225 /** Channels aggregate all possible Notifications of an Observable. 226 * 227 * Usually, one implements an enumeration of the channel number which is 228 * visible to the outside only. 229 * 230 */ 231 class Channels { 232 public: 233 Channels(Observable *_owner); 234 virtual ~Channels(); 235 236 void addChannel(size_t no); 237 238 Notification_ptr getChannel(size_t no) const; 239 size_t getType(Notification_ptr channel) const; 240 241 protected: 242 void removeChannel(size_t no); 243 244 private: 245 Observable * const owner; 246 247 typedef std::map< size_t, Notification_ptr> NotificationTypetoRefMap; 248 249 NotificationTypetoRefMap ChannelMap; 208 250 }; 209 251 … … 245 287 #define PASTE_HELPER(a,b) a ## b 246 288 #define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this) 247 #define NOTIFY( notification) do{Observable::enque_notification_internal(this,notification);}while(0)289 #define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels->getChannel(channelno));}while(0) 248 290 #define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable)) 249 291 -
src/Patterns/unittests/ObserverUnitTest.cpp
r451f17 r74e0f7 61 61 observer4 = new UpdateCountObserver(); 62 62 63 notificationObserver1 = new NotificationObserver(notificationObservable->notification1); 64 notificationObserver2 = new NotificationObserver(notificationObservable->notification2); 63 notificationObserver1 = new NotificationObserver( 64 notificationObservable->getChannel(NotificationObservable::Operation1Notify)); 65 notificationObserver2 = new NotificationObserver( 66 notificationObservable->getChannel(NotificationObservable::Operation2Notify)); 65 67 } 66 68 … … 157 159 158 160 void ObserverTest::doesNotifyTest(){ 161 CPPUNIT_ASSERT_EQUAL( 162 (size_t)NotificationObservable::Operation1Notify, 163 notificationObservable->getChannel(NotificationObservable::Operation1Notify)->getChannelNo()); 164 CPPUNIT_ASSERT_EQUAL( 165 (size_t)NotificationObservable::Operation2Notify, 166 notificationObservable->getChannel(NotificationObservable::Operation2Notify)->getChannelNo()); 159 167 notificationObservable->signOn(notificationObserver1, 160 notificationObservable-> notification1);168 notificationObservable->getChannel(NotificationObservable::Operation1Notify)); 161 169 notificationObservable->signOn(notificationObserver2, 162 notificationObservable-> notification2);170 notificationObservable->getChannel(NotificationObservable::Operation2Notify)); 163 171 164 172 notificationObservable->operation1(); -
src/Patterns/unittests/stubs/ObserverStub.cpp
r451f17 r74e0f7 31 31 {}; 32 32 33 UpdateCountObserver::~UpdateCountObserver() 34 {} 35 33 36 void UpdateCountObserver::update(Observable *publisher){ 34 37 updates++; … … 127 130 128 131 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; 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; 137 142 } 138 143 139 144 void NotificationObservable::operation1(){ 140 145 OBSERVE; 141 NOTIFY( notification1);146 NOTIFY(Operation1Notify); 142 147 } 143 148 144 149 void NotificationObservable::operation2(){ 145 150 OBSERVE; 146 NOTIFY( notification2);151 NOTIFY(Operation2Notify); 147 152 } 148 153 … … 153 158 requestedNotification(notification), 154 159 wasNotified(false) 160 {} 161 162 NotificationObserver::~NotificationObserver() 155 163 {} 156 164 -
src/Patterns/unittests/stubs/ObserverStub.hpp
r451f17 r74e0f7 15 15 public: 16 16 UpdateCountObserver(); 17 virtual ~UpdateCountObserver(); 18 17 19 void update(Observable *publisher); 18 20 void subjectKilled(Observable *publisher); … … 52 54 public: 53 55 SuperObservable(); 54 ~SuperObservable();56 virtual ~SuperObservable(); 55 57 56 58 void changeMethod(); … … 62 64 public: 63 65 NotificationObservable(); 64 ~NotificationObservable(); 66 virtual ~NotificationObservable(); 67 68 enum NotificationType { 69 Operation1Notify = 0, 70 Operation2Notify = 1 71 }; 65 72 66 73 void operation1(); 67 74 void operation2(); 68 69 Notification_ptr notification1;70 Notification_ptr notification2;71 75 }; 72 76 … … 74 78 public: 75 79 NotificationObserver(Notification_ptr notification); 80 virtual ~NotificationObserver(); 76 81 77 82 void update(Observable*); … … 91 96 92 97 ObservableSet(int _num); 93 ~ObservableSet();98 virtual ~ObservableSet(); 94 99 95 100 iterator begin(); … … 109 114 110 115 ObservableMap(int _num); 111 ~ObservableMap();116 virtual ~ObservableMap(); 112 117 113 118 iterator begin();
Note:
See TracChangeset
for help on using the changeset viewer.
