野百合也有春天

导航

Active Object C++智能指针实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#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> ActiveObjectEngineWeakPtr;
 
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;
    ActiveObjectEngineWeakPtr engine;
    double sleepTime;
    bool started;
    LARGE_INTEGER lv;
    double start_time;
 
public:
    SleepCommmand(double milliseconds, ActiveObjectEngineWeakPtr 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(shared_from_this());
        }
        else
        {
             
            double elasped_time = current_time - start_time;
            if (elasped_time < sleepTime)
            {
                ActiveObjectEnginePtr strong_ptr = engine.lock();
                if (strong_ptr)
                    strong_ptr->AddCommand(shared_from_this());
                Sleep(1);
            }
            else
            {
                ActiveObjectEnginePtr strong_ptr = engine.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;
}

posted on   flydream  阅读(362)  评论(0编辑  收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示