定时器的应用实在是太广了,从普通应用程序到游戏,大部分程序都会用到定时器。如果你打算让你的应用程序跨越平台的限制,一个跨平台的定时器是必不可少的。上一次,我给大家分享了一段有关线程的代码。实际上,定时器的是以线程为基础的,在这里我仍然是使用pthread,当然大家可以直接继承我的utils::CThread类。

 

timer.h:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 *  timer.h
 *  utils
 *
 *  Created by Bagusflyer on 09-6-2.
 *  Copyright 2009 www.iphone-geek.cn. All rights reserved.
 *
 */


#ifndef _IPHONEGEEK_UTILS_TIMER_H_
#define _IPHONEGEEK_UTILS_TIMER_H_

#include <pthread.h>

namespace utils
{
   
    class CTimer;
   
    class CTimerCallbackInterface
    {
    public:
        virtual void onTimer(const CTimer* timer) = 0;
    };

    class CTimer
    {
    public:    
       
        typedef enum
        {
            TIMER_STOP  = 0,
            TIMER_RUN,
            TIMER_PAUSE
        } TimerState;
       
        CTimer(CTimerCallbackInterface* callback,unsigned long interval,void* userData,bool repeat = true);
       
        void resume();
        void pause();
        void stop();    // calling this will destroy CTimer object
       
        unsigned long interval() const              { return m_ulInterval;  }
        void* userData() const                      { return m_pUserData;   }
        bool isRepeat() const                       { return m_bRepeat;     }
        CTimerCallbackInterface* callback() const   { return m_pCallback;   }
        TimerState state() const                    { return m_state;       }
       
    private:
        CTimerCallbackInterface*    m_pCallback;
        void*                       m_pUserData;
        bool                        m_bRepeat;
        unsigned long               m_ulInterval;
        pthread_t                   m_thread;
        TimerState                  m_state;

        friend void* onRun(void* ptr);

        void start();
       
        ~CTimer();

    };

}

#endif // _IPHONEGEEK_UTILS_TIMER_H_

 

timer.cpp:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 *  Timer.cpp
 *  utils
 *
 *  Created by Bagusflyer on 09-6-2.
 *  Copyright 2009 www.iphone-geek.cn. All rights reserved.
 *
 */


#include "utils/timer.h"
#include <assert.h>

namespace utils
{

    // main loop for the timer thread
    void* onRun(void* ptr)
    {      
        bool run = true;
       
        CTimer* theTimer = (CTimer*)ptr;
        while( run )
        {
            switch ( theTimer->state() )
            {
                case CTimer::TIMER_PAUSE:
                {
                    msleep(50);
                    break;
                }
                   
                case CTimer::TIMER_RUN:
                {
                    CTimerCallbackInterface* callback = theTimer->callback();
                    callback->onTimer(theTimer);
                   
                    if ( !theTimer->isRepeat() )
                        theTimer->stop();
                    else
                        msleep(theTimer->interval());                  
                    break;
                }
                   
                case CTimer::TIMER_STOP:
                {
                    run = false;    // exit the main loop
                    break;
                }
            }
        }
       
        delete theTimer;
       
        return NULL;
    }

    CTimer::CTimer(CTimerCallbackInterface* callback,unsigned long interval,void* userData,bool repeat)
        : m_pCallback(callback),
          m_pUserData(userData),
          m_bRepeat(repeat),
          m_ulInterval(interval),
          m_state(TIMER_PAUSE)
    {
        assert(callback);
        // create a thread
        pthread_create( &m_thread,NULL,onRun, (void *)this );
        // TODO: error check
        start();
    }

    CTimer::~CTimer()
    {
        pthread_join(m_thread, NULL);
    }
   
    void CTimer::start()
    {
        m_state = TIMER_RUN;
    }
   
    void CTimer::resume()
    {
        // TODO: to be done
        m_state = TIMER_RUN;
    }
   
    void CTimer::stop()
    {
        m_state = TIMER_STOP;
    }
   
    void CTimer::pause()
    {
        // TODO: to be implemented
        m_state = TIMER_PAUSE;
    }

}

 

注意:msleep在跨平台代码分享系列之一中有说明。

 

使用方法:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// .h文件中
class MyApp: public utils::CTimerCallbackInterface  // 继承CTimerCallbackInterface
{
public:
    void onTimer(const utils::CTimer* timer); // 实现onTimer

};

// .cpp 文件中某处
// 参数1 - 指向CTimerCallbackInterface
// 参数2 - 定时器分辨率,以ms为单位,此例为10ms
// 参数3 - 数据指针,用来传递数据到定时器回调中
// 参数4 - 是否循环触发
utils::CTimer* timer = new utils::CTimer(this,10,data,true);

// 回调
void MyApp::onTimer(const utils::CTimer* theTimer)
{
    // 指向你想要的功能
    // 你可以使用下面方法获取来自于定时器的数据
    UserData* data = (UserData*)theTimer->userData();
}

// 释放
// 记住一定要调用stop,来删除创建的定时器
timer->stop();

 

下次的题目是跨平台xml解析。有什么问题,建议,希望大家给点回复!你们的支持是我坚持下去的动力,谢谢!