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