/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * Channels.cpp * * Created on: Dec 1, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include #include "CodePatterns/Assert.hpp" #include "CodePatterns/Observer/Channels.hpp" #include "CodePatterns/Observer/Notification.hpp" Channels::Channels() {} Channels::~Channels() { boost::recursive_mutex::scoped_lock guard(ChannelLock); // free all present Notifications for(NotificationTypetoRefMap::iterator iter = ChannelMap.begin(); !ChannelMap.empty(); iter = ChannelMap.begin()) { removeChannel(iter->first); } } void Channels::addChannel(size_t no) { boost::recursive_mutex::scoped_lock guard(ChannelLock); #ifndef NDEBUG NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no); ASSERT(iter == ChannelMap.end(), "Channels::addChannel() - channel "+toString(int(no))+" is already present in ChannelMap."); #endif ChannelMap.insert( std::make_pair(no, new Notification(no)) ); } void Channels::removeChannel(size_t no) { boost::recursive_mutex::scoped_lock guard(ChannelLock); NotificationTypetoRefMap::iterator iter = ChannelMap.find(no); ASSERT(iter != ChannelMap.end(), "Channels::removeChannel() - channel "+toString(int(no))+" not present in ChannelMap."); delete iter->second; ChannelMap.erase(iter); } void Channels::subjectKilled(Observable * const publisher) { boost::recursive_mutex::scoped_lock guard(ChannelLock); for(NotificationTypetoRefMap::iterator iter = ChannelMap.begin(); iter != ChannelMap.end();++iter) { iter->second->subjectKilled(publisher); } } Notification_ptr Channels::getChannel(size_t no) const { boost::recursive_mutex::scoped_lock guard(ChannelLock); NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no); ASSERT(iter != ChannelMap.end(), "Channels::getChannel() - channel "+toString(int(no))+" not present in ChannelMap."); return iter->second; } size_t Channels::getType(Notification_ptr channel) const { boost::recursive_mutex::scoped_lock guard(ChannelLock); return channel->getChannelNo(); }