Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1772)

Unified Diff: base/memory/scoped_nsobject.h

Issue 9290046: Adding a scoped_nsprotocol (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Follow review. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698