From 9ae0bd3829a34d4239521d9c7838089395c2336c Mon Sep 17 00:00:00 2001 From: Francisco Jerez Date: Tue, 18 Feb 2014 13:16:19 +0100 Subject: clover: Some improvements for the intrusive pointer class. Define some additional convenience operators, clean up the implementation slightly, and rename it to 'intrusive_ptr' for reasons that will be obvious in the next commit. Tested-by: Tom Stellard --- src/gallium/state_trackers/clover/util/pointer.hpp | 53 ++++++++++++++-------- 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'src/gallium/state_trackers/clover/util') diff --git a/src/gallium/state_trackers/clover/util/pointer.hpp b/src/gallium/state_trackers/clover/util/pointer.hpp index f0c5b1618a5..b7a633b523c 100644 --- a/src/gallium/state_trackers/clover/util/pointer.hpp +++ b/src/gallium/state_trackers/clover/util/pointer.hpp @@ -57,35 +57,43 @@ namespace clover { /// clover::ref_counter interface. /// template - class ref_ptr { + class intrusive_ptr { public: - ref_ptr(T *q = NULL) : p(NULL) { - reset(q); + intrusive_ptr(T *q = NULL) : p(q) { + if (p) + p->retain(); } - ref_ptr(const ref_ptr &ref) : p(NULL) { - reset(ref.p); + intrusive_ptr(const intrusive_ptr &ptr) : + intrusive_ptr(ptr.p) { } - ~ref_ptr() { - reset(NULL); + intrusive_ptr(intrusive_ptr &&ptr) : + p(ptr.p) { + ptr.p = NULL; } - void - reset(T *q = NULL) { - if (q) - q->retain(); + ~intrusive_ptr() { if (p && p->release()) delete p; - p = q; } - ref_ptr & - operator=(const ref_ptr &ref) { - reset(ref.p); + intrusive_ptr & + operator=(intrusive_ptr ptr) { + std::swap(ptr.p, p); return *this; } + bool + operator==(const intrusive_ptr &ref) const { + return p == ref.p; + } + + bool + operator!=(const intrusive_ptr &ref) const { + return p != ref.p; + } + T & operator*() const { return *p; @@ -96,22 +104,31 @@ namespace clover { return p; } + T * + operator()() const { + return p; + } + explicit operator bool() const { return p; } + explicit operator T *() const { + return p; + } + private: T *p; }; /// /// Transfer the caller's ownership of a reference-counted object - /// to a clover::ref_ptr smart pointer. + /// to a clover::intrusive_ptr smart pointer. /// template - inline ref_ptr + inline intrusive_ptr transfer(T *p) { - ref_ptr ref { p }; + intrusive_ptr ref { p }; p->release(); return ref; } -- cgit v1.2.3