]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
pluginlib: Update commctx to handle foreach status.
authorRadosław Korzeniewski <radekk@inteos.pl>
Tue, 5 Jan 2021 17:06:41 +0000 (18:06 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:00 +0000 (09:03 +0100)
bacula/src/plugins/fd/pluginlib/commctx.h
bacula/src/plugins/fd/pluginlib/commctx_test.cpp

index 1a5ec3b999f8faa942e65187218281cd64d959b2..6c8e20f9d8122a99301c5d7414be23b34d959218 100644 (file)
  * @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<T>::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<typename T>
+bRC COMMCTX<T>::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
index 4f91dc7b09bbbf6285d0434938e768c675af8740..120db3743a7a0b0cd8115b0b4954c657b3232aea 100644 (file)
@@ -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<testctx> 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<testctx> 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();
 }