00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00027
00028 #include <senf/Utils/membind.hh>
00029
00030 #define prefix_
00031
00032
00033 template<typename IdType>
00034 prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
00035 : entrySetById( entrySet.template get<Id>()),
00036 entrySetByTimeout( entrySet.template get<Timeout> ()),
00037 timer( "TimerEventProxy " + description,
00038 membind(&TimerEventProxy<IdType>::timerEvent, this), 0, false)
00039 { }
00040
00041 template<typename IdType>
00042 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
00043 {
00044 ClockService::clock_type now = senf::scheduler::now();
00045 typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
00046 while (it != entrySetByTimeout.end() && it->timeout <= now) {
00047 Entry item (*it);
00048
00049 entrySetByTimeout.erase(it);
00050
00051 item.cb(now, item.id);
00052 it = entrySetByTimeout.begin();
00053 }
00054 if (entrySet.size() > 0)
00055 timer.timeout(entrySetByTimeout.begin()->timeout);
00056 }
00057
00058 template<typename IdType>
00059 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
00060 ClockService::clock_type timeout, IdType const & id, Callback cb)
00061 {
00062
00063 typename EntrySetById_t::iterator i = entrySetById.find(id);
00064 if(i == entrySetById.end())
00065 entrySetByTimeout.insert( Entry(timeout, id, cb));
00066 else{
00067 Entry tmp = *i;
00068 tmp.timeout = timeout;
00069 entrySetById.replace(i,tmp);
00070 }
00071
00072 timer.timeout( entrySetByTimeout.begin()->timeout);
00073 }
00074
00075 template<typename IdType>
00076 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
00077 {
00078 bool removed (entrySetById.erase( id) > 0);
00079 if (entrySet.size() > 0)
00080 timer.timeout(entrySetByTimeout.begin()->timeout);
00081 else
00082 timer.disable();
00083 return removed;
00084 }
00085
00086 template<typename IdType>
00087 prefix_ senf::ClockService::clock_type senf::scheduler::TimerEventProxy<IdType>::timeout(IdType const & id)
00088 const
00089 {
00090 typename EntrySetById_t::const_iterator i ( entrySetById.find( id));
00091 return i == entrySetById.end() ? 0 : i->timeout;
00092 }
00093
00094
00095 template<typename IdType>
00096 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
00097 const
00098 {
00099 std::vector<std::pair<ClockService::clock_type, IdType> > tmp;
00100
00101 typename EntrySetByTimeout_t::const_iterator it;
00102 for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
00103 tmp.push_back(std::make_pair<ClockService::clock_type, IdType>( it->timeout, it->id));
00104 }
00105 return tmp;
00106 }
00107
00108 template<typename IdType>
00109 prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
00110 const
00111 {
00112 return entrySetByTimeout.size();
00113 }
00114
00115 template<typename IdType>
00116 prefix_ void senf::scheduler::TimerEventProxy<IdType>::clear()
00117 {
00118 entrySetByTimeout.clear();
00119 }
00120
00121
00122 #undef prefix_
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135