Browse Source

unique pointer and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
9133b9da5e
6 changed files with 108 additions and 37 deletions
  1. 2 0
      Main.cpp
  2. 44 0
      memory/UniquePointer.h
  3. 1 0
      meson.build
  4. 43 0
      tests/UniquePointerTests.cpp
  5. 8 0
      tests/UniquePointerTests.h
  6. 10 37
      utils/Types.h

+ 2 - 0
Main.cpp

@@ -23,6 +23,7 @@
 #include "tests/StackTests.h"
 #include "tests/StringBufferTests.h"
 #include "tests/TypedBufferTests.h"
+#include "tests/UniquePointerTests.h"
 #include "tests/UtilsTests.h"
 #include "tests/VectorTests.h"
 #include "wrapper/Framebuffer.h"
@@ -56,5 +57,6 @@ int main(int argAmount, char** args) {
     StackAllocatorTests::test();
     TypedBufferTests::test();
     ObjectPoolTests::test();
+    UniquePointerTests::test();
     return 0;
 }

+ 44 - 0
memory/UniquePointer.h

@@ -0,0 +1,44 @@
+#ifndef UNIQUEPOINTER_H
+#define UNIQUEPOINTER_H
+
+template<typename T>
+class UniquePointer {
+    T* t;
+
+public:
+    UniquePointer(T* t) : t(t) {
+    }
+
+    UniquePointer() : UniquePointer(nullptr) {
+    }
+
+    ~UniquePointer() {
+        delete t;
+    }
+
+    UniquePointer(const UniquePointer&) = delete;
+    UniquePointer& operator=(const UniquePointer&) = delete;
+
+    UniquePointer(UniquePointer&& other) : UniquePointer(other.t) {
+        other.t = nullptr;
+    }
+
+    UniquePointer& operator=(UniquePointer&& other) {
+        if(this != &other) {
+            delete t;
+            t = other.t;
+            other.t = nullptr;
+        }
+        return *this;
+    };
+
+    T* operator->() {
+        return t;
+    }
+
+    const T* operator->() const {
+        return t;
+    }
+};
+
+#endif

+ 1 - 0
meson.build

@@ -50,6 +50,7 @@ sources = ['Main.cpp',
     'memory/StackAllocator.cpp',
     'tests/StackAllocatorTests.cpp',
     'tests/ObjectPoolTests.cpp',
+    'tests/UniquePointerTests.cpp',
     'utils/Buffer.cpp']
 
 glewDep = dependency('glew')

+ 43 - 0
tests/UniquePointerTests.cpp

@@ -0,0 +1,43 @@
+#include "tests/UniquePointerTests.h"
+#include "memory/UniquePointer.h"
+#include "tests/Test.h"
+
+struct B {
+    static int instances;
+
+    B() {
+        instances++;
+    }
+
+    ~B() {
+        instances--;
+    }
+};
+
+int B::instances = 0;
+
+static void testDestroy(Test& test) {
+    {
+        UniquePointer<B> p(new B());
+        test.checkEqual(1, B::instances, "one instance");
+    }
+    test.checkEqual(0, B::instances, "instance is destroyed");
+}
+
+static void testMoveDestroys(Test& test) {
+    {
+        UniquePointer<B> p1(new B());
+        UniquePointer<B> p2(new B());
+        test.checkEqual(2, B::instances, "two instances");
+        p1 = std::move(p2);
+        test.checkEqual(1, B::instances, "one after move");
+    }
+    test.checkEqual(0, B::instances, "everything destroyed correctly after move");
+}
+
+void UniquePointerTests::test() {
+    Test test("UniquePointer");
+    testDestroy(test);
+    testMoveDestroys(test);
+    test.finalize();
+}

+ 8 - 0
tests/UniquePointerTests.h

@@ -0,0 +1,8 @@
+#ifndef UNIQUEPOINTERTESTS_H
+#define UNIQUEPOINTERTESTS_H
+
+namespace UniquePointerTests {
+    void test();
+}
+
+#endif

+ 10 - 37
utils/Types.h

@@ -1,42 +1,15 @@
 #ifndef TYPES_H
 #define TYPES_H
 
-namespace NumberSize {
-    template<bool condition, typename IfTrue, typename IfFalse>
-    struct Conditional {
-        typedef IfTrue dummy;
-    };
-
-    template<typename IfTrue, typename IfFalse>
-    struct Conditional<false, IfTrue, IfFalse> {
-        typedef IfFalse dummy;
-    };
-
-    template<int N, typename A, typename B>
-    using SizeCheck = Conditional<sizeof(A) == N, A, B>;
-
-    template<int N>
-    using Signed =
-        SizeCheck<N, long long,
-                  typename SizeCheck<
-                      N, long, typename SizeCheck<N, int, typename SizeCheck<N, short, char>::dummy>::dummy>::dummy>;
-
-    template<int N>
-    using Unsigned =
-        SizeCheck<N, unsigned long long,
-                  typename SizeCheck<
-                      N, unsigned long,
-                      typename SizeCheck<N, unsigned int,
-                                         typename SizeCheck<N, unsigned short, unsigned char>::dummy>::dummy>::dummy>;
-}
-
-typedef NumberSize::Signed<8>::dummy int64;
-typedef NumberSize::Signed<4>::dummy int32;
-typedef NumberSize::Signed<2>::dummy int16;
-typedef NumberSize::Signed<1>::dummy int8;
-typedef NumberSize::Unsigned<8>::dummy uint64;
-typedef NumberSize::Unsigned<4>::dummy uint32;
-typedef NumberSize::Unsigned<2>::dummy uint16;
-typedef NumberSize::Unsigned<1>::dummy uint8;
+#include <cinttypes>
+
+typedef int64_t int64;
+typedef int32_t int32;
+typedef int16_t int16;
+typedef int8_t int8;
+typedef uint64_t uint64;
+typedef uint32_t uint32;
+typedef uint16_t uint16;
+typedef uint8_t uint8;
 
 #endif