nullptr_initalizer

v1
Brett 2023-03-05 12:57:57 -05:00
parent 48e126214b
commit 2b60b2621f
1 changed files with 31 additions and 0 deletions

View File

@ -46,6 +46,37 @@ namespace blt {
delete[] buffer;
}
};
template<typename T>
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<T>& ptr): m_ptr(ptr.m_ptr) {}
nullptr_initializer(nullptr_initializer<T>&& ptr) noexcept : m_ptr(ptr.m_ptr) {}
nullptr_initializer<T>& operator=(const nullptr_initializer<T>& ptr){
if (&ptr == this)
return *this;
this->m_ptr = ptr.m_ptr;
return *this;
}
nullptr_initializer<T>& operator=(nullptr_initializer<T>&& 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