Active Object C ++ implementation

Source: Internet
Author: User
// 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.