zoukankan      html  css  js  c++  java
  • 写的一个简单定时器(非独立线程)

    //Callback.h
    #ifndef __CALLBACK_H__
    #define __CALLBACK_H__
    
    typedef void (*T_CallBack)(void *);
    typedef struct  
    {
        T_CallBack cb;
        void *obj;
    }ST_CallBack;
    
    int __NewTimer(void* obj, int interval, bool isloop, T_CallBack cb);
    void __DeleteTimer(int handle);
    
    
    #define NewTimer(handle,obj,delay,isloop,CLASS,MEMBER) do{ 
    union {       
        T_CallBack cb; 
        void (CLASS::*___M)(void); 
    }___T; 
        ___T.___M = &CLASS::MEMBER;    
        handle = __NewTimer(obj, delay, isloop, ___T.cb); 
    }while(0)
    
    #define DeleteTimer(handle) do{ 
        if ( handle) { 
            __DeleteTimer(handle); 
            handle = 0; 
        } 
    }while(0)
    
    //#define NewTimer
    
    int TimerGetDelay(int handle);
    void TimerSetDelay(int handle, int delay);
    void TimerStart(int handle);
    void TimerStop(int handle);
    
    
    #endif

    //Callback.cpp
    
    
    typedef struct  
    {    
        Uint32 lastticks;    
        bool isloop;
        int delay;
        ST_CallBack cb;    
        bool isdel;
        bool ispause;
    }ST_TimerTask;
    
    static std::list<ST_TimerTask*> timerTasks; 
    
    int __NewTimer(void* obj, int delay, bool isloop, T_CallBack cb)
    {  
        ST_TimerTask *task;
    
        NEW_OBJ(task, ST_TimerTask);
        task->isloop = isloop;
        task->delay = delay;
        task->cb.obj = obj;
        task->cb.cb = (T_CallBack)cb;
        task->lastticks = SDL_GetTicks();
        task->isdel = false;    
        task->ispause = false;
        timerTasks.push_back(task);
        return (int)task;
    }
    
    void __DeleteTimer(int handle)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end(); iter++)    {
            task = (*iter);
            if ( (int)task == handle){
                task->isdel = true;
                return;
            }
        }
    }
    
    void DeleteAllTimer(void)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end();)    {
            task = (*iter);
            if ( task){
                iter = timerTasks.erase(iter);        
                DELETE_OBJ(task);
            }else
                iter++;
        }
        timerTasks.clear();
    }
    
    void TimerRunning(Uint32 curticks)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end(); )    {
            task = (*iter);
            if ( task) { 
                if(curticks - task->lastticks >= task->delay){
                    if ( task->isdel == false && task->ispause == false)
                        task->cb.cb(task->cb.obj);                
                    if (task->isdel || task->isloop == false){
                        iter = timerTasks.erase(iter);
                        DELETE_OBJ(task);
                        continue;
                    }
                    task->lastticks = curticks;            
                }            
            }
            iter++;
        }
    }
    
    int TimerGetDelay(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        return task->delay;
    }
    
    void TimerSetDelay(int handle, int delay)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->delay = delay;
    }
    void TimerStart(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->ispause = false;
    }
    void TimerStop(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->ispause = true;
    }
    
    
    
     
  • 相关阅读:
    leetcode 48. Rotate Image
    leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点) 、26/80. Remove Duplicates from Sorted ArrayI、II
    leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
    leetcode 58. Length of Last Word
    安卓操作的一些问题解决
    leetcode 378. Kth Smallest Element in a Sorted Matrix
    android studio Gradle Build速度加快方法
    禁用gridview,listview回弹或下拉悬停
    Android Studio找不到FragmentActivity类
    安卓获取ListView、GridView等滚动的距离(高度)
  • 原文地址:https://www.cnblogs.com/colife/p/3779210.html
Copyright © 2011-2022 走看看