073d32b56040fc33469c2661dd219840bacf6e18
[libasyncio] / asyncio / async_timer.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /** @file
21  *
22  * (c) Copyright 2007 by Intra2net AG
23  */
24
25 //#define NOISEDEBUG
26
27
28 #include "async_timer.hpp"
29
30
31 #ifdef NOISEDEBUG
32 #include <iostream>
33 #include <iomanip>
34 #define DOUT(msg) std::cout << msg << std::endl
35 #define FODOUT(obj,msg) std::cout << typeid(*obj).name() << "[" << obj << "]:" << msg << std::endl
36 #define ODOUT(msg) std::cout << typeid(*this).name() << "[" << this << "]:" << msg << std::endl
37 #else
38 #define DOUT(msg) do {} while (0)
39 #define FODOUT(obj,msg) do {} while (0)
40 #define ODOUT(msg) do {} while (0)
41 #endif
42
43
44 namespace AsyncIo
45 {
46
47 using namespace Utils;
48
49 /*
50  * Implementation of SimpleTimer
51  */
52
53 SimpleTimer::SimpleTimer()
54 {
55 } // eo SimpleTimer::SimpleTimer()
56
57
58 SimpleTimer::SimpleTimer(const MilliTime& delta, const TimerSignal::slot_function_type& action)
59 {
60    addAction(action);
61    startTimer(delta);
62 } // eo SimpleTimer::SimpleTimer(const MilliTime&, const TimerSignal::slot_type&)
63
64
65 SimpleTimer::SimpleTimer(long milli_seonds_delta, const TimerSignal::slot_function_type& action)
66 {
67    addAction(action);
68    startTimerMS(milli_seonds_delta);
69 } // eo SimpleTimer::SimpleTimer(long, const TimerSignal::slot_type&)
70
71
72 void SimpleTimer::execute()
73 {
74    ODOUT("execute()!");
75    m_timer_signal();
76    ODOUT("  signal with this");
77    m_timer_signal_p(this);
78    ODOUT("execute done.");
79 } // eo SimpleTimer::execute
80
81
82 void SimpleTimer::addAction( const TimerSignal::slot_function_type& action )
83 {
84    m_timer_signal.connect(action);
85 } // eo SimpleTimer::addAction(const TimerSignal::slot_type&)
86
87
88 void SimpleTimer::addActionP( const TimerSignalP::slot_function_type& action )
89 {
90    m_timer_signal_p.connect(action);
91 } // eo SimpleTimer::addAction(const TimerSignalP::slot_type&)
92
93
94 void SimpleTimer::startTimer( const MilliTime& delta )
95 {
96    m_delta= delta;
97    setDeltaWhenTime( delta );
98    activate(true);
99 #ifdef NOISEDEBUG
100    MilliTime now, t;
101    get_current_monotonic_time(now);
102    t= getWhenTime();
103    MilliTime dt= t-now;
104    ODOUT("startTimer");
105    ODOUT("  now: sec="<< now.mt_sec << ", msec="<<now.mt_msec);
106    ODOUT("  t:   sec="<< t.mt_sec << ", msec="<<t.mt_msec);
107    ODOUT("  dt:  sec="<< dt.mt_sec << ", msec="<<dt.mt_msec);
108 #endif
109 } // eo SimpleTimer::startTimer(const MilliTime&)
110
111
112 void SimpleTimer::startTimerMS( long milli_seconds )
113 {
114    startTimer( MilliTime(0,milli_seconds) );
115 } // eo SimpleTimer::stratTimerMS(long)
116
117
118 void SimpleTimer::stopTimer()
119 {
120    deactivate();
121 } // eo SimpleTimer::stopTimer
122
123
124 } // eo namespace AsyncIo