Add Poller

Add implement of Looper::operator -
This commit is contained in:
Kirigaya Kazuto 2017-05-02 20:51:56 +08:00
parent d18fcf385d
commit 2ecddc44e7
2 changed files with 54 additions and 1 deletions

View File

@ -29,6 +29,7 @@ Looper::Looper()
{
_update=_running=true;
_loop_cnt=0;
updater=[](){};
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback)
{
@ -149,6 +150,11 @@ bool Looper::remove(const LooperID& looperid)
return false;
}
bool Looper::operator -(const LooperID& looperid)
{
return remove(looperid);
}
void Looper::dispatch()
{
for(auto callbackPack:_evmap[_e.type])
@ -165,6 +171,8 @@ void Looper::run()
dispatch();
}
updater();
_update=false;
}
}
@ -191,4 +199,36 @@ void Looper::reset()
_update=true;
_evmap.clear();
_loop_cnt=0;
updater=[](){};
}
Poller::Poller()
{
idler=[](){};
}
void Poller::reset()
{
Looper::reset();
idler=[](){};
}
void Poller::run()
{
int pollret=1;
while(_running)
{
while(!_update&&(pollret=PollEvent(_e)))
{
dispatch();
}
/// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call update.
if(!pollret) idler();
if(_update)
{
updater();
_update=false;
}
}
}

View File

@ -56,9 +56,22 @@ public:
void needstop();
void stop();
void reset();
private:
std::function<void()> updater;
protected:
Event _e;
bool _running,_update;
std::map<_SDLEventType_,std::list<std::pair<int,std::function<int(Looper&,Event&)>>>> _evmap;
int _loop_cnt;
};
class Poller : public Looper
{
public:
Poller();
void run();
void reset();
std::function<void()> idler;
};