From a9a5996310bb33207d3338f353aab97b9ed3a5e8 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 30 Aug 2018 20:35:08 -0400 Subject: Improvements to recptr recptr is now default constructible. You can evaluate it as a bool to determine if it contains a non-null pointer. Copying or copy assigning an empty recptr no longer crashes. --- hkutil/recptr.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/hkutil/recptr.h b/hkutil/recptr.h index c39e9d8..09c3113 100644 --- a/hkutil/recptr.h +++ b/hkutil/recptr.h @@ -14,6 +14,8 @@ namespace hatkirby { class recptr { public: + recptr() = default; + recptr(T* ptr) : ptr_(ptr) { } @@ -24,8 +26,11 @@ namespace hatkirby { } recptr(const recptr& other) - : ptr_(new T(*other.ptr_)) { + if (other.ptr_) + { + ptr_ = new T(*other.ptr_); + } } recptr(recptr&& other) @@ -38,9 +43,14 @@ namespace hatkirby { if (ptr_) { delete ptr_; + + ptr_ = nullptr; } - ptr_ = new T(*other.ptr_); + if (other.ptr_) + { + ptr_ = new T(*other.ptr_); + } return *this; } @@ -79,6 +89,11 @@ namespace hatkirby { return ptr_; } + operator bool() const + { + return (ptr_ != nullptr); + } + private: T* ptr_ = nullptr; -- cgit 1.4.1