OSDN Git Service

rm latex/
[moflib/moflib.git] / oldmof / EventScheduler.cpp
1 #include "mof/EventScheduler.hpp"
2 #include "mof/EventCondition.hpp"
3 #include <list>
4 #include <utility>
5 #include "mof/utilities.hpp"
6
7
8 class TimeEventCondition : public mof::EventCondition{
9         mof::FrameNumber limit;
10         mof::FrameNumber count;
11
12 public:
13         TimeEventCondition(mof::FrameNumber limit)
14                 : limit(limit) , count(0)
15         {
16         }
17
18         virtual ~TimeEventCondition(){}
19
20         virtual bool test(){
21                 return count == limit;
22         }
23
24         virtual bool isDisposable(){
25                 return count > limit;
26         }
27
28         virtual void update(){
29                 if(limit >= count)count++;
30         }
31 };
32
33 struct mof::EventScheduler::Impl{
34         typedef std::pair<mof::EventCondition* , boost::function0<void>> Schedule; 
35         typedef std::list<std::pair<mof::EventCondition* , boost::function0<void>>> EventList; 
36         EventList eventList;
37
38         Impl(){
39         }
40
41         ~Impl(){
42                 foreach( Impl::Schedule &value , eventList ){
43                         delete value.first;
44                 }
45         }
46
47 };
48
49 mof::EventScheduler::EventScheduler()
50 : m_pImpl(new Impl())
51 {
52 }
53
54 mof::EventScheduler::~EventScheduler(){
55         
56 }
57
58 void mof::EventScheduler::addEvent( mof::EventCondition* pCondition , const boost::function0<void> &action){
59         m_pImpl->eventList.push_back(std::pair<mof::EventCondition* , boost::function0<void>>(pCondition , action));
60 }
61
62 void mof::EventScheduler::addEvent(mof::FrameNumber frame, const boost::function0<void> &action){
63         addEvent(new TimeEventCondition(frame) , action);
64 }
65
66 void mof::EventScheduler::update(){
67         for( Impl::EventList::iterator itr = m_pImpl->eventList.begin() ;
68                 itr != m_pImpl->eventList.end() ; 
69                 ){
70                         itr->first->update();
71                         if(itr->first->test()){
72                                 itr->second();
73                         }
74                         if(itr->first->isDisposable()){
75                                 delete itr->first;
76                                 itr = m_pImpl->eventList.erase(itr);
77                         }
78                         else ++itr;
79         }
80 }
81
82
83