From: Radosław Korzeniewski Date: Tue, 5 Jan 2021 17:06:41 +0000 (+0100) Subject: pluginlib: Update commctx to handle foreach status. X-Git-Tag: Release-11.3.2~727 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed2d47ff0076fd8a5c6b1b5e54bd02a46015e189;p=thirdparty%2Fbacula.git pluginlib: Update commctx to handle foreach status. --- diff --git a/bacula/src/plugins/fd/pluginlib/commctx.h b/bacula/src/plugins/fd/pluginlib/commctx.h index 1a5ec3b99..6c8e20f9d 100644 --- a/bacula/src/plugins/fd/pluginlib/commctx.h +++ b/bacula/src/plugins/fd/pluginlib/commctx.h @@ -20,14 +20,15 @@ * @file commctx.h * @author Radosław Korzeniewski (radoslaw@korzeniewski.net) * @brief This is a Bacula plugin command context switcher template. - * @version 1.1.0 - * @date 2020-12-23 + * @version 1.2.0 + * @date 2020-01-05 * - * @copyright Copyright (c) 2020 All rights reserved. IP transferred to Bacula Systems according to agreement. + * @copyright Copyright (c) 2021 All rights reserved. + * IP transferred to Bacula Systems according to agreement. */ -#ifndef COMMCTX_H -#define COMMCTX_H +#ifndef PLUGINLIB_COMMCTX_H +#define PLUGINLIB_COMMCTX_H #include "pluginlib.h" #include "smartalist.h" @@ -67,6 +68,7 @@ public: T * switch_command(const char *command); bool check_command(const char *command); void foreach_command(void (*func)(T *, void *), void *param); + bRC foreach_command_status(bRC (*func)(T *, void *), void *param); }; @@ -135,4 +137,27 @@ void COMMCTX::foreach_command(void(*func)(T*, void*), void* param) } } -#endif /* COMMCTX_H */ +/** + * @brief Iterate on all command context to execute function and return status. + * + * @tparam T is command context type. + * @param param is the execution function param. + */ +template +bRC COMMCTX::foreach_command_status(bRC(*func)(T*, void*), void* param) +{ + CMD * cmdctx; + bRC status = bRC_OK; + + foreach_alist(cmdctx, &_command_list) + { + ctx = cmdctx->ptr; + bRC rc = func(ctx, param); + if (rc != bRC_OK) + status = rc; + } + + return status; +} + +#endif // PLUGINLIB_COMMCTX_H diff --git a/bacula/src/plugins/fd/pluginlib/commctx_test.cpp b/bacula/src/plugins/fd/pluginlib/commctx_test.cpp index 4f91dc7b0..120db3743 100644 --- a/bacula/src/plugins/fd/pluginlib/commctx_test.cpp +++ b/bacula/src/plugins/fd/pluginlib/commctx_test.cpp @@ -34,6 +34,7 @@ bFuncs *bfuncs; bInfo *binfo; static int referencenumber = 0; +static int dosomethingvariable = 0; struct testctx : public SMARTALLOC { @@ -42,6 +43,27 @@ struct testctx : public SMARTALLOC ~testctx() { referencenumber--; };; }; +void do_something(testctx*, void*data) +{ + dosomethingvariable++; + + if (data != NULL) + { + int *var = (int *)data; + dosomethingvariable += *var; + } +} + +bRC do_status(testctx*, void*data) +{ + if (data != NULL) + { + return bRC_OK; + } + + return bRC_Error; +} + int main() { Unittests pluglib_test("commctx_test"); @@ -54,7 +76,7 @@ int main() { COMMCTX ctx; - nok(ctx.check_command("TEST1"), "test empty ctx list"); + nok(ctx.check_command(TEST1), "test empty ctx list"); ok(referencenumber == 0, "check no allocation yet"); auto testctx1 = ctx.switch_command(TEST1); @@ -75,5 +97,31 @@ int main() ok(referencenumber == 0, "check smart free"); + { + COMMCTX ctx; + + auto testctx1 = ctx.switch_command(TEST1); + ok(testctx1 != nullptr, "test switch command1"); + ok(referencenumber == 1, "check ref allocation1"); + + auto testctx2 = ctx.switch_command(TEST2); + ok(testctx2 != nullptr, "test switch command2"); + ok(referencenumber == 2, "check allocation2"); + + int append = 2; + ctx.foreach_command(do_something, &append); + ok(dosomethingvariable == 6, "dosomethingvariable"); + + dosomethingvariable = 0; + ctx.foreach_command(do_something, NULL); + ok(dosomethingvariable == 2, "do_something with NULL"); + + auto status = ctx.foreach_command_status(do_status, &append); + ok(status != bRC_Error, "do_status"); + + status = ctx.foreach_command_status(do_status, NULL); + ok(status == bRC_Error, "do_status with NULL"); + } + return report(); }