OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MEMORY_SCOPED_NSOBJECT_H_ | 5 #ifndef BASE_MEMORY_SCOPED_NSOBJECT_H_ |
6 #define BASE_MEMORY_SCOPED_NSOBJECT_H_ | 6 #define BASE_MEMORY_SCOPED_NSOBJECT_H_ |
7 | 7 |
8 #import <Foundation/Foundation.h> | 8 // TODO(thakis): Update all clients to include base/mac/scoped_nsobject.h and |
9 #include "base/basictypes.h" | 9 // get rid of this forwarding header. |
10 #include "base/compiler_specific.h" | 10 #include "base/mac/scoped_nsobject.h" |
11 | 11 |
12 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership | 12 using base::scoped_nsobject; |
13 // of an NSObject subclass object. Style deviations here are solely for | 13 using base::scoped_nsprotocol; |
14 // compatibility with scoped_ptr<>'s interface, with which everyone is already | |
15 // familiar. | |
16 // | |
17 // scoped_nsobject<> takes ownership of an object (in the constructor or in | |
18 // reset()) by taking over the caller's existing ownership claim. The caller | |
19 // must own the object it gives to scoped_nsobject<>, and relinquishes an | |
20 // ownership claim to that object. scoped_nsobject<> does not call -retain, | |
21 // callers have to call this manually if appropriate. | |
22 // | |
23 // scoped_nsprotocol<> has the same behavior as scoped_nsobject, but can be used | |
24 // with protocols. | |
25 // | |
26 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For | |
27 // NSAutoreleasePools use ScopedNSAutoreleasePool from | |
28 // scoped_nsautorelease_pool.h instead. | |
29 // We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile | |
30 // time with a template specialization (see below). | |
31 | 14 |
32 template<typename NST> | |
33 class scoped_nsprotocol { | |
34 public: | |
35 explicit scoped_nsprotocol(NST object = nil) : object_(object) {} | |
36 | |
37 scoped_nsprotocol(const scoped_nsprotocol<NST>& that) | |
38 : object_([that.object_ retain]) { | |
39 } | |
40 | |
41 ~scoped_nsprotocol() { | |
42 [object_ release]; | |
43 } | |
44 | |
45 scoped_nsprotocol& operator=(const scoped_nsprotocol<NST>& that) { | |
46 reset([that.get() retain]); | |
47 return *this; | |
48 } | |
49 | |
50 void reset(NST object = nil) { | |
51 // We intentionally do not check that object != object_ as the caller must | |
52 // either already have an ownership claim over whatever it passes to this | |
53 // method, or call it with the |RETAIN| policy which will have ensured that | |
54 // the object is retained once more when reaching this point. | |
55 [object_ release]; | |
56 object_ = object; | |
57 } | |
58 | |
59 bool operator==(NST that) const { return object_ == that; } | |
60 bool operator!=(NST that) const { return object_ != that; } | |
61 | |
62 operator NST() const { | |
63 return object_; | |
64 } | |
65 | |
66 NST get() const { | |
67 return object_; | |
68 } | |
69 | |
70 void swap(scoped_nsprotocol& that) { | |
71 NST temp = that.object_; | |
72 that.object_ = object_; | |
73 object_ = temp; | |
74 } | |
75 | |
76 // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a | |
77 // wrapper for [object_ release]. To force a scoped_nsprotocol<> to call | |
78 // [object_ release], use scoped_nsprotocol<>::reset(). | |
79 NST release() WARN_UNUSED_RESULT { | |
80 NST temp = object_; | |
81 object_ = nil; | |
82 return temp; | |
83 } | |
84 | |
85 // Shift reference to the autorelease pool to be released later. | |
86 NST autorelease() { | |
87 return [release() autorelease]; | |
88 } | |
89 | |
90 private: | |
91 NST object_; | |
92 }; | |
93 | |
94 // Free functions | |
95 template <class C> | |
96 void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) { | |
97 p1.swap(p2); | |
98 } | |
99 | |
100 template <class C> | |
101 bool operator==(C p1, const scoped_nsprotocol<C>& p2) { | |
102 return p1 == p2.get(); | |
103 } | |
104 | |
105 template <class C> | |
106 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) { | |
107 return p1 != p2.get(); | |
108 } | |
109 | |
110 template<typename NST> | |
111 class scoped_nsobject : public scoped_nsprotocol<NST*> { | |
112 public: | |
113 explicit scoped_nsobject(NST* object = nil) | |
114 : scoped_nsprotocol<NST*>(object) {} | |
115 | |
116 scoped_nsobject(const scoped_nsobject<NST>& that) | |
117 : scoped_nsprotocol<NST*>(that) { | |
118 } | |
119 | |
120 scoped_nsobject& operator=(const scoped_nsobject<NST>& that) { | |
121 scoped_nsprotocol<NST*>::operator=(that); | |
122 return *this; | |
123 } | |
124 }; | |
125 | |
126 // Specialization to make scoped_nsobject<id> work. | |
127 template<> | |
128 class scoped_nsobject<id> : public scoped_nsprotocol<id> { | |
129 public: | |
130 explicit scoped_nsobject(id object = nil) : scoped_nsprotocol<id>(object) {} | |
131 | |
132 scoped_nsobject(const scoped_nsobject<id>& that) | |
133 : scoped_nsprotocol<id>(that) { | |
134 } | |
135 | |
136 scoped_nsobject& operator=(const scoped_nsobject<id>& that) { | |
137 scoped_nsprotocol<id>::operator=(that); | |
138 return *this; | |
139 } | |
140 }; | |
141 | |
142 // Do not use scoped_nsobject for NSAutoreleasePools, use | |
143 // ScopedNSAutoreleasePool instead. This is a compile time check. See details | |
144 // at top of header. | |
145 template<> | |
146 class scoped_nsobject<NSAutoreleasePool> { | |
147 private: | |
148 explicit scoped_nsobject(NSAutoreleasePool* object = nil); | |
149 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | |
150 }; | |
151 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ | 15 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ |
OLD | NEW |