diff options
author | Mark D. Roth <roth@google.com> | 2018-07-27 08:04:24 -0700 |
---|---|---|
committer | Mark D. Roth <roth@google.com> | 2018-07-27 08:04:24 -0700 |
commit | 326f82e5e25a8144cc0566cf2cf61bcdc0cd25f4 (patch) | |
tree | 8c8ddc5a2b793856cc03d1b3f112fe799b9df527 /src | |
parent | 78aca7bf85fee5eb9c47b430ce20452cec1faf2d (diff) |
A bit of cleanup and add more tests.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/lib/gprpp/ref_counted_ptr.h | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/core/lib/gprpp/ref_counted_ptr.h b/src/core/lib/gprpp/ref_counted_ptr.h index 91ca8eae63..c2dfbdd90f 100644 --- a/src/core/lib/gprpp/ref_counted_ptr.h +++ b/src/core/lib/gprpp/ref_counted_ptr.h @@ -41,21 +41,23 @@ class RefCountedPtr { value_ = value; } - // Move support. + // Move ctors. RefCountedPtr(RefCountedPtr&& other) { value_ = other.value_; other.value_ = nullptr; } - RefCountedPtr& operator=(RefCountedPtr&& other) { - if (value_ != nullptr) value_->Unref(); + template <typename Y> + RefCountedPtr(RefCountedPtr<Y>&& other) { value_ = other.value_; other.value_ = nullptr; - return *this; } - template <typename Y> - RefCountedPtr(RefCountedPtr<Y>&& other) { + + // Move assignment. + RefCountedPtr& operator=(RefCountedPtr&& other) { + if (value_ != nullptr) value_->Unref(); value_ = other.value_; other.value_ = nullptr; + return *this; } template <typename Y> RefCountedPtr& operator=(RefCountedPtr<Y>&& other) { @@ -65,11 +67,18 @@ class RefCountedPtr { return *this; } - // Copy support. + // Copy ctors. RefCountedPtr(const RefCountedPtr& other) { if (other.value_ != nullptr) other.value_->IncrementRefCount(); value_ = other.value_; } + template <typename Y> + RefCountedPtr(const RefCountedPtr<Y>& other) { + if (other.value_ != nullptr) other.value_->IncrementRefCount(); + value_ = other.value_; + } + + // Copy assignment. RefCountedPtr& operator=(const RefCountedPtr& other) { // Note: Order of reffing and unreffing is important here in case value_ // and other.value_ are the same object. @@ -79,11 +88,6 @@ class RefCountedPtr { return *this; } template <typename Y> - RefCountedPtr(const RefCountedPtr<Y>& other) { - if (other.value_ != nullptr) other.value_->IncrementRefCount(); - value_ = other.value_; - } - template <typename Y> RefCountedPtr& operator=(const RefCountedPtr<Y>& other) { // Note: Order of reffing and unreffing is important here in case value_ // and other.value_ are the same object. |