Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

misc/observer.h

Go to the documentation of this file.
00001 #ifndef MISC_OBSERVER_H
00002 #define MISC_OBSERVER_H
00003 
00004 /*
00005 
00006   GLT OpenGL C++ Toolkit (LGPL)
00007   Copyright (C) 2000-2002 Nigel Stewart  
00008 
00009   Email: nigels.com@gmail.com   
00010   WWW:   http://www.nigels.com/glt/
00011 
00012   This library is free software; you can redistribute it and/or
00013   modify it under the terms of the GNU Lesser General Public
00014   License as published by the Free Software Foundation; either
00015   version 2.1 of the License, or (at your option) any later version.
00016 
00017   This library is distributed in the hope that it will be useful,
00018   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020   Lesser General Public License for more details.
00021 
00022   You should have received a copy of the GNU Lesser General Public
00023   License along with this library; if not, write to the Free Software
00024   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 */
00027 
00033 #include <glt/config.h>
00034 
00035 #include <cassert>
00036 #include <list>
00037 
00038 class GltSubject;
00039 
00048 class GltObserverAbstract 
00049 {
00050     friend class GltSubject;
00051 
00052 protected:
00053 
00055     GltObserverAbstract();
00057     GltObserverAbstract(const GltObserverAbstract &);
00059     virtual ~GltObserverAbstract();
00060 
00062     GltObserverAbstract &operator=(const GltObserverAbstract &);
00063 
00065     void observe(GltSubject &subject);
00067     void forget (GltSubject &subject);
00068 
00070     virtual void OnNotify(const void *subject) = 0;
00071 
00073     std::list<GltSubject *> _subjects;
00074 };
00075 
00094 template<class T>
00095 class GltObserver : public GltObserverAbstract
00096 {
00097 public:
00098 
00100     GltObserver() {}
00102     ~GltObserver() {}
00103 
00105     void observe(T &subject) { GltObserverAbstract::observe(subject); }
00107     void forget (T &subject) { GltObserverAbstract::forget (subject); }
00108     
00110     virtual void OnNotify(const T *subject) = 0;
00111 
00112 protected:
00113 
00114     void OnNotify(const void *subject)
00115     {
00116         OnNotify((const T *) subject);
00117     }
00118 };
00119 
00132 class GltSubject
00133 {
00134     friend class GltObserverAbstract;
00135 
00136 public:
00138     GltSubject();
00140     GltSubject(const GltSubject &);
00142     ~GltSubject();
00143 
00145     GltSubject &operator=(const GltSubject &);
00146 
00148     void notify(const void *object);
00149 
00150 private:
00151 
00152     std::list<GltObserverAbstract *> _observers;
00153 };
00154 
00155 #define NOTIFY notify(this)
00156 
00157 #endif
00158 

Generated on Tue Nov 5 11:11:05 2002 for GLT by doxygen1.2.18