00001 #ifndef _CALLBACK_H__
00002 #define _CALLBACK_H__
00003
00004 #include <list>
00005 #include <algorithm>
00006
00007 template<typename P>
00008 class Callback {
00009
00010 typedef bool (*callback_f)(void *, const P);
00011 typedef std::pair<callback_f, void*> Entry;
00012
00013 typedef std::list<Entry> EntrySet;
00014 EntrySet entrySet;
00015
00016
00017 public:
00018
00019 Callback() {
00020 entrySet.clear();
00021 }
00022
00023 ~Callback() {
00024
00025 }
00026
00027 bool operator()(const P param) {
00028
00029 bool ret = true;
00030 typename EntrySet::iterator itr;
00031
00032 for ( itr = entrySet.begin();
00033 itr != entrySet.end();
00034 ++itr) {
00035
00036 ret = itr->first(itr->second, param);
00037 if (!ret)
00038 return ret;
00039 }
00040 return ret;
00041 }
00042
00043
00044 bool add(callback_f f, void *data) {
00045
00046
00047 if (find(entrySet.begin(), entrySet.end(), Entry(f,data)) != entrySet.end() )
00048 return false;
00049
00050 entrySet.push_back(Entry(f,data));
00051 return true;
00052 }
00053
00054 bool remove(callback_f f, void *data) {
00055
00056 typename EntrySet::iterator itr =
00057 find(entrySet.begin(), entrySet.end(), Entry(f,data));
00058
00059 if (itr == entrySet.end())
00060 return false;
00061
00062 entrySet.erase(itr);
00063 return true;
00064 }
00065
00066 bool empty() const {
00067 return entrySet.empty();
00068 }
00069
00070 };
00071
00072
00073
00074
00075 #endif //CALLBACK_H__
00076
00077
00078
00079
00080
00081