bbdae19540285ee6fe8b2432ddeadcd5b47a4824
[senf.git] / Scheduler / TimerEvent.cci
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief TimerDispatcher inline non-template implementation */
25
26 #include "TimerEvent.ih"
27
28 // Custom includes
29 #include <unistd.h>
30
31 #define prefix_ inline
32 ///////////////////////////////cci.p///////////////////////////////////////
33
34 ///////////////////////////////////////////////////////////////////////////
35 // senf::scheduler::TimerEvent
36
37 prefix_ senf::scheduler::TimerEvent::TimerEvent(std::string const & name, Callback const & cb,
38                                                 ClockService::clock_type timeout,
39                                                 bool initiallyEnabled)
40     : detail::FIFORunner::TaskInfo (name), cb_ (cb), timeout_ (timeout)
41 {
42     if (initiallyEnabled)
43         enable();
44 }
45
46 prefix_ senf::scheduler::TimerEvent::TimerEvent(std::string const & name, Callback const & cb)
47     : detail::FIFORunner::TaskInfo (name), cb_ (cb), timeout_ (0)
48 {}
49
50 prefix_ senf::scheduler::TimerEvent::~TimerEvent()
51 {
52     if (senf::scheduler::detail::TimerDispatcher::alive())
53         disable();
54 }
55
56 prefix_ void senf::scheduler::TimerEvent::enable()
57 {
58     if (! detail::TimerSetBase::linked())
59         senf::scheduler::detail::TimerDispatcher::instance().add(*this);
60 }
61
62 prefix_ void senf::scheduler::TimerEvent::disable()
63 {
64     if (detail::TimerSetBase::linked())
65         senf::scheduler::detail::TimerDispatcher::instance().remove(*this);
66 }
67
68 prefix_ void senf::scheduler::TimerEvent::action(Callback const & cb)
69 {
70     cb_ = cb;
71 }
72
73 prefix_ void senf::scheduler::TimerEvent::timeout(ClockService::clock_type timeout, 
74                                                   bool initiallyEnabled)
75 {
76     disable();
77     timeout_ = timeout;
78     if (initiallyEnabled)
79         enable();
80 }
81
82 ///////////////////////////////////////////////////////////////////////////
83 // senf::scheduler::detail::TimerDispatcher
84
85 prefix_ void senf::scheduler::detail::TimerDispatcher::enable()
86 {
87     source_->enable();
88 }
89
90 prefix_ void senf::scheduler::detail::TimerDispatcher::disable()
91 {
92     source_->disable();
93 }
94
95 prefix_ bool senf::scheduler::detail::TimerDispatcher::empty()
96     const
97 {
98     return timers_.empty();
99 }
100
101 prefix_ void senf::scheduler::detail::TimerDispatcher::
102 timerSource(std::auto_ptr<TimerSource> timerSource)
103 {
104     source_.reset(timerSource.release());
105 }
106
107 prefix_ senf::scheduler::detail::TimerSource *
108 senf::scheduler::detail::TimerDispatcher::timerSource()
109 {
110     return source_.get();
111 }
112
113 ///////////////////////////////cci.e///////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // comment-column: 40
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // End: