1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MAC_SCOPED_NSOBJECT_H_
6 #define BASE_MAC_SCOPED_NSOBJECT_H_
8 // Include NSObject.h directly because Foundation.h pulls in many dependencies.
9 // (Approx 100k lines of code versus 1.5k for NSObject.h). scoped_nsobject gets
10 // singled out because it is most typically included from other header files.
11 #import <Foundation/NSObject.h>
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
16 @
class NSAutoreleasePool
;
20 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership
21 // of an NSObject subclass object. Style deviations here are solely for
22 // compatibility with scoped_ptr<>'s interface, with which everyone is already
25 // scoped_nsobject<> takes ownership of an object (in the constructor or in
26 // reset()) by taking over the caller's existing ownership claim. The caller
27 // must own the object it gives to scoped_nsobject<>, and relinquishes an
28 // ownership claim to that object. scoped_nsobject<> does not call -retain,
29 // callers have to call this manually if appropriate.
31 // scoped_nsprotocol<> has the same behavior as scoped_nsobject, but can be used
34 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For
35 // NSAutoreleasePools use ScopedNSAutoreleasePool from
36 // scoped_nsautorelease_pool.h instead.
37 // We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile
38 // time with a template specialization (see below).
40 template<typename NST
>
41 class scoped_nsprotocol
{
43 explicit scoped_nsprotocol(NST object
= nil
) : object_(object
) {}
45 scoped_nsprotocol(const scoped_nsprotocol
<NST
>& that
)
46 : object_([that
.object_ retain
]) {
49 template <typename NSU
>
50 scoped_nsprotocol(const scoped_nsprotocol
<NSU
>& that
)
51 : object_([that
.get() retain
]) {
54 ~scoped_nsprotocol() {
58 scoped_nsprotocol
& operator=(const scoped_nsprotocol
<NST
>& that
) {
59 reset([that
.get() retain
]);
63 void reset(NST object
= nil
) {
64 // We intentionally do not check that object != object_ as the caller must
65 // either already have an ownership claim over whatever it passes to this
66 // method, or call it with the |RETAIN| policy which will have ensured that
67 // the object is retained once more when reaching this point.
72 bool operator==(NST that
) const { return object_
== that
; }
73 bool operator!=(NST that
) const { return object_
!= that
; }
75 operator NST() const {
83 void swap(scoped_nsprotocol
& that
) {
84 NST temp
= that
.object_
;
85 that
.object_
= object_
;
89 // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a
90 // wrapper for [object_ release]. To force a scoped_nsprotocol<> to call
91 // [object_ release], use scoped_nsprotocol<>::reset().
92 NST
release() WARN_UNUSED_RESULT
{
98 // Shift reference to the autorelease pool to be released later.
100 return [release() autorelease
];
109 void swap(scoped_nsprotocol
<C
>& p1
, scoped_nsprotocol
<C
>& p2
) {
114 bool operator==(C p1
, const scoped_nsprotocol
<C
>& p2
) {
115 return p1
== p2
.get();
119 bool operator!=(C p1
, const scoped_nsprotocol
<C
>& p2
) {
120 return p1
!= p2
.get();
123 template<typename NST
>
124 class scoped_nsobject
: public scoped_nsprotocol
<NST
*> {
126 explicit scoped_nsobject(NST
* object
= nil
)
127 : scoped_nsprotocol
<NST
*>(object
) {}
129 scoped_nsobject(const scoped_nsobject
<NST
>& that
)
130 : scoped_nsprotocol
<NST
*>(that
) {
133 template<typename NSU
>
134 scoped_nsobject(const scoped_nsobject
<NSU
>& that
)
135 : scoped_nsprotocol
<NST
*>(that
) {
138 scoped_nsobject
& operator=(const scoped_nsobject
<NST
>& that
) {
139 scoped_nsprotocol
<NST
*>::operator=(that
);
144 // Specialization to make scoped_nsobject<id> work.
146 class scoped_nsobject
<id
> : public scoped_nsprotocol
<id
> {
148 explicit scoped_nsobject(id object
= nil
) : scoped_nsprotocol
<id
>(object
) {}
150 scoped_nsobject(const scoped_nsobject
<id
>& that
)
151 : scoped_nsprotocol
<id
>(that
) {
154 template<typename NSU
>
155 scoped_nsobject(const scoped_nsobject
<NSU
>& that
)
156 : scoped_nsprotocol
<id
>(that
) {
159 scoped_nsobject
& operator=(const scoped_nsobject
<id
>& that
) {
160 scoped_nsprotocol
<id
>::operator=(that
);
165 // Do not use scoped_nsobject for NSAutoreleasePools, use
166 // ScopedNSAutoreleasePool instead. This is a compile time check. See details
169 class scoped_nsobject
<NSAutoreleasePool
> {
171 explicit scoped_nsobject(NSAutoreleasePool
* object
= nil
);
172 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject
);
177 #endif // BASE_MAC_SCOPED_NSOBJECT_H_