Index: base/memory/scoped_nsobject.h |
diff --git a/base/memory/scoped_nsobject.h b/base/memory/scoped_nsobject.h |
index c438ee720f605f06cce98a2a880a92de82d52110..906cbde9108773a7784878769b91f64771c07ff0 100644 |
--- a/base/memory/scoped_nsobject.h |
+++ b/base/memory/scoped_nsobject.h |
@@ -15,139 +15,134 @@ |
// compatibility with scoped_ptr<>'s interface, with which everyone is already |
// familiar. |
// |
-// When scoped_nsobject<> takes ownership of an object (in the constructor or |
-// in reset()), it takes over the caller's existing ownership claim. The |
-// caller must own the object it gives to scoped_nsobject<>, and relinquishes |
-// an ownership claim to that object. scoped_nsobject<> does not call |
-// -retain. |
+// By default, scoped_nsobject<> takes ownership of an object (in the |
+// constructor or in reset()), it takes over the caller's existing ownership |
Mark Mentovai
2012/02/07 17:13:17
Awkward phrasing. How ’bout
By default, scoped_ns
qsr
2012/02/08 14:59:41
Done.
|
+// claim. The caller must own the object it gives to scoped_nsobject<>, and |
+// relinquishes an ownership claim to that object. scoped_nsobject<> does not |
+// call -retain. This behavior is parametrized by the |OwnershipPolicy| enum. |
+// If the value |RETAIN| is passed (in the constructor or in reset()), then |
+// scoped_nsobject<> will call |retain| on the object, and the initial |
Mark Mentovai
2012/02/07 17:13:17
-retain instead of |retain|
qsr
2012/02/08 14:59:41
Done.
|
+// ownsershop is not changed. |
Mark Mentovai
2012/02/07 17:13:17
ownership, not ownsershop.
qsr
2012/02/08 14:59:41
Done.
|
+// |
+// scoped_nsprotocol<> has the same behavior than scoped_nsobject, but can be |
+// used with protocols. |
// |
// scoped_nsobject<> is not to be used for NSAutoreleasePools. For |
// NSAutoreleasePools use ScopedNSAutoreleasePool from |
// scoped_nsautorelease_pool.h instead. |
// We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile |
// time with a template specialization (see below). |
+ |
+enum OwnershipPolicy { |
+ ACQUIRE, |
Mark Mentovai
2012/02/07 17:13:17
Can we stuff this into a namespace? Having ACQUIRE
qsr
2012/02/08 14:59:41
Done.
|
+ RETAIN |
+}; |
+ |
template<typename NST> |
-class scoped_nsobject { |
+class scoped_nsprotocol { |
public: |
- explicit scoped_nsobject(NST* object = nil) |
- : object_(object) { |
+ scoped_nsprotocol(NST object = nil, OwnershipPolicy policy = ACQUIRE) |
+ : object_(policy == ACQUIRE ? object : [object retain]) { |
Mark Mentovai
2012/02/07 17:13:17
Here and on line 62:
I can appreciate the tersene
qsr
2012/02/08 14:59:41
Done.
|
+ } |
+ |
+ explicit scoped_nsprotocol(const scoped_nsprotocol<NST>& other) |
Mark Mentovai
2012/02/07 17:13:17
I don’t think this will work in STL containers wit
Mark Mentovai
2012/02/07 17:13:17
Here and on line 56:
The other methods in this cl
qsr
2012/02/08 14:59:41
Done.
qsr
2012/02/08 14:59:41
You are right. The funny thing is this compiles pe
|
+ : object_([other.object_ retain]) { |
} |
- ~scoped_nsobject() { |
+ ~scoped_nsprotocol() { |
[object_ release]; |
} |
- void reset(NST* object = nil) { |
- // We intentionally do not check that object != object_ as the caller must |
- // already have an ownership claim over whatever it gives to |
- // scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or |
- // in a call to reset(). In either case, it relinquishes that claim and |
- // the scoper assumes it. |
+ scoped_nsprotocol& operator= (const scoped_nsprotocol<NST>& other) { |
Mark Mentovai
2012/02/07 17:13:17
No space between operator= and the (.
qsr
2012/02/08 14:59:41
Done.
|
+ reset(other.get(), RETAIN); |
+ return *this; |
+ } |
+ |
+ void reset(NST object = nil, OwnershipPolicy policy = ACQUIRE) { |
+ NST temp = policy == ACQUIRE ? object : [object retain]; |
[object_ release]; |
- object_ = object; |
+ object_ = temp; |
} |
- bool operator==(NST* that) const { return object_ == that; } |
- bool operator!=(NST* that) const { return object_ != that; } |
+ bool operator==(NST that) const { return object_ == that; } |
+ bool operator!=(NST that) const { return object_ != that; } |
- operator NST*() const { |
+ operator NST() const { |
return object_; |
} |
- NST* get() const { |
+ NST get() const { |
return object_; |
} |
- void swap(scoped_nsobject& that) { |
- NST* temp = that.object_; |
+ void swap(scoped_nsprotocol& that) { |
+ NST temp = that.object_; |
that.object_ = object_; |
object_ = temp; |
} |
- // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT |
- // a wrapper for [object_ release]. To force a scoped_nsobject<> object to |
- // call [object_ release], use scoped_nsobject<>::reset(). |
- NST* release() WARN_UNUSED_RESULT { |
- NST* temp = object_; |
+ // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a |
+ // wrapper for [object_ release]. To force a scoped_nsprotocol<> call |
+ // [object_ release], use scoped_nsprotocol<>::reset(). |
+ NST release() WARN_UNUSED_RESULT { |
+ NST temp = object_; |
object_ = nil; |
return temp; |
} |
private: |
- NST* object_; |
- |
- DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
+ NST object_; |
}; |
// Free functions |
template <class C> |
-void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) { |
+void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) { |
p1.swap(p2); |
} |
template <class C> |
-bool operator==(C* p1, const scoped_nsobject<C>& p2) { |
+bool operator==(C p1, const scoped_nsprotocol<C>& p2) { |
return p1 == p2.get(); |
} |
template <class C> |
-bool operator!=(C* p1, const scoped_nsobject<C>& p2) { |
+bool operator!=(C p1, const scoped_nsprotocol<C>& p2) { |
return p1 != p2.get(); |
} |
- |
-// Specialization to make scoped_nsobject<id> work. |
-template<> |
-class scoped_nsobject<id> { |
+template<typename NST> |
+class scoped_nsobject : public scoped_nsprotocol<NST*> { |
public: |
- explicit scoped_nsobject(id object = nil) |
- : object_(object) { |
+ scoped_nsobject(NST* object = nil, OwnershipPolicy policy = ACQUIRE) |
+ : scoped_nsprotocol<NST*>(object, policy) { |
} |
- ~scoped_nsobject() { |
- [object_ release]; |
+ explicit scoped_nsobject(const scoped_nsobject<NST>& other) |
Mark Mentovai
2012/02/07 17:13:17
The same comments as on line 48 applies here and o
qsr
2012/02/08 14:59:41
Done.
|
+ : scoped_nsprotocol<NST*>(other) { |
} |
- void reset(id object = nil) { |
- // We intentionally do not check that object != object_ as the caller must |
- // already have an ownership claim over whatever it gives to |
- // scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or |
- // in a call to reset(). In either case, it relinquishes that claim and |
- // the scoper assumes it. |
- [object_ release]; |
- object_ = object; |
- } |
- |
- bool operator==(id that) const { return object_ == that; } |
- bool operator!=(id that) const { return object_ != that; } |
- |
- operator id() const { |
- return object_; |
+ scoped_nsobject& operator= (const scoped_nsobject<NST>& other) { |
Mark Mentovai
2012/02/07 17:13:17
The same comment as on line 56 applies here and on
qsr
2012/02/08 14:59:41
Done.
|
+ scoped_nsprotocol<NST*>::operator=(other); |
+ return *this; |
} |
+}; |
Mark Mentovai
2012/02/07 17:13:17
Do we need the “free functions” (lines 97-111) for
qsr
2012/02/08 14:59:41
I might be missing something, but why would we nee
|
- id get() const { |
- return object_; |
+// Specialization to make scoped_nsobject<id> work. |
+template<> |
+class scoped_nsobject<id> : public scoped_nsprotocol<id> { |
+ public: |
+ scoped_nsobject(id object = nil, OwnershipPolicy policy = ACQUIRE) |
+ : scoped_nsprotocol<id>(object, policy) { |
} |
- void swap(scoped_nsobject& that) { |
- id temp = that.object_; |
- that.object_ = object_; |
- object_ = temp; |
+ explicit scoped_nsobject(const scoped_nsobject<id>& other) |
+ : scoped_nsprotocol<id>(other) { |
} |
- // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT |
- // a wrapper for [object_ release]. To force a scoped_nsobject<> object to |
- // call [object_ release], use scoped_nsobject<>::reset(). |
- id release() WARN_UNUSED_RESULT { |
- id temp = object_; |
- object_ = nil; |
- return temp; |
+ scoped_nsobject& operator= (const scoped_nsobject<id>& other) { |
+ scoped_nsprotocol<id>::operator=(other); |
+ return *this; |
} |
- |
- private: |
- id object_; |
- |
- DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
}; |
// Do not use scoped_nsobject for NSAutoreleasePools, use |
@@ -156,71 +151,8 @@ class scoped_nsobject<id> { |
template<> |
class scoped_nsobject<NSAutoreleasePool> { |
private: |
- explicit scoped_nsobject(NSAutoreleasePool* object = nil); |
+ scoped_nsobject(NSAutoreleasePool* object = nil, |
+ OwnershipPolicy policy = ACQUIRE); |
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_ |