67d7fd3759fbde31215b4a72018b7a5727434516
[libasyncio] / asyncio / async_timer.cpp
1 /** @file
2  *
3  * (c) Copyright 2007 by Intra2net AG
4  * 
5  * info@intra2net.com
6  */
7
8 //#define NOISEDEBUG
9
10
11 #include "async_timer.hpp"
12
13
14 #ifdef NOISEDEBUG
15 #include <iostream>
16 #include <iomanip>
17 #define DOUT(msg) std::cout << msg << std::endl
18 #define FODOUT(obj,msg) std::cout << typeid(*obj).name() << "[" << obj << "]:" << msg << std::endl
19 #define ODOUT(msg) std::cout << typeid(*this).name() << "[" << this << "]:" << msg << std::endl
20 #else
21 #define DOUT(msg) do {} while (0)
22 #define FODOUT(obj,msg) do {} while (0)
23 #define ODOUT(msg) do {} while (0)
24 #endif
25
26
27 namespace AsyncIo
28 {
29
30
31 /*
32  * Implementation of SimpleTimer
33  */
34
35 SimpleTimer::SimpleTimer()
36 {
37 } // eo SimpleTimer::SimpleTimer()
38
39
40 SimpleTimer::SimpleTimer(const MilliTime& delta, const TimerSignal::slot_function_type& action)
41 {
42    addAction(action);
43    startTimer(delta);
44 } // eo SimpleTimer::SimpleTimer(const MilliTime&, const TimerSignal::slot_type&)
45
46
47 SimpleTimer::SimpleTimer(long milli_seonds_delta, const TimerSignal::slot_function_type& action)
48 {
49    addAction(action);
50    startTimerMS(milli_seonds_delta);
51 } // eo SimpleTimer::SimpleTimer(long, const TimerSignal::slot_type&)
52
53
54 void SimpleTimer::execute()
55 {
56    ODOUT("execute()!");
57    m_timer_signal();
58    m_timer_signal_p(this);
59 } // eo SimpleTimer::execute
60
61
62 void SimpleTimer::addAction( const TimerSignal::slot_function_type& action )
63 {
64    m_timer_signal.connect(action);
65 } // eo SimpleTimer::addAction(const TimerSignal::slot_type&)
66
67
68 void SimpleTimer::addActionP( const TimerSignalP::slot_function_type& action )
69 {
70    m_timer_signal_p.connect(action);
71 } // eo SimpleTimer::addAction(const TimerSignalP::slot_type&)
72
73
74 void SimpleTimer::startTimer( const MilliTime& delta )
75 {
76    m_delta= delta;
77    setDeltaWhenTime( delta );
78    activate(true);
79 #ifdef NOISEDEBUG
80    MilliTime now, t;
81    get_current_time(now);
82    t= getWhenTime();
83    MilliTime dt= t-now;
84    ODOUT("startTimer");
85    ODOUT("  now: sec="<< now.mt_sec << ", msec="<<now.mt_msec);
86    ODOUT("  t:   sec="<< t.mt_sec << ", msec="<<t.mt_msec);
87    ODOUT("  dt:  sec="<< dt.mt_sec << ", msec="<<dt.mt_msec);
88 #endif
89 } // eo SimpleTimer::startTimer(const MilliTime&)
90
91
92 void SimpleTimer::startTimerMS( long milli_seconds )
93 {
94    startTimer( MilliTime(0,milli_seconds) );
95 } // eo SimpleTimer::stratTimerMS(long)
96
97
98 void SimpleTimer::stopTimer()
99 {
100    deactivate();
101 } // eo SimpleTimer::stopTimer
102
103
104 } // eo namespace AsyncIo