From: Radosław Korzeniewski Date: Fri, 23 Apr 2021 11:28:47 +0000 (+0200) Subject: pluginlib: Update smart_ptr class. X-Git-Tag: Release-11.3.2~564 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc20075962bc366383c63820564c72d469fdb301;p=thirdparty%2Fbacula.git pluginlib: Update smart_ptr class. 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. --- diff --git a/bacula/src/plugins/fd/pluginlib/Makefile b/bacula/src/plugins/fd/pluginlib/Makefile index ec741f92a..a9853cc76 100644 --- a/bacula/src/plugins/fd/pluginlib/Makefile +++ b/bacula/src/plugins/fd/pluginlib/Makefile @@ -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 diff --git a/bacula/src/plugins/fd/pluginlib/smartptr.h b/bacula/src/plugins/fd/pluginlib/smartptr.h index 32cf68c2a..1ebeea70d 100644 --- a/bacula/src/plugins/fd/pluginlib/smartptr.h +++ b/bacula/src/plugins/fd/pluginlib/smartptr.h @@ -20,14 +20,14 @@ * @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 diff --git a/bacula/src/plugins/fd/pluginlib/smartptr_test.cpp b/bacula/src/plugins/fd/pluginlib/smartptr_test.cpp index 0b44fe50c..49f6936af 100644 --- a/bacula/src/plugins/fd/pluginlib/smartptr_test.cpp +++ b/bacula/src/plugins/fd/pluginlib/smartptr_test.cpp @@ -20,10 +20,10 @@ * @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 ptr; + ok(ptr.get() == NULL, "check default constructor"); + ok(referencenumber == 0, "check refnr"); + } + + referencenumber = 0; // ensure it is zero { smart_ptr 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 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(); }