-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncrementorT.h
362 lines (294 loc) · 5.32 KB
/
IncrementorT.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
(c) 2012-2013 Nicolaus Anderson
Generated from Incrementer.h on: Jan 1, 2012
Modified Nov 11, 2019
Incrementor - A simple incrementing class for keeping track of changes in a value
with fewer keystrokes.
*/
#ifndef __INCREMENTOR__
#define __INCREMENTOR__
template<class Num>
class Inc
{
public:
enum ECycleType
{
/* This incrementor is to act like a boolean value, switching back and forth
between 0 and 1. */
CYC_BOOLEAN = 0,
/* This incrementor must be set to the min value after reaching the max value
or must be set to the max value if decremented from the min value. */
CYC_REPEAT,
/* This incrementor counts up and down like an ordinary integer. */
CYC_DEFAULT
};
private:
Num value;
Num step;
Num min;
Num max;
ECycleType cycle;
public:
//! Constructor
Inc(
ECycleType type = CYC_DEFAULT,
Num start_val=0, Num start_step=1, Num start_min=0, Num start_max=1
)
: value( start_val ), step( start_step ), min(start_min), max( start_max ), cycle( type )
{}
//! Deconstructor
~Inc() {}
//! Get type of cycle
ECycleType getCycleType()
{
return cycle;
}
//! Set the cycle type directly
void setCycleType( ECycleType type )
{
cycle = type;
}
//! Set the cycle type to the default
void cycleDefault()
{
cycle = CYC_DEFAULT;
}
//! Set the cycle type to boolean
void cycleBoolean()
{
cycle = CYC_BOOLEAN;
}
//! Set the cycle type to repeat
void cycleRepeat()
{
cycle = CYC_REPEAT;
}
//! Checks if out of bounds - fixes if necessary
bool outOfBounds()
{
switch ( cycle )
{
case CYC_BOOLEAN:
if ( value > 1 )
{
value = 0;
return true;
} else if ( value < 0 )
{
value = 1;
return true;
}
break;
case CYC_REPEAT:
if ( value > max )
{
// Wrap around if greater than the max
value = (value - max) + min;
return true;
} else if ( value < min )
{
// Wrap around if greater than the max
value = (value - max) + min;
return true;
}
default:
if ( value < min || value > max )
return true;
break;
}
return false;
}
//******* Getters and setters **********
Num& Val()
{
return value;
}
Num& getStep()
{
return step;
}
Num& getMin()
{
return min;
}
Num& getMax()
{
return max;
}
void setVal( Num new_val )
{
value = new_val;
}
void setStep( Num new_step )
{
step = new_step;
}
void setMin( Num new_min )
{
min = new_min;
clampVal();
}
void setMax( Num new_max )
{
max = new_max;
clampVal();
}
void setMinShiftMax( Num new_min )
{
max += new_min - min;
min = new_min;
clampVal();
}
void setMaxShiftMin( Num new_max )
{
min += new_max - max;
max = new_max;
clampVal();
}
void setRange( Num new_min, Num new_max )
{
min = new_min;
max = new_max;
clampVal();
}
// Range modifiers
#ifdef __RANGE_CLASS__
Range<Num> getRange()
{
return Range<Num>( min, max );
}
void setRange( const Range<Num>& range )
{
min = range.start;
max = range.end;
clampVal();
}
Range<Num>& operator= ( const Range<Num>& range )
{
min = range.start;
max = range.end;
clampVal();
return *this;
}
#endif
void restart()
{
value = min;
}
void clampVal()
{
if ( min > max )
{
Num m = min;
min = max;
max = m;
}
if ( value < min )
value = min;
else if ( max < value )
value = max;
}
// ********* Shortcut operators ***********
bool operator++ ()
{
switch( cycle )
{
case CYC_BOOLEAN:
value++;
break;
case CYC_REPEAT:
if ( value == max )
{
value = min;
return true;
} else {
value += step;
}
break;
default:
value += step;
}
return outOfBounds();
}
bool operator-- ()
{
switch( cycle )
{
case CYC_BOOLEAN:
value--;
break;
case CYC_REPEAT:
if ( value == min )
{
value = max;
return true;
} else {
value -= step;
}
break;
default:
value -= step;
}
return outOfBounds();
}
bool operator==( Inc<Num>& other )
{
return value == other.value;
}
bool operator>( Inc<Num>& other )
{
return value > other.value;
}
bool operator>=( Inc<Num>& other )
{
return value >= other.value;
}
bool operator<( Inc<Num>& other )
{
return value < other.value;
}
bool operator<=( Inc<Num>& other )
{
return value <= other.value;
}
Inc<Num>& operator=( Inc<Num>& other )
{
return copy(other);
}
Inc<Num>& copy( Inc<Num>& other )
{
value = other.value;
step = other.step;
min = other.min;
max = other.max;
cycle = other.cycle;
return *this;
}
//****** assignment operators *******
Inc<Num>& operator+=( Inc<Num>& other )
{
value += other.value;
return *this;
}
Inc<Num>& operator-=( Inc<Num>& other )
{
value -= other.value;
return *this;
}
Inc<Num>& operator=( Num val )
{
value = val;
return *this;
}
Inc<Num>& operator+=( Num val )
{
value += val;
return *this;
}
Inc<Num>& operator-=( Num val )
{
value -= val;
return *this;
}
};
#endif // define __INCREMENTOR__