# Include <windows. h >#include <deque >#include <iostream> # include <boost/shared_ptr.hpp> # include <boost/weak_ptr.hpp> # include <boost/enable_shared_from_this.hpp> using namespace STD; using namespace boost; Class command; Class activeobjectengine; typedef shared_ptr <command> commandptr; typedef shared_ptr <activeobjectengine> activeobjectengineptr; typedef weak_ptr <activeobjectengine> activeobjectengineweakpt R; double secondspertick = 0; Class command: Public enable_shared_from_this <command> {public: Virtual void execute () = 0 ;}; class activeobjectengine {deque <commandptr> itscommands; public: void addcommand (commandptr c) {itscommands. push_back (c);} void run () {While (itscommands. size ()> 0) {commandptr c = itscommands. front (); C-> execute (); itscommands. pop_front () ;}}; class sleepcommmand: Public command {commandptr Wakeupcommand; short engine; double sleeptime; bool started; large_integer lv; double start_time; public: sleepcommmand (double milliseconds, lead E, commandptr WC): started (false) {sleeptime = milliseconds; engine = E; wakeupcommand = WC;} virtual void execute () {queryperformancecounter (& LV); double current_time = secondspertick * lv. quadpart; If (! Started) {started = true; start_time = current_time; activeobjectengineptr strong_ptr = engine. lock (); If (strong_ptr) strong_ptr-> addcommand (partial ();} else {double elasped_time = current_time-start_time; If (elasped_time <sleeptime) {activeobjectengineptr strong_ptr =. lock (); If (strong_ptr) strong_ptr-> addcommand (shared_from_this (); sleep (1);} else {activeobjectengineptr strong_ptr = ENG INE. lock (); If (strong_ptr) strong_ptr-> addcommand (commandptr (wakeupcommand) ;}}; class wakeupcommand: Public command {bool excuted; public: wakeupcommand () {excuted = false;} virtual void execute () {large_integer lv; queryperformancecounter (& LV); double 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 activeobjectengineptr engine; delayedtyper (double delay, char c) {itsdelay = delay; itschar = C ;}virtual void execute () {cout <itschar; If (! Stop) {delayandrepeat () ;}} void delayandrepeat () {commandptr C (New sleepcommmand (itsdelay, engine, shared_from_this ())); engine-> addcommand (c) ;}}; bool delayedtyper: Stop = false; activeobjectengineptr 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/* shared_ptr <wakeupcommand> wakup (New wakeupcommand (); activeobjectengineptr E (New activeobjectengine ()); shared_ptr <sleepcommmand> C (New sleepcommmand (6, E, wakup); e-> addcommand (c); cout <"start...: "<current_time; e-> Run (); * // periodic delayedtyper: Engine-> addcommand (commandptr (New delayedtyper (0.01, '1 '))); delayedtyper: Engine-> addcommand (commandptr (New delayedtyper (1, '2'); delayedtyper: Engine-> addcommand (commandptr (New delayedtyper (3, '3'); commandptr sleep_command (New sleepcommmand (6, delayedtyper: Engine, commandptr (New stopcommand); delayedtyper: Engine-> addcommand (sleep_command ); delayedtyper: Engine-> Run (); Return 0 ;}