4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Mathias Kretschmer <mtk@berlios.de>
27 // Jens Moedeker <jmo@berlios.de>
30 \brief TimerEventProxy non-inline template implementation */
33 #include <senf/Utils/membind.hh>
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 template<typename IdType>
39 prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
40 : entrySetById( entrySet.template get<Id>()),
41 entrySetByTimeout( entrySet.template get<Timeout> ()),
42 timer( "TimerEventProxy " + description,
43 membind(&TimerEventProxy<IdType>::timerEvent, this), ClockService::clock_type(0), false)
46 template<typename IdType>
47 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
49 ClockService::clock_type now = senf::scheduler::now();
50 typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
51 while (it != entrySetByTimeout.end() && it->timeout <= now) {
53 // remove due entry from set
54 entrySetByTimeout.erase(it);
56 item.cb(now, item.id);
57 it = entrySetByTimeout.begin();
59 if (entrySet.size() > 0)
60 timer.timeout(entrySetByTimeout.begin()->timeout);
63 template<typename IdType>
64 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
65 ClockService::clock_type timeout, IdType const & id, Callback cb)
67 // insert new entry or replace the timeout of an entry already indexed
68 typename EntrySetById_t::iterator i = entrySetById.find(id);
69 if (i == entrySetById.end())
70 entrySetByTimeout.insert( Entry(timeout, id, cb));
73 tmp.timeout = timeout;
74 entrySetById.replace(i,tmp);
76 // the scheduler time to the first earliest timeout (ordered index)
77 timer.timeout( entrySetByTimeout.begin()->timeout);
80 template<typename IdType>
81 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
83 bool removed (entrySetById.erase( id) > 0);
84 if (entrySet.size() > 0)
85 timer.timeout(entrySetByTimeout.begin()->timeout);
91 template<typename IdType>
92 prefix_ senf::ClockService::clock_type senf::scheduler::TimerEventProxy<IdType>::timeout(IdType const & id)
95 typename EntrySetById_t::const_iterator i ( entrySetById.find( id));
96 return i == entrySetById.end() ? ClockService::clock_type(0) : i->timeout;
100 template<typename IdType>
101 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
104 std::vector<std::pair<ClockService::clock_type, IdType> > tmp;
106 typename EntrySetByTimeout_t::const_iterator it;
107 for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
108 tmp.push_back(std::make_pair<ClockService::clock_type, IdType>( it->timeout, it->id));
113 template<typename IdType>
114 prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
117 return entrySetByTimeout.size();
120 template<typename IdType>
121 prefix_ void senf::scheduler::TimerEventProxy<IdType>::clear()
123 entrySetByTimeout.clear();
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
133 // c-file-style: "senf"
134 // indent-tabs-mode: nil
135 // ispell-local-dictionary: "american"
136 // compile-command: "scons -u test"
137 // comment-column: 40