source: src/Observer/Channels.cpp@ 454bc54

Last change on this file since 454bc54 was 454bc54, checked in by Frederik Heber <heber@…>, 10 years ago

Added new pattern ObservedValue, contrary to Cacheable.

  • essentially updates immediately and caches a value for off-line use, safe- guard with mutexes.
  • added unit test for ObservedValue.
  • factored threeNumbers stub out of CacheableUnitTest, also used the stub in ObservedValueUnitTest.
  • we may now signOn() to Notification with a priority.
  • Cacheable can also update now via notification channels not just for global updates.
  • Observable can now also instantiate the channels directly if given a list. This was necessary as Cacheable or ObservedValue are instantiated in the constructor, too, and if listening to channels, these must already be present.
  • FIX: Channels::addChannels() used NDEBUG wrong way round.
  • ObservedValue::get() returns reference.
  • Property mode set to 100644
File size: 2.0 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 * Channels.cpp
10 *
11 * Created on: Dec 1, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include "CodePatterns/Assert.hpp"
23
24#include "CodePatterns/Observer/Channels.hpp"
25#include "CodePatterns/Observer/Notification.hpp"
26
27
28Channels::Channels()
29{}
30
31Channels::~Channels()
32{
33 // free all present Notifications
34 for(NotificationTypetoRefMap::iterator iter = ChannelMap.begin();
35 !ChannelMap.empty(); iter = ChannelMap.begin()) {
36 removeChannel(iter->first);
37 }
38}
39
40void Channels::addChannel(size_t no)
41{
42#ifndef NDEBUG
43 NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no);
44 ASSERT(iter == ChannelMap.end(),
45 "Channels::addChannel() - channel "+toString(int(no))+" is already present in ChannelMap.");
46#endif
47 ChannelMap.insert( std::make_pair(no, new Notification(no)) );
48}
49
50void Channels::removeChannel(size_t no)
51{
52 NotificationTypetoRefMap::iterator iter = ChannelMap.find(no);
53 ASSERT(iter != ChannelMap.end(),
54 "Channels::removeChannel() - channel "+toString(int(no))+" not present in ChannelMap.");
55 delete iter->second;
56 ChannelMap.erase(iter);
57}
58
59void Channels::subjectKilled(Observable * const publisher)
60{
61 for(NotificationTypetoRefMap::iterator iter = ChannelMap.begin();
62 iter != ChannelMap.end();++iter) {
63 iter->second->subjectKilled(publisher);
64 }
65}
66
67Notification_ptr Channels::getChannel(size_t no) const
68{
69 NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no);
70 ASSERT(iter != ChannelMap.end(),
71 "Channels::getChannel() - channel "+toString(int(no))+" not present in ChannelMap.");
72 return iter->second;
73}
74
75size_t Channels::getType(Notification_ptr channel) const
76{
77 return channel->getChannelNo();
78}
79
Note: See TracBrowser for help on using the repository browser.