12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- struct card_timer
- {
- ev::dynamic_loop&m_loop;
- std::vector<ct_timer*> m_timers;
- std::vector<int> m_timers_free;
- card_timer(ev::dynamic_loop&loop)
- :m_loop(loop)
- {
- grow();
- }
- ~card_timer()
- {
- for(auto&i:m_timers)
- delete i;
- }
- void grow()
- {
- int on=m_timers.size();
- int n=on<<1;
- if(n==0)
- n=4;
- m_timers.resize(n);
- for(int i=on;i<n;i++)
- {
- m_timers[i]=new ct_timer();
- m_timers[i]->m_id=i;
- m_timers_free.push_back(i);
- }
- }
- void alloc_timer(int delay_ms,int ct,int type)
- {
- if(m_timers_free.empty())
- grow();
- int id=m_timers_free.back(); m_timers_free.pop_back();
- m_timers[id]->start<card_timer,&card_timer::on_timer>(m_loop,this,delay_ms,ct,type);
- }
- void free_timer(int ct,int type)
- {
- for(int i=0,len=m_timers.size();i<len;i++)
- {
- if(m_timers[i]->m_ct==ct && m_timers[i]->m_type==type)
- {
- m_timers[i]->stop();
- m_timers_free.push_back(i);
- break;
- }
- }
- }
- virtual void on_ct_timer(int ct,int type)=0;
- void on_timer(ev::timer&t,int)
- {
- ct_timer&tmp=*(ct_timer*)&t;
- on_ct_timer(tmp.m_ct,tmp.m_type);
- }
- };
|