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_CLUSTERING_BASE_H__ 00020 #define __UTIL_BASECLASSES_CLUSTERING_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 00034 template <typename OsModel_P, 00035 int MAX_RECEIVERS = 5 > 00036 class ClusteringBase { 00037 public: 00038 00039 typedef OsModel_P OsModel; 00040 typedef delegate1<void, int> cluster_delegate_t; 00041 00042 // -------------------------------------------------------------------- 00043 typedef vector_static<OsModel, cluster_delegate_t, MAX_RECEIVERS> CallbackVector; 00044 typedef typename CallbackVector::iterator CallbackVectorIterator; 00045 // -------------------------------------------------------------------- 00046 enum ReturnValues { 00047 SUCCESS = OsModel::SUCCESS 00048 }; 00049 // -------------------------------------------------------------------- 00050 00051 template<class T, void (T::*TMethod)(int) > 00052 int reg_state_changed_callback(T *obj_pnt) { 00053 if (callbacks_.empty()) 00054 callbacks_.assign(MAX_RECEIVERS, cluster_delegate_t()); 00055 00056 for (unsigned int i = 0; i < callbacks_.size(); ++i) { 00057 if (callbacks_.at(i) == cluster_delegate_t()) { 00058 callbacks_.at(i) = cluster_delegate_t::template from_method<T, TMethod > (obj_pnt); 00059 return i; 00060 } 00061 } 00062 00063 return -1; 00064 } 00065 // -------------------------------------------------------------------- 00066 00067 int unreg_state_changed_callback(int idx) { 00068 callbacks_.at(idx) = cluster_delegate_t(); 00069 return SUCCESS; 00070 } 00071 // -------------------------------------------------------------------- 00072 00073 void state_changed(int event) { 00074 for (CallbackVectorIterator 00075 it = callbacks_.begin(); 00076 it != callbacks_.end(); 00077 ++it) { 00078 if (*it != cluster_delegate_t()) 00079 (*it)(event); 00080 } 00081 } 00082 00083 private: 00084 CallbackVector callbacks_; 00085 00086 00087 }; 00088 00089 } 00090 #endif