00001 // 00002 #ifndef SIMPLEEVENT_H 00003 #define SIMPLEEVENT_H 00004 //## begin module.includes preserve=yes 00005 #include "ScheduledEvent.h" 00006 00007 //## end module.includes preserve=yes 00008 template <class Receiver> 00009 class SimpleEvent : public ScheduledEvent { 00010 // A template class for simple events. The template parameter is the 00011 // class which is the receiver, i.e the class which actually performs 00012 // the operations. 00013 // usage: Scheduler::instance()->schedule(time, new SimpleEvent<MyClass>(my_instance, &MyClass::method)); 00014 public: 00015 //## begin SimpleEvent.initialDeclarations preserve=yes 00016 public: 00017 typedef void (Receiver::* Action)(); 00018 00019 //## end SimpleEvent.initialDeclarations 00020 SimpleEvent(Receiver* r, Action a) 00021 //## begin SimpleEvent::SimpleEvent%249491085.initialization preserve=yes 00022 :m_receiver(r),m_action(a) 00023 //## end SimpleEvent::SimpleEvent%249491085.initialization 00024 { 00025 00026 //## begin SimpleEvent::SimpleEvent%249491085.body preserve=yes 00027 //## end SimpleEvent::SimpleEvent%249491085.body 00028 } 00029 ; 00030 // Constructor needs the receiver class and the method to invoke in that class 00031 00032 SimpleEvent(Receiver*r, Action a, std::string n) 00033 : m_receiver(r), m_action(a), m_name(n){}; 00034 00035 virtual void execute(); 00036 // All a command can do - tells the receiver to perform the action. 00037 00038 virtual std::string name()const { 00039 return !m_name.empty()? m_name : ScheduledEvent::name();} 00040 00041 private: 00042 Receiver* m_receiver; 00043 Action m_action; 00044 std::string m_name; 00045 00046 }; 00047 00048 template <class Receiver> inline 00049 void SimpleEvent<Receiver>::execute(){ 00050 //## begin SimpleEvent::execute%478434479.body preserve=yes 00051 if (m_receiver) (m_receiver->*m_action)(); 00052 //## end SimpleEvent::execute%478434479.body 00053 } 00054 00055 00056 #endif