Wiselib
wiselib.stable/util/base_classes/state_callback_base.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib 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 Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef __UTIL_BASECLASSES_STATECALLBACK_BASE_H__
00020 #define __UTIL_BASECLASSES_STATECALLBACK_BASE_H__
00021 
00022 #include "util/delegates/delegate.hpp"
00023 #include "util/pstl/vector_static.h"
00024 #include "config.h"
00025 
00026 namespace wiselib
00027 {
00028 
00035    template<typename OsModel_P,
00036             int MAX_RECEIVERS = STATE_CALLBACK_BASE_MAX_RECEIVERS>
00037    class StateCallbackBase
00038    {
00039    public:
00040       typedef OsModel_P OsModel;
00041 
00042       typedef delegate1<void, int> state_callback_delegate_t;
00043 
00044       typedef vector_static<OsModel, state_callback_delegate_t, MAX_RECEIVERS> CallbackVector;
00045       typedef typename CallbackVector::iterator CallbackVectorIterator;
00046       // --------------------------------------------------------------------
00047       enum ReturnValues
00048       {
00049          SUCCESS = OsModel::SUCCESS
00050       };
00051       // --------------------------------------------------------------------
00052       template<class T, void (T::*TMethod)(int)>
00053       int register_state_callback( T *obj_pnt )
00054       {
00055          if ( callbacks_.empty() )
00056             callbacks_.assign( MAX_RECEIVERS, state_callback_delegate_t() );
00057 
00058          for ( unsigned int i = 0; i < callbacks_.size(); ++i )
00059          {
00060             if ( callbacks_.at(i) == state_callback_delegate_t() )
00061             {
00062                callbacks_.at(i) = state_callback_delegate_t::template from_method<T, TMethod>( obj_pnt );
00063                return i;
00064             }
00065          }
00066 
00067          return -1;
00068       }
00069       // --------------------------------------------------------------------
00070       int unreg_read_callback( int idx )
00071       {
00072          callbacks_.at(idx) = state_callback_delegate_t();
00073          return SUCCESS;
00074       }
00075       // --------------------------------------------------------------------
00076       void notify_receivers( int value )
00077       {
00078          for ( CallbackVectorIterator
00079                   it = callbacks_.begin();
00080                   it != callbacks_.end();
00081                   ++it )
00082          {
00083             if ( *it != state_callback_delegate_t() )
00084                (*it)( value );
00085          }
00086       }
00087 
00088    private:
00089       CallbackVector callbacks_;
00090 
00091    };
00092 
00093 }
00094 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines