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

Unified Diff: base/memory/scoped_nsobject.h

Issue 9349016: Update scoped_nsobject to be copyable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Follow review, Created 8 years, 10 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 | « base/base.gyp ('k') | base/memory/scoped_nsobject_unittest.mm » ('j') | 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 c438ee720f605f06cce98a2a880a92de82d52110..54435a1828e33c6afa704b421e9eb426697e3513 100644
--- a/base/memory/scoped_nsobject.h
+++ b/base/memory/scoped_nsobject.h
@@ -15,139 +15,150 @@
// 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()) by taking 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. 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
+// ownsership is not changed.
Avi (use Gerrit) 2012/02/08 15:56:16 spelling: ownership
qsr 2012/02/08 16:37:26 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).
+
+namespace scoped_policy {
+enum OwnershipPolicy {
+ ACQUIRE,
+ RETAIN
+};
+} // namespace scoped_policy
+
template<typename NST>
-class scoped_nsobject {
+class scoped_nsprotocol {
public:
- explicit scoped_nsobject(NST* object = nil)
+ explicit scoped_nsprotocol(
+ NST object = nil,
+ scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE)
: object_(object) {
+ if (policy == scoped_policy::RETAIN)
+ [object retain];
+ }
+
+ scoped_nsprotocol(const scoped_nsprotocol<NST>& that)
+ : object_([that.object_ retain]) {
}
- ~scoped_nsobject() {
+ virtual ~scoped_nsprotocol() {
[object_ release];
}
- void reset(NST* object = nil) {
+ scoped_nsprotocol& operator=(const scoped_nsprotocol<NST>& that) {
+ reset(that.get(), scoped_policy::RETAIN);
+ return *this;
+ }
+
+ void reset(NST object = nil,
+ scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE) {
+ if (policy == scoped_policy::RETAIN)
+ [object retain];
// 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.
+ // either already have an ownership claim over whatever it passes to this
+ // method, or call it with the |RETAIN| policy which will have ensured that
+ // the object is retained once more when reaching this point.
[object_ release];
object_ = object;
}
- 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) {
+ explicit scoped_nsobject(
+ NST* object = nil,
+ scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE)
+ : scoped_nsprotocol<NST*>(object, policy) {
}
- ~scoped_nsobject() {
- [object_ release];
+ scoped_nsobject(const scoped_nsobject<NST>& that)
+ : scoped_nsprotocol<NST*>(that) {
}
- 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>& that) {
+ scoped_nsprotocol<NST*>::operator=(that);
+ return *this;
}
+};
- id get() const {
- return object_;
+// Specialization to make scoped_nsobject<id> work.
+template<>
+class scoped_nsobject<id> : public scoped_nsprotocol<id> {
+ public:
+ explicit scoped_nsobject(
+ id object = nil,
+ scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE)
+ : scoped_nsprotocol<id>(object, policy) {
}
- void swap(scoped_nsobject& that) {
- id temp = that.object_;
- that.object_ = object_;
- object_ = temp;
+ scoped_nsobject(const scoped_nsobject<id>& that)
+ : scoped_nsprotocol<id>(that) {
}
- // 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>& that) {
+ scoped_nsprotocol<id>::operator=(that);
+ return *this;
}
-
- private:
- id object_;
-
- DISALLOW_COPY_AND_ASSIGN(scoped_nsobject);
};
// Do not use scoped_nsobject for NSAutoreleasePools, use
@@ -156,71 +167,9 @@ class scoped_nsobject<id> {
template<>
class scoped_nsobject<NSAutoreleasePool> {
private:
- explicit scoped_nsobject(NSAutoreleasePool* object = nil);
+ explicit scoped_nsobject(
+ NSAutoreleasePool* object = nil,
+ scoped_policy::OwnershipPolicy policy = scoped_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_
« no previous file with comments | « base/base.gyp ('k') | base/memory/scoped_nsobject_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698