|
solarpowerlog trunk
|
00001 /* ---------------------------------------------------------------------------- 00002 solarpowerlog 00003 Copyright (C) 2009 Tobias Frost 00004 00005 This file is part of solarpowerlog. 00006 00007 Solarpowerlog is free software; However, it is dual-licenced 00008 as described in the file "COPYING". 00009 00010 For this file (IObserverSubject.cpp), the license terms are: 00011 00012 You can redistribute it and/or modify it under the terms of the GNU 00013 General Public License as published by the Free Software Foundation; either 00014 version 3 of the License, or (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, but 00017 WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 Lesser General Public License for more details. 00020 00021 You should have received a copy of the GNU Library General Public 00022 License along with this proramm; if not, see 00023 <http://www.gnu.org/licenses/>. 00024 ---------------------------------------------------------------------------- 00025 */ 00026 00035 #ifdef HAVE_CONFIG_H 00036 #include "config.h" 00037 #endif 00038 00039 #include "IObserverSubject.h" 00040 #include "IObserverObserver.h" 00041 #include <list> 00042 00043 using namespace std; 00044 00045 void IObserverSubject::Subscribe( class IObserverObserver *observer ) 00046 { 00047 if (!CheckSubscription(observer)) 00048 listobservers.push_back(observer); 00049 } 00050 00051 void IObserverSubject::UnSubscribe( class IObserverObserver *observer ) 00052 { 00053 if (CheckSubscription(observer)) 00054 listobservers.remove(observer); 00055 } 00056 00057 void IObserverSubject::Notify( void ) 00058 { 00059 std::list<class IObserverObserver *>::iterator i; 00060 for (i = listobservers.begin(); i != listobservers.end(); ++i) { 00061 (*i)->Update(this); 00062 } 00063 } 00064 00065 unsigned int IObserverSubject::GetNumSubscribers( void ) 00066 { 00067 return listobservers.size(); 00068 } 00069 00070 bool IObserverSubject::CheckSubscription( class IObserverObserver *observer ) 00071 { 00072 std::list<class IObserverObserver *>::iterator i; 00073 for (i = listobservers.begin(); i != listobservers.end(); ++i) { 00074 if ((*i) == observer) 00075 return true; 00076 } 00077 00078 return false; 00079 } 00080 00081 void IObserverSubject::SetSubscription( class IObserverObserver *observer, 00082 bool subscribe ) 00083 { 00084 if (subscribe) 00085 Subscribe(observer); 00086 else 00087 UnSubscribe(observer); 00088 } 00089 00090 IObserverSubject::IObserverSubject() 00091 { 00092 // TODO Auto-generated constructor stub 00093 00094 } 00095 00096 IObserverSubject::~IObserverSubject() 00097 { 00098 listobservers.empty(); 00099 // TODO Auto-generated destructor stub 00100 }