include/observ.h

Go to the documentation of this file.
00001 /********************************************************************************
00002  *  Neural Network Framework.                                                   *
00003  *  Copyright (C) 2005-2008 Gianluca Massera <emmegian@yahoo.it>                     *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 // --- You can't include it directly
00021 #ifndef TYPES_INCLUDES
00022 #error "You can't include observ.h directly; Instead, You have to include types.h"
00023 // --- follow define avoid to get a lot of understandable error !
00024 #define OBSERV_H
00025 #endif
00026 
00027 #ifndef OBSERV_H
00028 #define OBSERV_H
00029 
00034 #include <list>
00035 #include <algorithm>
00036 
00037 
00038 namespace nnfw {
00039 
00046 class NNFW_API NotifyEvent {
00047 public:
00051     NotifyEvent( int type = 0 ) : etype(type) { /* Nothing to do */ };
00052 
00054 
00056 
00058     int type() const {
00059         return etype;
00060     };
00062 
00063 protected:
00065     int etype;
00066 };
00067 
00073 class NNFW_API Observer {
00074 public:
00077 
00080     virtual ~Observer() { /* -- Nothing to do -- */ };
00081 
00083 
00085 
00088     virtual void notify( const NotifyEvent& ) = 0;
00090 };
00091 
00092 
00098 class NNFW_API Observable {
00099 public:
00102 
00105     Observable() : observers() {
00106         // --- nothing to do
00107     };
00108 
00110 
00112 
00115     void addObserver( Observer* ob ) {
00116         std::list<Observer*>::iterator it = std::find( observers.begin(), observers.end(), ob );
00117         if ( it != observers.end() ) return;
00118         observers.push_back( ob );
00119     };
00120 
00123     void delObserver( Observer* ob ) {
00124         std::list<Observer*>::iterator it = std::find( observers.begin(), observers.end(), ob );
00125         if ( it == observers.end() ) return;
00126         observers.erase( it );
00127     };
00128 
00131     void notifyAll( const NotifyEvent& event = NotifyEvent() ) {
00132         std::list<Observer*>::iterator it = observers.begin();
00133         std::list<Observer*>::iterator end = observers.end();
00134         while( it != end ) {
00135             (*it)->notify( event );
00136             it++;
00137         }
00138     };
00139 
00141 
00142 private:
00144     std::list<Observer*> observers;
00145 };
00146 
00147 }
00148 
00149 #endif
BerliOS Developer Logo Valid XHTML 1.0 Transitional Valid CSS!