00001 00002 // 00003 // $Id: Observer.h,v 1.3 2003/06/02 21:59:59 scharver Exp $ 00004 // 00005 // Author: Chris Scharver 00006 // Email: scharver@evl.uic.edu 00007 // Copyright (c) 2003 Electronic Visualization Laboratory, 00008 // University of Illinois at Chicago 00009 // 00010 // Permission is hereby granted, free of charge, to any person 00011 // obtaining a copy of this software and associated documentation 00012 // files (the "Software"), to deal in the Software without 00013 // restriction, including without limitation the rights to use, copy, 00014 // modify, merge, publish, distribute, sublicense, and/or sell copies 00015 // of the Software, and to permit persons to whom the Software is 00016 // furnished to do so, subject to the following conditions: 00017 // 00018 // The above copyright notice and this permission notice shall be 00019 // included in all copies or substantial portions of the Software. 00020 // 00021 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00022 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00023 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00024 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00025 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00026 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00027 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00028 // DEALINGS IN THE SOFTWARE. 00029 // 00031 00032 #ifndef OBSERVER_H 00033 #define OBSERVER_H 00034 00035 #include <list.h> 00036 00037 class Subject; 00038 00044 class Observer 00045 { 00046 public: 00047 virtual ~Observer() {} 00048 00050 virtual void update(Subject* theChangedSubject) = 0; 00051 00052 protected: 00053 Observer() {} 00054 }; 00055 00062 class Subject 00063 { 00064 public: 00065 virtual ~Subject() {} 00066 00068 inline virtual void attach(Observer*); 00069 00071 inline virtual void detach(Observer*); 00072 00074 inline virtual void notify(); 00075 00076 protected: 00077 Subject() {} 00078 private: 00079 list<Observer*> m_observers; 00080 }; 00081 00082 inline void 00083 Subject::attach(Observer* o) 00084 { 00085 if (o) 00086 m_observers.push_back(o); 00087 } 00088 00089 inline void 00090 Subject::detach(Observer* o) 00091 { 00092 if (o) 00093 m_observers.remove(o); 00094 } 00095 00096 inline void 00097 Subject::notify() 00098 { 00099 for (list<Observer*>::iterator i = m_observers.begin(); 00100 i != m_observers.end(); i++) { 00101 (*i)->update(this); 00102 } 00103 } 00104 00105 #endif