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))
clean: libtool-clean
@rm -f main *.so *.o
- @rm -f $(COMMONPLUGINTESTS)
+ @rm -f $(COMMONPLUGINTESTS) test_metaplugin_backend_fd
distclean: clean
* @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.
{
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
* @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"
bInfo *binfo;
static int referencenumber = 0;
+static int referencenumber2 = 0;
struct testclass
{
- testclass() { referencenumber++; };
- ~testclass() { referencenumber--; };
+ testclass() { referencenumber++; }
+ ~testclass() { referencenumber--; }
};
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");
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();
}