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 #import <Foundation/Foundation.h>
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
14 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership
15 // of an NSObject subclass object. Style deviations here are solely for
16 // compatibility with scoped_ptr<>'s interface, with which everyone is already
19 // scoped_nsobject<> takes ownership of an object (in the constructor or in
20 // reset()) by taking over the caller's existing ownership claim. The caller
21 // must own the object it gives to scoped_nsobject<>, and relinquishes an
22 // ownership claim to that object. scoped_nsobject<> does not call -retain,
23 // callers have to call this manually if appropriate.
25 // scoped_nsprotocol<> has the same behavior as scoped_nsobject, but can be used
28 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For
29 // NSAutoreleasePools use ScopedNSAutoreleasePool from
30 // scoped_nsautorelease_pool.h instead.
31 // We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile
32 // time with a template specialization (see below).
34 template<typename NST
>
35 class scoped_nsprotocol
{
37 explicit scoped_nsprotocol(NST object
= nil
) : object_(object
) {}
39 scoped_nsprotocol(const scoped_nsprotocol
<NST
>& that
)
40 : object_([that
.object_ retain
]) {
43 template <typename NSU
>
44 scoped_nsprotocol(const scoped_nsprotocol
<NSU
>& that
)
45 : object_([that
.get() retain
]) {
48 ~scoped_nsprotocol() {
52 scoped_nsprotocol
& operator=(const scoped_nsprotocol
<NST
>& that
) {
53 reset([that
.get() retain
]);
57 void reset(NST object
= nil
) {
58 // We intentionally do not check that object != object_ as the caller must
59 // either already have an ownership claim over whatever it passes to this
60 // method, or call it with the |RETAIN| policy which will have ensured that
61 // the object is retained once more when reaching this point.
66 bool operator==(NST that
) const { return object_
== that
; }
67 bool operator!=(NST that
) const { return object_
!= that
; }
69 operator NST() const {
77 void swap(scoped_nsprotocol
& that
) {
78 NST temp
= that
.object_
;
79 that
.object_
= object_
;
83 // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a
84 // wrapper for [object_ release]. To force a scoped_nsprotocol<> to call
85 // [object_ release], use scoped_nsprotocol<>::reset().
86 NST
release() WARN_UNUSED_RESULT
{
92 // Shift reference to the autorelease pool to be released later.
94 return [release() autorelease
];
103 void swap(scoped_nsprotocol
<C
>& p1
, scoped_nsprotocol
<C
>& p2
) {
108 bool operator==(C p1
, const scoped_nsprotocol
<C
>& p2
) {
109 return p1
== p2
.get();
113 bool operator!=(C p1
, const scoped_nsprotocol
<C
>& p2
) {
114 return p1
!= p2
.get();
117 template<typename NST
>
118 class scoped_nsobject
: public scoped_nsprotocol
<NST
*> {
120 explicit scoped_nsobject(NST
* object
= nil
)
121 : scoped_nsprotocol
<NST
*>(object
) {}
123 scoped_nsobject(const scoped_nsobject
<NST
>& that
)
124 : scoped_nsprotocol
<NST
*>(that
) {
127 template<typename NSU
>
128 scoped_nsobject(const scoped_nsobject
<NSU
>& that
)
129 : scoped_nsprotocol
<NST
*>(that
) {
132 scoped_nsobject
& operator=(const scoped_nsobject
<NST
>& that
) {
133 scoped_nsprotocol
<NST
*>::operator=(that
);
138 // Specialization to make scoped_nsobject<id> work.
140 class scoped_nsobject
<id
> : public scoped_nsprotocol
<id
> {
142 explicit scoped_nsobject(id object
= nil
) : scoped_nsprotocol
<id
>(object
) {}
144 scoped_nsobject(const scoped_nsobject
<id
>& that
)
145 : scoped_nsprotocol
<id
>(that
) {
148 template<typename NSU
>
149 scoped_nsobject(const scoped_nsobject
<NSU
>& that
)
150 : scoped_nsprotocol
<id
>(that
) {
153 scoped_nsobject
& operator=(const scoped_nsobject
<id
>& that
) {
154 scoped_nsprotocol
<id
>::operator=(that
);
159 // Do not use scoped_nsobject for NSAutoreleasePools, use
160 // ScopedNSAutoreleasePool instead. This is a compile time check. See details
163 class scoped_nsobject
<NSAutoreleasePool
> {
165 explicit scoped_nsobject(NSAutoreleasePool
* object
= nil
);
166 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject
);
171 #endif // BASE_MAC_SCOPED_NSOBJECT_H_