fixed hooks a bit

This commit is contained in:
2021-05-10 21:52:11 +02:00
parent 32d3130b12
commit 524354075a
2 changed files with 49 additions and 12 deletions
+5 -12
View File
@@ -49,7 +49,6 @@ struct Lambda<R(Args...)>
}
};
struct Hook {
Array_List<Lambda<void()>> lambdas;
Hook() {
@@ -58,20 +57,14 @@ struct Hook {
~Hook () {
lambdas.dealloc();
}
void operator<<(Lambda<void()> f) {
// FIXME(Felix): Why can I not call Array_List::append here??? Hallo?
if (lambdas.count == lambdas.length) {
lambdas.length *= 2;
lambdas.data = (Lambda<void()>*)realloc(lambdas.data, lambdas.length * sizeof(Lambda<void()>));
}
lambdas.data[lambdas.count] = f;
lambdas.count++;
void operator<<(Lambda<void()>&& f) {
lambdas.append(f);
}
void operator()() {
while(lambdas.count --> 0) {
lambdas.data[lambdas.count]();
for (auto l : lambdas) {
l();
}
lambdas.count = 0;
lambdas.clear();
}
};
+44
View File
@@ -560,6 +560,49 @@ auto test_array_list_sort_many() -> testresult {
return pass;
}
auto test_hooks() -> testresult {
s32 a = 0;
s32 b = 0;
s32 c = 0;
Hook hook;
hook << [&]() {
a = 1;
};
hook << [&]() {
// NOTE(Felix): assert correct execution order
if (a == 1 && c == 0) {
b = 2;
}
};
hook << [&]() {
c = 3;
};
assert_equal_int(a, 0);
assert_equal_int(b, 0);
assert_equal_int(c, 0);
hook();
assert_equal_int(a, 1);
assert_equal_int(b, 2);
assert_equal_int(c, 3);
a = 0;
b = 0;
c = 0;
// NOTE(Felix): hook should be empty now
hook();
assert_equal_int(a, 0);
assert_equal_int(b, 0);
assert_equal_int(c, 0);
return pass;
}
s32 main(s32, char**) {
init_printer();
testresult result;
@@ -571,6 +614,7 @@ s32 main(s32, char**) {
invoke_test(test_stack_array_lists);
invoke_test(test_bucket_allocator);
invoke_test(test_queue);
invoke_test(test_hooks);
return 0;
}