clover/util: Implement minimalist reference to clover::ref_counter object.
authorFrancisco Jerez <currojerez@riseup.net>
Sun, 17 Aug 2014 20:18:45 +0000 (23:18 +0300)
committerFrancisco Jerez <currojerez@riseup.net>
Mon, 18 Aug 2014 06:32:24 +0000 (09:32 +0300)
Tested-by: EdB <edb+mesa@sigluy.net>
src/gallium/state_trackers/clover/util/pointer.hpp

index 59c6e6e9f643d61ed112e74e54c51395a9753786..b790fd451c4968651b8311bb728e58f41361239a 100644 (file)
@@ -52,6 +52,51 @@ namespace clover {
       std::atomic<unsigned> _ref_count;
    };
 
+   ///
+   /// Simple reference to a clover::ref_counter object.  Unlike
+   /// clover::intrusive_ptr and clover::intrusive_ref, it does nothing
+   /// special when the reference count drops to zero.
+   ///
+   class ref_holder {
+   public:
+      ref_holder(ref_counter &o) : p(&o) {
+         p->retain();
+      }
+
+      ref_holder(const ref_holder &ref) :
+         ref_holder(*ref.p) {
+      }
+
+      ref_holder(ref_holder &&ref) :
+         p(ref.p) {
+         ref.p = NULL;
+      }
+
+      ~ref_holder() {
+         if (p)
+            p->release();
+      }
+
+      ref_holder &
+      operator=(ref_holder ref) {
+         std::swap(ref.p, p);
+         return *this;
+      }
+
+      bool
+      operator==(const ref_holder &ref) const {
+         return p == ref.p;
+      }
+
+      bool
+      operator!=(const ref_holder &ref) const {
+         return p != ref.p;
+      }
+
+   private:
+      ref_counter *p;
+   };
+
    ///
    /// Intrusive smart pointer for objects that implement the
    /// clover::ref_counter interface.