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