• Active Object C++实现


    // ActiveObject.cpp : 定义控制台应用程序的入口点。
    //
    
    #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();
    		}
    	}
    
    };
    
    
    class 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.QuadPart;
    		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 );
    		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 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;
    }
    
    
  • 相关阅读:
    leetcode题目19.删除链表的倒数第N个节点(中等)
    Android学习笔记---使用Service模仿下载效果
    Android学习笔记-简单聊天界面的实现
    Android学习笔记-ContentProvider操作
    Android高级-Android操作SQL数据管理,增删改查
    Android高级-正则表达式
    Android高级-SQL语言
    Android学习之路-录音功能实现
    gitee上传下载代码命令
    opencv进行视频播放每帧处理,读取视频失败
  • 原文地址:https://www.cnblogs.com/lilun/p/1902073.html
Copyright © 2020-2023  润新知