about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-08-30 20:35:08 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-08-30 20:35:08 -0400
commita9a5996310bb33207d3338f353aab97b9ed3a5e8 (patch)
tree653099c36612360a1415c462b7dddfa7ca968d8e
parentb430eb58654298d17492a36c7bcda9f803a327fe (diff)
downloadhkutil-a9a5996310bb33207d3338f353aab97b9ed3a5e8.tar.gz
hkutil-a9a5996310bb33207d3338f353aab97b9ed3a5e8.tar.bz2
hkutil-a9a5996310bb33207d3338f353aab97b9ed3a5e8.zip
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.
-rw-r--r--hkutil/recptr.h19
1 files 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 {
14 class recptr { 14 class recptr {
15 public: 15 public:
16 16
17 recptr() = default;
18
17 recptr(T* ptr) : ptr_(ptr) 19 recptr(T* ptr) : ptr_(ptr)
18 { 20 {
19 } 21 }
@@ -24,8 +26,11 @@ namespace hatkirby {
24 } 26 }
25 27
26 recptr(const recptr& other) 28 recptr(const recptr& other)
27 : ptr_(new T(*other.ptr_))
28 { 29 {
30 if (other.ptr_)
31 {
32 ptr_ = new T(*other.ptr_);
33 }
29 } 34 }
30 35
31 recptr(recptr&& other) 36 recptr(recptr&& other)
@@ -38,9 +43,14 @@ namespace hatkirby {
38 if (ptr_) 43 if (ptr_)
39 { 44 {
40 delete ptr_; 45 delete ptr_;
46
47 ptr_ = nullptr;
41 } 48 }
42 49
43 ptr_ = new T(*other.ptr_); 50 if (other.ptr_)
51 {
52 ptr_ = new T(*other.ptr_);
53 }
44 54
45 return *this; 55 return *this;
46 } 56 }
@@ -79,6 +89,11 @@ namespace hatkirby {
79 return ptr_; 89 return ptr_;
80 } 90 }
81 91
92 operator bool() const
93 {
94 return (ptr_ != nullptr);
95 }
96
82 private: 97 private:
83 98
84 T* ptr_ = nullptr; 99 T* ptr_ = nullptr;