Wiselib
wiselib.stable/util/base_classes/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_RADIO_BASE_H__
00020 #define __UTIL_BASECLASSES_RADIO_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 
00036    template<typename OsModel_P,
00037             typename NodeId_P,
00038             typename Size_P,
00039             typename BlockData_P,
00040             int MAX_RECEIVERS = RADIO_BASE_MAX_RECEIVERS>
00041    class RadioBase
00042    {
00043    public:
00044       typedef OsModel_P OsModel;
00045 
00046       typedef NodeId_P node_id_t;
00047       typedef Size_P size_t;
00048       typedef BlockData_P block_data_t;
00049 
00050       typedef delegate3<void, node_id_t, size_t, block_data_t*> radio_delegate_t;
00051 
00052       typedef vector_static<OsModel, radio_delegate_t, MAX_RECEIVERS> CallbackVector;
00053       typedef typename CallbackVector::iterator CallbackVectorIterator;
00054       // --------------------------------------------------------------------
00055       enum ReturnValues
00056       {
00057          SUCCESS = OsModel::SUCCESS
00058       };
00059       // --------------------------------------------------------------------
00060       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00061       int reg_recv_callback( T *obj_pnt )
00062       {
00063          if ( callbacks_.empty() )
00064             callbacks_.assign( MAX_RECEIVERS, radio_delegate_t() );
00065 
00066          for ( unsigned int i = 0; i < callbacks_.size(); ++i )
00067          {
00068             if ( callbacks_.at(i) == radio_delegate_t() )
00069             {
00070                callbacks_.at(i) = radio_delegate_t::template from_method<T, TMethod>( obj_pnt );
00071                return i;
00072             }
00073          }
00074 
00075          return -1;
00076       }
00077       // --------------------------------------------------------------------
00078       int unreg_recv_callback( int idx )
00079       {
00080          callbacks_.at(idx) = radio_delegate_t();
00081          return SUCCESS;
00082       }
00083       // --------------------------------------------------------------------
00084       void notify_receivers( node_id_t from, size_t len, block_data_t *data )
00085       {
00086          for ( CallbackVectorIterator
00087                   it = callbacks_.begin();
00088                   it != callbacks_.end();
00089                   ++it )
00090          {
00091             if ( *it != radio_delegate_t() )
00092                (*it)( from, len, data );
00093          }
00094       }
00095 
00096    private:
00097       CallbackVector callbacks_;
00098 
00099    };
00100 
00101 }
00102 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines