95aecab74a01d65d31d62f4124a6611bd2fd6be8
[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
48 /*
49  * Implementation of SimpleTimer
50  */
51
52 SimpleTimer::SimpleTimer()
53 {
54 } // eo SimpleTimer::SimpleTimer()
55
56
57 SimpleTimer::SimpleTimer(const MilliTime& delta, const TimerSignal::slot_function_type& action)
58 {
59    addAction(action);
60    startTimer(delta);
61 } // eo SimpleTimer::SimpleTimer(const MilliTime&, const TimerSignal::slot_type&)
62
63
64 SimpleTimer::SimpleTimer(long milli_seonds_delta, const TimerSignal::slot_function_type& action)
65 {
66    addAction(action);
67    startTimerMS(milli_seonds_delta);
68 } // eo SimpleTimer::SimpleTimer(long, const TimerSignal::slot_type&)
69
70
71 void SimpleTimer::execute()
72 {
73    ODOUT("execute()!");
74    m_timer_signal();
75    m_timer_signal_p(this);
76 } // eo SimpleTimer::execute
77
78
79 void SimpleTimer::addAction( const TimerSignal::slot_function_type& action )
80 {
81    m_timer_signal.connect(action);
82 } // eo SimpleTimer::addAction(const TimerSignal::slot_type&)
83
84
85 void SimpleTimer::addActionP( const TimerSignalP::slot_function_type& action )
86 {
87    m_timer_signal_p.connect(action);
88 } // eo SimpleTimer::addAction(const TimerSignalP::slot_type&)
89
90
91 void SimpleTimer::startTimer( const MilliTime& delta )
92 {
93    m_delta= delta;
94    setDeltaWhenTime( delta );
95    activate(true);
96 #ifdef NOISEDEBUG
97    MilliTime now, t;
98    get_current_time(now);
99    t= getWhenTime();
100    MilliTime dt= t-now;
101    ODOUT("startTimer");
102    ODOUT("  now: sec="<< now.mt_sec << ", msec="<<now.mt_msec);
103    ODOUT("  t:   sec="<< t.mt_sec << ", msec="<<t.mt_msec);
104    ODOUT("  dt:  sec="<< dt.mt_sec << ", msec="<<dt.mt_msec);
105 #endif
106 } // eo SimpleTimer::startTimer(const MilliTime&)
107
108
109 void SimpleTimer::startTimerMS( long milli_seconds )
110 {
111    startTimer( MilliTime(0,milli_seconds) );
112 } // eo SimpleTimer::stratTimerMS(long)
113
114
115 void SimpleTimer::stopTimer()
116 {
117    deactivate();
118 } // eo SimpleTimer::stopTimer
119
120
121 } // eo namespace AsyncIo