Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

SubjectObserver.h

Go to the documentation of this file.
00001 // SubjectObserver.h: interface for the Subject and Observer classes
00002 //
00004 
00005 // Adapted from the Subject-Observer section of 
00006 // "Design Patterns, Elements of Reusable Object-Oriented Software"
00007 // by E. Gamma, R. Helm, R. Johnson, and J. Vlissides, 
00008 // Addison Wesley, Massachusetts, 1995
00009 // ISBN 0-201-63361-2
00010 
00011 
00012 #ifndef OBSERVER_H
00013 #define OBSERVER_H
00014 
00015 #ifndef SWIG
00016 #if _MSC_VER > 1000
00017 #pragma once
00018 #endif // _MSC_VER > 1000
00019 #endif
00020 
00021 #include <list>
00022 // #include "Introspective.h"
00023 
00024 class Subject;
00025 
00026 // class Observer : public Introspective
00027 class Observer
00028 {
00029 public:
00030         virtual ~Observer() {};
00031         virtual void Update(Subject *theChangedSubject) = 0;
00032 
00033 protected:
00034         Observer()
00035         {
00036 /*              // Drew's registration with MOP: allows Get and Set functions to be called
00037                 // from dialog boxes, in serialization, and for copying
00038                 if (NotYetRegistered("Observer",typeid(*this))) 
00039                 {
00040                         AddType("Observer",typeid(*this));
00041                 } */
00042         } 
00043 };
00044 
00045 class Subject  
00046 {
00047 public:
00048         virtual ~Subject() 
00049         {
00050                 delete _observers;
00051         }
00052         virtual void  Attach(Observer* o) 
00053         {
00054                 _observers->push_back(o);
00055         }
00056         virtual void Detach(Observer* o) 
00057         {
00058                 _observers->remove(o);
00059         }
00060         virtual void Notify() 
00061         {
00062                 std::list<Observer*>::iterator i = _observers->begin();
00063                 while (i != _observers->end()) 
00064                 {
00065                         (*i)->Update(this);
00066                         i++;
00067                 }
00068         }
00069 
00070 protected:
00071 #ifndef SWIG // XXX SWIG doesn't support standard template libraries
00072         Subject() 
00073         {
00074                 _observers = new std::list<Observer*>;
00075         }
00076 #endif // SWIG
00077 private:
00078 #ifndef SWIG // XXX SWIG doesn't support standard template libraries
00079         std::list<Observer*> *_observers;
00080 #endif // SWIG
00081 };
00082 
00083 #endif // !defined(OBSERVER_H)

Generated on Mon Nov 3 09:38:17 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002