Wiselib
wiselib.testing/util/base_classes/extended_radio_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_EXTENDED_RADIO_BASE_H__
00020 #define __UTIL_BASECLASSES_EXTENDED_RADIO_BASE_H__
00021 
00022 #include "util/base_classes/radio_base.h"
00023 #include "util/delegates/delegate.hpp"
00024 #include "util/pstl/vector_static.h"
00025 #include "util/base_classes/base_extended_data.h"
00026 #include "config.h"
00027 
00028 namespace wiselib
00029 {
00030 
00037    template<typename OsModel_P,
00038             typename NodeId_P,
00039             typename Size_P,
00040             typename BlockData_P,
00041             int MAX_RECEIVERS = RADIO_BASE_MAX_RECEIVERS,
00042             typename ExtendedData_P = BaseExtendedData<OsModel_P>
00043             >
00044    class ExtendedRadioBase : public RadioBase< OsModel_P, NodeId_P, Size_P, BlockData_P, MAX_RECEIVERS>
00045    {
00046    public:
00047      typedef ExtendedData_P ExtendedData;
00048 
00049       typedef OsModel_P OsModel;
00050 
00051       typedef NodeId_P node_id_t;
00052       typedef Size_P size_t;
00053       typedef BlockData_P block_data_t;
00054 
00055       typedef delegate3<void, node_id_t, size_t, block_data_t*> radio_delegate_t;
00056       typedef delegate4<void, node_id_t, size_t, block_data_t*, const ExtendedData&> extended_radio_delegate_t;
00057 
00058       typedef vector_static<OsModel, radio_delegate_t, MAX_RECEIVERS> CallbackVector;
00059       typedef typename CallbackVector::iterator CallbackVectorIterator;
00060 
00061       typedef vector_static<OsModel, extended_radio_delegate_t, MAX_RECEIVERS> ExtendedCallbackVector;
00062       typedef typename ExtendedCallbackVector::iterator ExtendedCallbackVectorIterator;
00063 
00064       // --------------------------------------------------------------------
00065       enum ReturnValues
00066       {
00067          SUCCESS = OsModel::SUCCESS
00068       };
00069       // --------------------------------------------------------------------
00070       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00071       int reg_recv_callback( T *obj_pnt )
00072       {
00073         return RadioBase<OsModel_P, NodeId_P, Size_P, BlockData_P, MAX_RECEIVERS>::template reg_recv_callback<T, TMethod>( obj_pnt );
00074       }
00075       // --------------------------------------------------------------------
00076       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*, const ExtendedData&)>
00077       int reg_recv_callback( T *obj_pnt )
00078       {
00079          if ( extended_callbacks_.empty() )
00080           extended_callbacks_.assign( MAX_RECEIVERS, extended_radio_delegate_t() );
00081 
00082          for ( unsigned int i = 0; i < extended_callbacks_.size(); ++i )
00083          {
00084             if ( extended_callbacks_.at(i) == extended_radio_delegate_t() )
00085             {
00086                extended_callbacks_.at(i) = extended_radio_delegate_t::template from_method<T, TMethod>( obj_pnt );
00087                return MAX_RECEIVERS+i;
00088             }
00089          }
00090 
00091          return -1;
00092       }
00093       // --------------------------------------------------------------------
00094       int unreg_recv_callback( int idx )
00095       {
00096         if( idx < MAX_RECEIVERS )
00097            return RadioBase<OsModel_P, NodeId_P, Size_P, BlockData_P, MAX_RECEIVERS>::unreg_recv_callback( idx );
00098         else
00099            extended_callbacks_.at( idx - MAX_RECEIVERS ) = extended_radio_delegate_t();
00100 
00101          return SUCCESS;
00102       }
00103       // --------------------------------------------------------------------
00104       void notify_receivers( node_id_t from, size_t len, block_data_t *data )
00105       {
00106         RadioBase<OsModel_P, NodeId_P, Size_P, BlockData_P, MAX_RECEIVERS>::notify_receivers( from, len, data );
00107       }
00108       // --------------------------------------------------------------------
00109       void notify_receivers( node_id_t from, size_t len, block_data_t *data, const ExtendedData& ext_data )
00110       {
00111        notify_receivers( from, len, data );
00112 
00113          for ( ExtendedCallbackVectorIterator
00114                   it = extended_callbacks_.begin();
00115                   it != extended_callbacks_.end();
00116                   ++it )
00117          {
00118             if ( *it != extended_radio_delegate_t() )
00119                (*it)( from, len, data, ext_data );
00120          }
00121       }
00122 
00123    private:
00124       ExtendedCallbackVector extended_callbacks_;
00125    };
00126 
00127 }
00128 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines