Wiselib
|
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_SENSORCALLBACK_BASE_H__ 00020 #define __UTIL_BASECLASSES_SENSORCALLBACK_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 typename Value_P, 00037 int MAX_RECEIVERS = SENSOR_CALLBACK_BASE_MAX_RECEIVERS> 00038 class SensorCallbackBase 00039 { 00040 public: 00041 typedef OsModel_P OsModel; 00042 typedef Value_P value_t; 00043 00044 typedef delegate1<void, value_t> sensor_callback_delegate_t; 00045 00046 typedef vector_static<OsModel, sensor_callback_delegate_t, MAX_RECEIVERS> CallbackVector; 00047 typedef typename CallbackVector::iterator CallbackVectorIterator; 00048 // -------------------------------------------------------------------- 00049 enum ReturnValues 00050 { 00051 SUCCESS = OsModel::SUCCESS 00052 }; 00053 // -------------------------------------------------------------------- 00054 template<class T, void (T::*TMethod)(value_t)> 00055 int register_sensor_callback( T *obj_pnt ) 00056 { 00057 if ( callbacks_.empty() ) 00058 callbacks_.assign( MAX_RECEIVERS, sensor_callback_delegate_t() ); 00059 00060 for ( unsigned int i = 0; i < callbacks_.size(); ++i ) 00061 { 00062 if ( callbacks_.at(i) == sensor_callback_delegate_t() ) 00063 { 00064 callbacks_.at(i) = sensor_callback_delegate_t::template from_method<T, TMethod>( obj_pnt ); 00065 return i; 00066 } 00067 } 00068 00069 return -1; 00070 } 00071 // -------------------------------------------------------------------- 00072 int unreg_read_callback( int idx ) 00073 { 00074 callbacks_.at(idx) = sensor_callback_delegate_t(); 00075 return SUCCESS; 00076 } 00077 // -------------------------------------------------------------------- 00078 void notify_receivers( value_t value ) 00079 { 00080 for ( CallbackVectorIterator 00081 it = callbacks_.begin(); 00082 it != callbacks_.end(); 00083 ++it ) 00084 { 00085 if ( *it != sensor_callback_delegate_t() ) 00086 (*it)( value ); 00087 } 00088 } 00089 00090 private: 00091 CallbackVector callbacks_; 00092 00093 }; 00094 00095 } 00096 #endif