-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunLoopObject.cpp
More file actions
186 lines (152 loc) · 3.86 KB
/
Copy pathRunLoopObject.cpp
File metadata and controls
186 lines (152 loc) · 3.86 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
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
183
184
185
186
/*
* RunLoopObject.cpp
*
* Copyright (c) 2017 by Sebastien MARCHAND (Web:www.marscaper.com, Email:sebastien@marscaper.com)
*/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RunLoopObject.hpp"
#pragma mark -
#pragma mark Memory management
RunLoopObject::RunLoopObject()
{
}
RunLoopObject::~RunLoopObject()
{
free(_runLoopObjectArray);
}
#pragma mark -
#pragma mark Run loop management
bool RunLoopObject::removeFromRunLoop(RunLoopObject *object, bool deleteObject)
{
for( unsigned short numObject=0; numObject<_runLoopObjectCount; numObject++ )
{
RunLoopObject *currentObject = _runLoopObjectArray[numObject];
if( currentObject == object )
{
// Unassign object
_runLoopObjectArray[numObject] = NULL;
// Delete if needed
if( deleteObject )
{
delete currentObject;
}
return true;
}
}
return false;
}
bool RunLoopObject::removeFromRunLoop(RunLoopObject *object)
{
return this->removeFromRunLoop(object,false);
}
bool RunLoopObject::addToRunLoop(RunLoopObject *object)
{
if( object->ancestor() )
{
// Object already in another run loop.
return false;
}
for( unsigned short numObject=0; numObject<_runLoopObjectCount; numObject++ )
{
RunLoopObject *currentObject = _runLoopObjectArray[numObject];
if( object == currentObject )
{
// Object already loaded in run loop.
return false;
}
else if ( currentObject == NULL )
{
// Space available. We use It.
_runLoopObjectArray[numObject] = object;
// Set the ancestor
object->setAncestor(this);
return true;
}
}
// Should we need to resize the array?
if( _runLoopObjectCount == 0 )
{
_runLoopObjectArray = (RunLoopObject**)malloc(sizeof(RunLoopObject*));
}
else
{
_runLoopObjectArray = (RunLoopObject**)realloc(_runLoopObjectArray,sizeof(RunLoopObject*)*(_runLoopObjectCount+1));
}
// Set the ancestor
object->setAncestor(this);
// Add object to run loop array...
_runLoopObjectArray[_runLoopObjectCount] = object;
_runLoopObjectCount++;
return true;
}
#pragma mark -
#pragma mark Virtual method for overwrite.
void RunLoopObject::setIdle(bool state)
{
if( state != _isIdle )
{
_isIdle = state;
}
}
void RunLoopObject::loop()
{
if( _isIdle == true )
{
return;
}
for( unsigned short numObject=0; numObject<_runLoopObjectCount; numObject++ )
{
RunLoopObject *currentObject = _runLoopObjectArray[numObject];
if( currentObject->isIdle() == false )
{
currentObject->loop();
}
}
}
#pragma mark -
#pragma mark Memory management
void RunLoopObject::smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
// Execute run loop for registered objects
this->RunLoopObject::loop();
}while(millis() - start < ms);
}
void RunLoopObject::setAncestor(RunLoopObject *ancestor)
{
_ancestor = ancestor;
}
bool RunLoopObject::areAncestorsIdle()
{
bool idle = false;
if( _ancestor )
{
RunLoopObject *ancestor = this->ancestor();
while( ancestor )
{
if( ancestor->isIdle() )
{
idle = true;
break;
}
// Next ancestor
ancestor = ancestor->ancestor();
}
}
return idle;
}