]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
pluginlib: Update smart_ptr class.
authorRadosław Korzeniewski <radoslaw@korzeniewski.net>
Fri, 23 Apr 2021 11:28:47 +0000 (13:28 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:01 +0000 (09:03 +0100)
Now you can create an empty smart_ptr class with default ctor
and assign a new ptr to already defined object and handle it smartly.

bacula/src/plugins/fd/pluginlib/Makefile
bacula/src/plugins/fd/pluginlib/smartptr.h
bacula/src/plugins/fd/pluginlib/smartptr_test.cpp

index ec741f92a725b662dc3d0a78a6a3cc86f2d9218a..a9853cc760a5f2a77bc087a8b018f831d4cc585c 100644 (file)
@@ -23,7 +23,7 @@ PTCOMMSRC = ptcomm.cpp ptcomm.h
 PTCOMMOBJ = $(filter %.lo,$(PTCOMMSRC:.cpp=.lo))
 COMMCTXSRC = commctx.h
 SMARTALISTSRC = smartalist.h
-SMARTPTRSRC = smartalist.h
+SMARTPTRSRC = smartptr.h
 METAPLUGINSRC = metaplugin.cpp metaplugin.h
 METAPLUGINOBJ = $(filter %.lo,$(METAPLUGIN:.cpp=.lo))
 
@@ -121,7 +121,7 @@ libtool-clean:
 
 clean: libtool-clean
        @rm -f main *.so *.o
-       @rm -f $(COMMONPLUGINTESTS)
+       @rm -f $(COMMONPLUGINTESTS) test_metaplugin_backend_fd
 
 distclean: clean
 
index 32cf68c2ae7e662116a21137a41b338f6e64d750..1ebeea70d35c1161323d2a36c0561e2ebd80aec7 100644 (file)
  * @file smartptr.h
  * @author Radosław Korzeniewski (radoslaw@korzeniewski.net)
  * @brief This is a simple smart pointer guard conceptually based on C++11 smart pointers - unique_ptr.
- * @version 1.1.0
- * @date 2020-12-23
+ * @version 1.2.0
+ * @date 2021-04-23
  *
- * @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 _SMARTPTR_H_
-#define _SMARTPTR_H_
+#ifndef PLUGINLIB_SMARTPTR_H
+#define PLUGINLIB_SMARTPTR_H
 
 /**
  * @brief This is a simple smart pointer based conceptually on C++11 smart unique pointer.
@@ -39,16 +39,25 @@ class smart_ptr
 {
    T * _ptr;
 public:
-   smart_ptr(T *ptr) : _ptr(ptr) {};
+   smart_ptr() : _ptr(NULL) {}
+   smart_ptr(T *ptr) : _ptr(ptr) {}
    ~smart_ptr()
    {
-      if (_ptr != NULL){
+      if (_ptr != NULL) {
          delete _ptr;
          _ptr = NULL;
       }
-   };
+   }
 
-   T * get() { return _ptr; };
+   T * get() { return _ptr; }
+   smart_ptr& operator=(T* p)
+   {
+      if (_ptr != NULL) {
+         delete _ptr;
+      }
+      _ptr = p;
+      return *this;
+   }
 };
 
-#endif   /* _SMARTPTR_H_ */
+#endif   // PLUGINLIB_SMARTPTR_H
index 0b44fe50cbdb5fa93cd327ea119660ee7f752109..49f6936aff6bbfc87921412027bca21045e9aad3 100644 (file)
  * @file smartptr_test.cpp
  * @author Radosław Korzeniewski (radoslaw@korzeniewski.net)
  * @brief Common definitions and utility functions for Inteos plugins - unittest.
- * @version 1.1.0
- * @date 2020-12-23
+ * @version 1.2.0
+ * @date 2021-04-23
  *
- * @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.
  */
 
 #include "pluginlib.h"
@@ -34,11 +34,12 @@ bFuncs *bfuncs;
 bInfo *binfo;
 
 static int referencenumber = 0;
+static int referencenumber2 = 0;
 
 struct testclass
 {
-   testclass() { referencenumber++; };
-   ~testclass() { referencenumber--; };
+   testclass() { referencenumber++; }
+   ~testclass() { referencenumber--; }
 };
 
 int main()
@@ -47,6 +48,14 @@ int main()
 
    // Pmsg0(0, "Initialize tests ...\n");
 
+   referencenumber = 0;    // ensure it is zero
+   {
+      smart_ptr<testclass> ptr;
+      ok(ptr.get() == NULL, "check default constructor");
+      ok(referencenumber == 0, "check refnr");
+   }
+
+   referencenumber = 0;    // ensure it is zero
    {
       smart_ptr<testclass> ptr(new testclass);
       ok(referencenumber == 1, "check smart allocation");
@@ -54,5 +63,18 @@ int main()
 
    ok(referencenumber == 0, "check smart free");
 
+   referencenumber = 0;    // ensure it is zero
+   referencenumber2 = 0;   // ensure it is zero
+   {
+      smart_ptr<testclass> ptr(new testclass);
+      ok(referencenumber == 1, "check first allocation");
+      testclass * other = new testclass;
+      ok(referencenumber == 2, "check other allocation");
+      ptr = other;
+      ok(referencenumber == 1, "check assign operator");
+   }
+
+   ok(referencenumber == 0, "check smart free");
+
    return report();
 }