Custom deleter

This commit is contained in:
karl 2019-11-28 12:09:49 +01:00
parent 027b560053
commit 5fbac41ab2

View File

@ -34,14 +34,18 @@ int TestObject::instanceCount = 0;
template<class T> template<class T>
class UniquePtr { class UniquePtr {
public: public:
explicit UniquePtr(T *object) : object(object) {} /// Constructor without deleter
explicit UniquePtr(T *object) : object(object), deleter(nullptr) {}
/// Constructor with deleter
UniquePtr(T *object, void (*deleter)(T*)) : object(object), deleter(deleter) {}
/// Copy constructor /// Copy constructor
/// Shouldn't be used, since the pointer wouldn't be unique otherwise /// Shouldn't be used, since the pointer wouldn't be unique otherwise
UniquePtr(const UniquePtr &other) = delete; UniquePtr(const UniquePtr &other) = delete;
/// Move constructor /// Move constructor
UniquePtr(UniquePtr &&other) : object(other.object) { UniquePtr(UniquePtr &&other) : object(other.object), deleter(other.deleter) {
other.object = nullptr; other.object = nullptr;
} }
@ -54,7 +58,7 @@ public:
/// Destructor /// Destructor
virtual ~UniquePtr() { virtual ~UniquePtr() {
delete object; deleteObject();
} }
T operator*() const { T operator*() const {
@ -79,35 +83,61 @@ public:
/// Reset the internal pointer to null /// Reset the internal pointer to null
void Reset() { void Reset() {
delete object; deleteObject();
object = nullptr; object = nullptr;
} }
/// Swaps the currently owned object with another /// Swaps the currently owned object with another
void Swap(T *other) { void Swap(T *other) {
delete object; deleteObject();
object = other; object = other;
} }
private: private:
T *object; T *object;
void (*deleter)(T*);
void deleteObject() {
if (deleter != nullptr && object != nullptr) {
deleter(object);
} else {
delete object;
}
}
}; };
void customTestObjectDeleter(TestObject *object) {
std::cout << "Custom deleter" << std::endl;
delete object;
}
int main() { int main() {
{ {
std::cout << "Constructing first pointer" << std::endl; std::cout << "Constructing first pointer" << std::endl;
UniquePtr pointer = UniquePtr<TestObject>(new TestObject()); UniquePtr pointer = UniquePtr<TestObject>(new TestObject());
std::cout << "Calling test print via pointer" << std::endl; std::cout << "Calling test print via pointer if it bools to true" << std::endl;
if (pointer) {
pointer->testPrint(); pointer->testPrint();
}
std::cout << "Swapping for new object" << std::endl; std::cout << "Swapping for new object" << std::endl;
pointer.Swap(new TestObject()); pointer.Swap(new TestObject());
std::cout << "Move constructing new pointer" << std::endl; std::cout << "Move constructing new pointer" << std::endl;
UniquePtr pointer2 = UniquePtr<TestObject>(std::move(pointer)); UniquePtr pointer2 = UniquePtr<TestObject>(std::move(pointer));
std::cout << "Constructing pointer with custom deleter" << std::endl;
UniquePtr pointer3 = UniquePtr<TestObject>(new TestObject(), customTestObjectDeleter);
std::cout << "Resetting that pointer" << std::endl;
pointer3.Reset();
if (pointer3) {
std::cout << "This shouldn't happen!" << std::endl;
}
} }
std::cout << std::endl << "All pointers should now be out of scope!" << std::endl; std::cout << std::endl << "All pointers should now be out of scope!" << std::endl;