From 2b60b2621f7157b4b50b1667b8930941ca68e669 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 5 Mar 2023 12:57:57 -0500 Subject: [PATCH] nullptr_initalizer --- include/blt/std/memory.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index 6dd53e2..6c7b4d6 100644 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -46,6 +46,37 @@ namespace blt { delete[] buffer; } }; + + template + struct nullptr_initializer { + private: + T* m_ptr = nullptr; + public: + nullptr_initializer() = default; + explicit nullptr_initializer(T* ptr): m_ptr(ptr) {} + nullptr_initializer(const nullptr_initializer& ptr): m_ptr(ptr.m_ptr) {} + nullptr_initializer(nullptr_initializer&& ptr) noexcept : m_ptr(ptr.m_ptr) {} + + nullptr_initializer& operator=(const nullptr_initializer& ptr){ + if (&ptr == this) + return *this; + this->m_ptr = ptr.m_ptr; + return *this; + } + + nullptr_initializer& operator=(nullptr_initializer&& ptr) noexcept { + if (&ptr == this) + return *this; + this->m_ptr = ptr.m_ptr; + return *this; + } + + inline T* operator->(){ + return m_ptr; + } + + ~nullptr_initializer() = default; + }; } #endif //BLT_TESTS_MEMORY_H