-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPCRunloop.m
More file actions
91 lines (85 loc) · 3.8 KB
/
Copy pathPCRunloop.m
File metadata and controls
91 lines (85 loc) · 3.8 KB
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
classdef PCRunloop < handle
% PCRUNLOOP is a class that take control of whole application
% PCRunloop is the core in PCKit
% when using PCRunloop, you need to register event in PCRunloop ...
% instance, and then call the run() method, the PCRunloop will ...
% automatically check every registered events in every loop and call...
% the callback when one events' fireJudger() return true.
% notice: you can register more than one callback within one event by
% calling register using the same event and different callback.
% Actually as long as the event's name is the same, the PCRunloop
% treats them as the same event even if the fireJudger is different(the
% first fireJudger will be recorded)
properties
startTime % the runloop's start time
eventArray % registered event and callback are saved here
% every row's first column is the event
% the rest column is the event's callback
endJudger
endCallback
forceEndFlag % set this value to true can force end the runloop
end
methods
function obj = PCRunloop(endJudger, endCallback)
obj.startTime = GetSecs();
obj.eventArray = {};
obj.endJudger = endJudger;
obj.endCallback = endCallback;
obj.forceEndFlag = false;
end
function register(obj, event, callback)
found = false;
for eventIndex = 1:size(obj.eventArray, 1)
if strcmp(obj.eventArray{eventIndex, 1}.eventName, event.eventName) == 1
found = true;
hasAdded = false;
for callbackIndex = 2:size(obj.eventArray, 2)
if ~isa(obj.eventArray{eventIndex, callbackIndex}, 'function_handle')
hasAdded = true;
obj.eventArray{eventIndex, callbackIndex} = callback;
break;
end
end
if ~hasAdded
obj.eventArray{eventIndex, size(obj. eventArray, 2) + 1} = callback;
end
break;
end
end
if ~found
obj.eventArray{size(obj.eventArray, 1) + 1, 1} = event;
obj.eventArray{size(obj.eventArray, 1), 2} = callback;
end
end
function isDeregistered = deregister(obj, eventName)
isDeregistered = false;
for eventIndex = 1:size(obj.eventArray, 1)
if strcmp(obj.eventArray{eventIndex, 1}.eventName, eventName) == 1
obj.eventArray(eventIndex, :) = [];
isDeregistered = true;
break;
end
end
end
function run(obj)
while ~obj.endJudger(obj) && ~obj.forceEndFlag
eventIndex = 1;
while eventIndex <= size(obj.eventArray, 1)
event = obj.eventArray{eventIndex, 1};
if event.fireJudger(obj)
callbackIndex = 2;
while callbackIndex <= size(obj.eventArray, 2) && isa(obj.eventArray{eventIndex, callbackIndex}, 'function_handle')
obj.eventArray{eventIndex, callbackIndex}();
callbackIndex = callbackIndex + 1;
end
if eventIndex <= size(obj.eventArray, 1) && ~obj.eventArray{eventIndex, 1}.repeat
obj.eventArray(eventIndex, :) = [];
end
end
eventIndex = eventIndex + 1;
end
end
obj.endCallback();
end
end
end