From 1b9fb2fd9129f834f041efe7f96e83998477c78b Mon Sep 17 00:00:00 2001 From: Francisco Jerez Date: Tue, 18 Feb 2014 13:20:07 +0100 Subject: clover: Define an intrusive smart reference class. Tested-by: Tom Stellard --- src/gallium/state_trackers/clover/util/pointer.hpp | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src') diff --git a/src/gallium/state_trackers/clover/util/pointer.hpp b/src/gallium/state_trackers/clover/util/pointer.hpp index b7a633b523c..98823aa7860 100644 --- a/src/gallium/state_trackers/clover/util/pointer.hpp +++ b/src/gallium/state_trackers/clover/util/pointer.hpp @@ -133,6 +133,72 @@ namespace clover { return ref; } + /// + /// Intrusive smart reference for objects that implement the + /// clover::ref_counter interface. + /// + template + class intrusive_ref { + public: + intrusive_ref(T &o) : p(&o) { + p->retain(); + } + + intrusive_ref(const intrusive_ref &ref) : + intrusive_ref(*ref.p) { + } + + intrusive_ref(intrusive_ref &&ref) : + p(ref.p) { + ref.p = NULL; + } + + ~intrusive_ref() { + if (p && p->release()) + delete p; + } + + intrusive_ref & + operator=(intrusive_ref ref) { + std::swap(ref.p, p); + return *this; + } + + bool + operator==(const intrusive_ref &ref) const { + return p == ref.p; + } + + bool + operator!=(const intrusive_ref &ref) const { + return p != ref.p; + } + + T & + operator()() const { + return *p; + } + + operator T &() const { + return *p; + } + + private: + T *p; + }; + + /// + /// Transfer the caller's ownership of a reference-counted object + /// to a clover::intrusive_ref smart reference. + /// + template + inline intrusive_ref + transfer(T &o) { + intrusive_ref ref { o }; + o.release(); + return ref; + } + /// /// Class that implements the usual pointer interface but in fact /// contains the object it seems to be pointing to. -- cgit v1.2.3