Index: base/memory/scoped_nsobject.h |
diff --git a/base/memory/scoped_nsobject.h b/base/memory/scoped_nsobject.h |
index 65e35d5623280599c6498b1a89595e9143392ba8..8fdfbab39d2316a5665046ba0f9c7fdba6940a04 100644 |
--- a/base/memory/scoped_nsobject.h |
+++ b/base/memory/scoped_nsobject.h |
@@ -160,4 +160,67 @@ class scoped_nsobject<NSAutoreleasePool> { |
DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
}; |
+// Equivalent of scoped_nsobject for a id<protocol>. This is exactly the same |
+// class as scoped_nsobject, except that it doesn't handle any pointer |
+// transformation. |
+template<typename NST> |
+class scoped_nsprotocol { |
+ public: |
+ explicit scoped_nsprotocol(NST object = nil) : object_(object) { |
+ } |
+ |
+ ~scoped_nsprotocol() { |
+ [object_ release]; |
+ } |
+ |
+ void reset(NST object = nil) { |
+ [object_ release]; |
+ object_ = object; |
+ } |
+ |
+ bool operator==(NST that) const { return object_ == that; } |
+ bool operator!=(NST that) const { return object_ != that; } |
+ |
+ operator NST() const { |
+ return object_; |
+ } |
+ |
+ NST get() const { |
+ return object_; |
+ } |
+ |
+ void swap(scoped_nsprotocol& that) { |
+ NST temp = that.object_; |
+ that.object_ = object_; |
+ object_ = temp; |
+ } |
+ |
+ NST release() WARN_UNUSED_RESULT { |
+ NST temp = object_; |
+ object_ = nil; |
+ return temp; |
+ } |
+ |
+ private: |
+ NST object_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(scoped_nsprotocol); |
+}; |
+ |
+// Free functions |
+template <class C> |
+void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) { |
+ p1.swap(p2); |
+} |
+ |
+template <class C> |
+bool operator==(C p1, const scoped_nsprotocol<C>& p2) { |
+ return p1 == p2.get(); |
+} |
+ |
+template <class C> |
+bool operator!=(C p1, const scoped_nsprotocol<C>& p2) { |
+ return p1 != p2.get(); |
+} |
+ |
#endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ |