From: Radosław Korzeniewski Date: Wed, 23 Dec 2020 10:25:51 +0000 (+0100) Subject: pluginlib: Add common smart class templates. X-Git-Tag: Release-11.3.2~737 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8bf039a3910a23ad46ce17608560d1979ddbb7b;p=thirdparty%2Fbacula.git pluginlib: Add common smart class templates. --- diff --git a/bacula/src/plugins/fd/pluginlib/smartalist.h b/bacula/src/plugins/fd/pluginlib/smartalist.h new file mode 100644 index 000000000..adb5ad88b --- /dev/null +++ b/bacula/src/plugins/fd/pluginlib/smartalist.h @@ -0,0 +1,58 @@ +/* + Bacula(R) - The Network Backup Solution + + Copyright (C) 2000-2020 Kern Sibbald + + The original author of Bacula is Kern Sibbald, with contributions + from many others, a complete list can be found in the file AUTHORS. + + You may use this file and others of this release according to the + license defined in the LICENSE file, which includes the Affero General + Public License, v3.0 ("AGPLv3") and some additional permissions and + terms pursuant to its AGPLv3 Section 7. + + This notice must be preserved when any source code is + conveyed and/or propagated. + + Bacula(R) is a registered trademark of Kern Sibbald. +*/ +/** + * @file commctx.h + * @author Radosław Korzeniewski (radoslaw@korzeniewski.net) + * @brief This is a simple smart array list (alist) resource guard conceptually based on C++11 - RAII. + * @version 1.1.0 + * @date 2020-12-23 + * + * @copyright Copyright (c) 2020 All rights reserved. IP transferred to Bacula Systems according to agreement. + */ + +#ifndef _SMARTALIST_H_ +#define _SMARTALIST_H_ + + +/** + * @brief This is a simple smart array list (alist) resource guard conceptually based on C++11 - RAII. + * + * @tparam T is a type for a pointer to allocate on smart_alist. + */ +template +class smart_alist : public alist +{ +public: + smart_alist(int num = 10) : alist(num, not_owned_by_alist) {}; + ~smart_alist() + { + T * it; + if (items) { + for (int i = 0; i < max_items; i++) { + if (items[i]) { + it = (T*)items[i]; + items[i] = NULL; + delete it; + } + } + } + }; +}; + +#endif /* _SMARTALIST_H_ */ diff --git a/bacula/src/plugins/fd/pluginlib/smartptr.h b/bacula/src/plugins/fd/pluginlib/smartptr.h new file mode 100644 index 000000000..a08cfdfb3 --- /dev/null +++ b/bacula/src/plugins/fd/pluginlib/smartptr.h @@ -0,0 +1,54 @@ +/* + Bacula(R) - The Network Backup Solution + + Copyright (C) 2000-2020 Kern Sibbald + + The original author of Bacula is Kern Sibbald, with contributions + from many others, a complete list can be found in the file AUTHORS. + + You may use this file and others of this release according to the + license defined in the LICENSE file, which includes the Affero General + Public License, v3.0 ("AGPLv3") and some additional permissions and + terms pursuant to its AGPLv3 Section 7. + + This notice must be preserved when any source code is + conveyed and/or propagated. + + Bacula(R) is a registered trademark of Kern Sibbald. +*/ +/** + * @file commctx.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 + * + * @copyright Copyright (c) 2020 All rights reserved. IP transferred to Bacula Systems according to agreement. + */ + +#ifndef _SMARTPTR_H_ +#define _SMARTPTR_H_ + +/** + * @brief This is a simple smart pointer based conceptually on C++11 smart unique pointer. + * + * @tparam T is a type for a pointer to guard + */ +template +class smart_ptr +{ + T * _ptr; +public: + smart_ptr(T *ptr) : _ptr(ptr) {}; + ~smart_ptr() + { + if (_ptr != NULL){ + delete _ptr; + _ptr = NULL; + } + }; + + T * get() { return _ptr; }; +}; + +#endif /* _SMARTPTR_H_ */