diff --git a/hooks.hpp b/hooks.hpp index 1635e5b..c96e3b5 100644 --- a/hooks.hpp +++ b/hooks.hpp @@ -49,7 +49,6 @@ struct Lambda } }; - struct Hook { Array_List> lambdas; Hook() { @@ -58,20 +57,14 @@ struct Hook { ~Hook () { lambdas.dealloc(); } - void operator<<(Lambda f) { - // FIXME(Felix): Why can I not call Array_List::append here??? Hallo? - if (lambdas.count == lambdas.length) { - lambdas.length *= 2; - lambdas.data = (Lambda*)realloc(lambdas.data, lambdas.length * sizeof(Lambda)); - } - lambdas.data[lambdas.count] = f; - lambdas.count++; + void operator<<(Lambda&& f) { + lambdas.append(f); } void operator()() { - while(lambdas.count --> 0) { - lambdas.data[lambdas.count](); + for (auto l : lambdas) { + l(); } - lambdas.count = 0; + lambdas.clear(); } }; diff --git a/tests/main.cpp b/tests/main.cpp index 3407d89..00e55a2 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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; }