Changeset dd7c44
- Timestamp:
- Oct 30, 2015, 11:43:20 AM (10 years ago)
- Children:
- 6e2f3b
- Parents:
- 1f96ec
- git-author:
- Frederik Heber <heber@…> (07/10/15 09:44:07)
- git-committer:
- Frederik Heber <heber@…> (10/30/15 11:43:20)
- Location:
- src
- Files:
-
- 3 edited
-
CodePatterns/Observer/Observable.hpp (modified) (3 diffs)
-
Observer/Observable.cpp (modified) (5 diffs)
-
Observer/unittests/stubs/ObserverStub.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodePatterns/Observer/Observable.hpp
r1f96ec rdd7c44 45 45 */ 46 46 class Observable : public Observer { 47 //!> grant Relay access to NotificationChannels as we know it uses the mutex as well 48 friend class Relay; 47 49 public: 48 50 //!> typedef for a vector of channels … … 134 136 135 137 protected: 138 139 static void insertNotificationChannel( std::pair<Observable*, Channels *> _pair); 140 static void eraseNotificationChannel(Observable * const _target); 141 static bool isNotificationChannelPresent(const Observable * const _target); 142 static const Channels *getNotificationChannels(const Observable * const _target); 143 static Notification_ptr getNotificationChannel(const Observable * const _target, const size_t _no); 144 145 private: 136 146 137 147 typedef std::map<Observable*, Channels *> ChannelMap; … … 188 198 #define PASTE_HELPER(a,b) a ## b 189 199 #define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this) 190 #define NOTIFY(channelno) do{Observable::enque_notification_internal(this, NotificationChannels[this]->getChannel(channelno));}while(0)200 #define NOTIFY(channelno) do{Observable::enque_notification_internal(this,Observable::getNotificationChannel(this,channelno));}while(0) 191 201 #define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable)) 192 202 -
src/Observer/Observable.cpp
r1f96ec rdd7c44 335 335 Notification_ptr Observable::getChannel(size_t no) const 336 336 { 337 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 338 const ChannelMap::const_iterator iter = NotificationChannels.find(const_cast<Observable *>(this)); 339 const bool status = iter != NotificationChannels.end(); 340 Channels *OurChannel = NULL; 341 if (status) 342 OurChannel = iter->second; 343 ASSERT(status, 344 "Observable::getChannel() - we do not have a channel "+toString(no)+" in NotificationChannels."); 345 ASSERT(OurChannel != NULL, 346 "Observable::getChannel() - observable has no channels."); 347 return OurChannel->getChannel(no); 337 return getNotificationChannel(this, no); 348 338 } 349 339 … … 361 351 } 362 352 { 363 const ChannelMap::const_iterator iter = 364 NotificationChannels.find(const_cast<Observable *>(this)); 365 // if not present, then we have zero observers 366 if (iter != NotificationChannels.end()) 367 for (Channels::NotificationTypetoRefMap::const_iterator channeliter = iter->second->ChannelMap.begin(); 368 channeliter != iter->second->ChannelMap.end(); 353 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 354 const Channels *OurChannels = getNotificationChannels(this); 355 if (OurChannels != NULL) 356 for (Channels::NotificationTypetoRefMap::const_iterator channeliter = OurChannels->ChannelMap.begin(); 357 channeliter != OurChannels->ChannelMap.end(); 369 358 ++channeliter) 370 359 ObserverCount += (channeliter->second)->getNumberOfObservers(); … … 396 385 397 386 if (!_channels.empty()) { 398 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());399 387 Channels *OurChannel = new Channels; 400 NotificationChannels.insert( std::make_pair(static_cast<Observable *>(this), OurChannel) );401 388 // add instance for each notification type 402 389 for (channels_t::const_iterator iter = _channels.begin(); 403 390 iter != _channels.end(); ++iter) 404 391 OurChannel->addChannel(*iter); 392 insertNotificationChannel( std::make_pair(static_cast<Observable *>(this), OurChannel) ); 405 393 } 406 394 } … … 436 424 437 425 // also kill instance in static Channels map if present 438 { 439 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 440 ChannelMap::iterator iter = NotificationChannels.find(static_cast<Observable *>(this)); 441 if (iter != NotificationChannels.end()) { 442 iter->second->subjectKilled(static_cast<Observable *>(this)); 443 delete iter->second; 444 NotificationChannels.erase(iter); 445 } 446 } 426 eraseNotificationChannel(this); 447 427 } 448 428 … … 453 433 return channels; 454 434 } 435 436 void Observable::insertNotificationChannel(std::pair<Observable*, Channels *> _pair) 437 { 438 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 439 NotificationChannels.insert(_pair); 440 } 441 442 void Observable::eraseNotificationChannel(Observable * const _target) 443 { 444 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 445 ChannelMap::iterator iter = NotificationChannels.find(static_cast<Observable *>(_target)); 446 if (iter != NotificationChannels.end()) { 447 iter->second->subjectKilled(static_cast<Observable *>(_target)); 448 delete iter->second; 449 NotificationChannels.erase(iter); 450 } 451 } 452 453 bool Observable::isNotificationChannelPresent(const Observable * const _target) 454 { 455 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 456 ChannelMap::const_iterator iter = 457 NotificationChannels.find(const_cast<Observable * const>(_target)); 458 return iter != NotificationChannels.end(); 459 } 460 461 462 const Channels* Observable::getNotificationChannels(const Observable * const _target) 463 { 464 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 465 ChannelMap::const_iterator iter = 466 NotificationChannels.find(const_cast<Observable * const>(_target)); 467 if (iter != NotificationChannels.end()) 468 return iter->second; 469 else 470 return NULL; 471 } 472 473 Notification_ptr Observable::getNotificationChannel(const Observable * const _target, const size_t _no) 474 { 475 boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); 476 ChannelMap::const_iterator iter = 477 NotificationChannels.find(const_cast<Observable * const>(_target)); 478 ASSERT(iter != NotificationChannels.end(), 479 "Observable::getNotificationChannel() - could not find channel for target " 480 +toString(_target)+"."); 481 return iter->second->getChannel(_no); 482 } -
src/Observer/unittests/stubs/ObserverStub.cpp
r1f96ec rdd7c44 136 136 { 137 137 Channels *OurChannel = new Channels(); 138 NotificationChannels.insert( std::make_pair(this, OurChannel) );138 Observable::insertNotificationChannel( std::make_pair(this, OurChannel) ); 139 139 OurChannel->addChannel(Operation1Notify); 140 140 OurChannel->addChannel(Operation2Notify); … … 143 143 NotificationObservable::~NotificationObservable() 144 144 { 145 NotificationChannels.erase(this);145 Observable::eraseNotificationChannel(this); 146 146 } 147 147 … … 245 245 { 246 246 Channels *OurChannel = new Channels(); 247 NotificationChannels.insert( std::make_pair(this, OurChannel) );247 Observable::insertNotificationChannel( std::make_pair(this, OurChannel) ); 248 248 OurChannel->addChannel(NotificationObservable::Operation1Notify); 249 249 OurChannel->addChannel(NotificationObservable::Operation2Notify);
Note:
See TracChangeset
for help on using the changeset viewer.
