Basic UniquePtr functionality and tests

This commit is contained in:
karl 2019-11-28 11:42:21 +01:00
commit 027b560053
2 changed files with 123 additions and 0 deletions

6
CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.15)
project(UniquePtr)
set(CMAKE_CXX_STANDARD 17)
add_executable(UniquePtr main.cpp)

117
main.cpp Normal file
View File

@ -0,0 +1,117 @@
#include <iostream>
class TestObject {
public:
TestObject() {
std::cout << "Constructor" << std::endl;
instanceCount += 1;
}
TestObject(const TestObject &other) {
std::cout << "Copy constructor" << std::endl;
}
~TestObject() {
std::cout << "Destructor" << std::endl;
instanceCount -= 1;
}
static void testPrint() {
std::cout << "testPrint" << std::endl;
}
static int getInstanceCount() {
return instanceCount;
}
private:
static int instanceCount;
};
int TestObject::instanceCount = 0;
/// Holds unique ownership of a pointed to object and destroys it automatically
template<class T>
class UniquePtr {
public:
explicit UniquePtr(T *object) : object(object) {}
/// Copy constructor
/// Shouldn't be used, since the pointer wouldn't be unique otherwise
UniquePtr(const UniquePtr &other) = delete;
/// Move constructor
UniquePtr(UniquePtr &&other) : object(other.object) {
other.object = nullptr;
}
/// Copy assignment operator
UniquePtr &operator=(const UniquePtr &) = delete;
/// Move assignment operator
/// Shouldn't be used, since the pointer wouldn't be unique otherwise
UniquePtr &operator=(UniquePtr &&) = delete;
/// Destructor
virtual ~UniquePtr() {
delete object;
}
T operator*() const {
return *object;
}
operator bool() const {
return object != nullptr;
}
T *operator->() const {
return object;
}
/// Release ownership of the owned object and return the pointer to it
T *Release() {
T *returnObject = object;
object = nullptr;
return returnObject;
}
/// Reset the internal pointer to null
void Reset() {
delete object;
object = nullptr;
}
/// Swaps the currently owned object with another
void Swap(T *other) {
delete object;
object = other;
}
private:
T *object;
};
int main() {
{
std::cout << "Constructing first pointer" << std::endl;
UniquePtr pointer = UniquePtr<TestObject>(new TestObject());
std::cout << "Calling test print via pointer" << std::endl;
pointer->testPrint();
std::cout << "Swapping for new object" << std::endl;
pointer.Swap(new TestObject());
std::cout << "Move constructing new pointer" << std::endl;
UniquePtr pointer2 = UniquePtr<TestObject>(std::move(pointer));
}
std::cout << std::endl << "All pointers should now be out of scope!" << std::endl;
std::cout << TestObject::getInstanceCount() << " instances left" << std::endl;
return 0;
}