// Activeobject. cpp: defines console applications Program . // # Include "stdafx. H "# include <windows. h ># include <deque ># include <iostream> using namespace STD; double secondspertick = 0; Class command {public: Virtual void execute () = 0 ;}; class activeobjectengine {deque <command *> itscommands; public: void addcommand (command * c) {itscommands. push_back (c);} void run () {While (itscommands. size ()> 0) {command * c = itscommands. front (); C-> execute (); itscommands. pop_front () ;}}; CIA SS sleepcommmand: Public command {command * wakeupcommand; activeobjectengine * engine; double sleeptime; bool started; large_integer lv; double start_time; public: sleepcommmand (double milliseconds, activeobjectengine * E, command * WC): started (false) {sleeptime = milliseconds; engine = E; wakeupcommand = WC;} virtual void execute () {queryperformancecounter (& LV ); double current_time = secondspertick * lv. quad Part; If (! Started) {started = true; start_time = current_time; engine-> addcommand (this);} else {double elasped_time = current_time-start_time; If (elasped_time <sleeptime) {Engine-> addcommand (this); sleep (1) ;}else {Engine-> addcommand (wakeupcommand) ;}}}; class wakeupcommand: Public command {bool excuted; public: wakeupcommand () {excuted = false;} virtual void execute () {large_integer lv; queryperformancecounter (& LV); do Uble current_time = secondspertick * lv. quadpart; excuted = true; cout <"\ n ********* \ nexcuted! \ N ************ "<current_time ;}}; class delayedtyper: Public command {public: Double itsdelay; char itschar; static bool stop; static activeobjectengine * engine; delayedtyper (double delay, char c) {itsdelay = delay; itschar = C ;}virtual void execute () {cout <itschar; If (! Stop) {delayandrepeat () ;}} void delayandrepeat () {Engine-> addcommand (New sleepcommmand (itsdelay, engine, this) ;}; bool delayedtyper :: stop = false; activeobjectengine * delayedtyper: Engine = new activeobjectengine; Class stopcommand: Public command {public: Virtual void execute () {delayedtyper: Stop = true ;}}; int _ tmain (INT argc, _ tchar * argv []) {large_integer lv; queryperformancefrequency (& LV); secondspertick = 1.0/LV. quadpart; queryperformancecounter (& LV); double current_time = secondspertick * lv. quadpart; // One Shot // wakeupcommand * wakup = new wakeupcommand (); // activeobjectengine * E = new activeobjectengine (); // sleepcommmand * c = new sleepcommmand (6, e, wakup); // E-> addcommand (c); // cout <"start...: "<current_time; // E-> Run (); // periodic delayedtyper: Engine-> addcommand (New delayedtyper (0.1, '1'); delayedtyper :: engine-> addcommand (New delayedtyper (1, '2'); delayedtyper: Engine-> addcommand (New delayedtyper (3, '3 ')); command * stop_command = new stopcommand; delayedtyper: Engine-> addcommand (New sleepcommmand (10, delayedtyper: Engine, stop_command); delayedtyper: Engine-> Run (); return 0 ;}