1 module mutils.timeline.executor; 2 3 import mutils.container.sorted_vector; 4 import mutils.container.vector; 5 import mutils.linalg.algorithm; 6 import mutils.timeline.utils; 7 8 alias ExecuteDelegate = void delegate(); 9 10 struct ExecuteUnit { 11 ExecuteDelegate del; 12 float time; 13 } 14 15 struct Executor { 16 SortedVector!(ExecuteUnit, "a.time < b.time") toExecute; 17 TimeIndexGetter indexGetter; 18 19 void executeTo(float time) { 20 ExecuteUnit[] exe = indexGetter.passedFromLast(toExecute[], time); 21 //writeln(exe); 22 foreach (e; exe) { 23 e.del(); 24 } 25 } 26 27 void executeFromTo(float start, float end) { 28 indexGetter.set(toExecute[], start); 29 executeTo(end); 30 } 31 } 32 33 unittest { 34 static struct Tmp { 35 int value; 36 37 void something() { 38 value += 1; 39 } 40 } 41 42 Tmp tmp; 43 44 Executor exe; 45 exe.toExecute ~= ExecuteUnit(&tmp.something, 0); 46 exe.toExecute ~= ExecuteUnit(&tmp.something, 0); 47 exe.toExecute ~= ExecuteUnit(&tmp.something, 0); 48 exe.toExecute ~= ExecuteUnit(&tmp.something, 1); 49 exe.toExecute ~= ExecuteUnit(&tmp.something, 2); 50 exe.toExecute ~= ExecuteUnit(&tmp.something, 3); 51 exe.toExecute ~= ExecuteUnit(&tmp.something, 4); 52 //Check executeTo 53 exe.executeTo(0.1); 54 exe.executeTo(0.1); 55 assert(tmp.value == 3); 56 exe.executeTo(1.1); 57 assert(tmp.value == 4); 58 exe.executeTo(2.1); 59 assert(tmp.value == 5); 60 exe.executeTo(5); 61 assert(tmp.value == 7); 62 //Check executeFromTo 63 tmp.value = 0; 64 exe.executeFromTo(2, 10); 65 assert(tmp.value == 3); 66 tmp.value = 0; 67 exe.executeFromTo(0, 10); 68 assert(tmp.value == 7); 69 }