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

Side by Side Diff: base/mac/scoped_nsobject.h

Issue 1565803002: Sync Mac scopers with upstream Chromium (Closed) Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master
Patch Set: Created 4 years, 11 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 unified diff | Download patch
« no previous file with comments | « base/mac/scoped_launch_data.h ('k') | base/mac/scoped_typeref.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright 2006-2008 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 MINI_CHROMIUM_BASE_MAC_SCOPED_NSOBJECT_H_ 5 #ifndef MINI_CHROMIUM_BASE_MAC_SCOPED_NSOBJECT_H_
6 #define MINI_CHROMIUM_BASE_MAC_SCOPED_NSOBJECT_H_ 6 #define MINI_CHROMIUM_BASE_MAC_SCOPED_NSOBJECT_H_
7 7
8 #import <Foundation/Foundation.h> 8 #import <Foundation/Foundation.h>
9 9
10 #include <type_traits>
11
10 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
11 #include "base/macros.h" 13 #include "base/mac/scoped_typeref.h"
12 #include "base/memory/scoped_policy.h"
13 14
14 namespace base { 15 namespace base {
15 16
16 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership 17 namespace internal {
17 // of an NSObject subclass object. Style deviations here are solely for
18 // compatibility with scoped_ptr<>'s interface, with which everyone is already
19 // familiar.
20 //
21 // By default, scoped_nsobject<> takes ownership of an object (in the
22 // constructor or in reset()) by taking over the caller's existing ownership
23 // claim. The caller must own the object it gives to scoped_nsobject<>, and
24 // relinquishes an ownership claim to that object. scoped_nsobject<> does not
25 // call -retain. This behavior is parametrized by the |OwnershipPolicy| enum.
26 // If the value |RETAIN| is passed (in the constructor or in reset()), then
27 // scoped_nsobject<> will call -retain on the object, and the initial
28 // ownership is not changed.
29 //
30 // scoped_nsprotocol<> has the same behavior as scoped_nsobject, but can be used
31 // with protocols.
32 //
33 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For
34 // NSAutoreleasePools use ScopedNSAutoreleasePool from
35 // scoped_nsautorelease_pool.h instead.
36 // We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile
37 // time with a template specialization (see below).
38 18
39 template<typename NST> 19 template <typename NST>
40 class scoped_nsprotocol { 20 struct ScopedNSProtocolTraits {
41 public: 21 static NST InvalidValue() { return nil; }
42 explicit scoped_nsprotocol( 22 static NST Retain(NST nst) { return [nst retain]; }
43 NST object = nil, 23 static void Release(NST nst) { [nst release]; }
44 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
45 : object_(object) {
46 if (policy == base::scoped_policy::RETAIN)
47 [object retain];
48 }
49
50 scoped_nsprotocol(const scoped_nsprotocol<NST>& that)
51 : object_([that.object_ retain]) {
52 }
53
54 ~scoped_nsprotocol() {
55 [object_ release];
56 }
57
58 scoped_nsprotocol& operator=(const scoped_nsprotocol<NST>& that) {
59 reset(that.get(), base::scoped_policy::RETAIN);
60 return *this;
61 }
62
63 void reset(NST object = nil,
64 base::scoped_policy::OwnershipPolicy policy =
65 base::scoped_policy::ASSUME) {
66 if (policy == base::scoped_policy::RETAIN)
67 [object retain];
68 // We intentionally do not check that object != object_ as the caller must
69 // either already have an ownership claim over whatever it passes to this
70 // method, or call it with the |RETAIN| policy which will have ensured that
71 // the object is retained once more when reaching this point.
72 [object_ release];
73 object_ = object;
74 }
75
76 bool operator==(NST that) const { return object_ == that; }
77 bool operator!=(NST that) const { return object_ != that; }
78
79 operator NST() const {
80 return object_;
81 }
82
83 NST get() const {
84 return object_;
85 }
86
87 void swap(scoped_nsprotocol& that) {
88 NST temp = that.object_;
89 that.object_ = object_;
90 object_ = temp;
91 }
92
93 // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a
94 // wrapper for [object_ release]. To force a scoped_nsprotocol<> to call
95 // [object_ release], use scoped_nsprotocol<>::reset().
96 NST release() WARN_UNUSED_RESULT {
97 NST temp = object_;
98 object_ = nil;
99 return temp;
100 }
101
102 // Shift reference to the autorelease pool to be released later.
103 NST autorelease() {
104 return [release() autorelease];
105 }
106
107 private:
108 NST object_;
109 }; 24 };
110 25
111 // Free functions 26 } // namespace internal
27
28 template <typename NST>
29 class scoped_nsprotocol
30 : public ScopedTypeRef<NST, internal::ScopedNSProtocolTraits<NST>> {
31 public:
32 using ScopedTypeRef<NST,
33 internal::ScopedNSProtocolTraits<NST>>::ScopedTypeRef;
34
35 NST autorelease() { return [this->release() autorelease]; }
36 };
37
112 template <class C> 38 template <class C>
113 void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) { 39 void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) {
114 p1.swap(p2); 40 p1.swap(p2);
115 } 41 }
116 42
117 template <class C> 43 template <class C>
118 bool operator==(C p1, const scoped_nsprotocol<C>& p2) { 44 bool operator==(C p1, const scoped_nsprotocol<C>& p2) {
119 return p1 == p2.get(); 45 return p1 == p2.get();
120 } 46 }
121 47
122 template <class C> 48 template <class C>
123 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) { 49 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) {
124 return p1 != p2.get(); 50 return p1 != p2.get();
125 } 51 }
126 52
127 template<typename NST> 53 template <typename NST>
128 class scoped_nsobject : public scoped_nsprotocol<NST*> { 54 class scoped_nsobject : public scoped_nsprotocol<NST*> {
129 public: 55 public:
130 explicit scoped_nsobject( 56 using scoped_nsprotocol<NST*>::scoped_nsprotocol;
131 NST* object = nil,
132 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
133 : scoped_nsprotocol<NST*>(object, policy) {
134 }
135 57
136 scoped_nsobject(const scoped_nsobject<NST>& that) 58 static_assert(std::is_same<NST, NSAutoreleasePool>::value == false,
137 : scoped_nsprotocol<NST*>(that) { 59 "Use ScopedNSAutoreleasePool instead");
138 }
139
140 scoped_nsobject& operator=(const scoped_nsobject<NST>& that) {
141 scoped_nsprotocol<NST*>::operator=(that);
142 return *this;
143 }
144 }; 60 };
145 61
146 // Specialization to make scoped_nsobject<id> work.
147 template<> 62 template<>
148 class scoped_nsobject<id> : public scoped_nsprotocol<id> { 63 class scoped_nsobject<id> : public scoped_nsprotocol<id> {
149 public: 64 public:
150 explicit scoped_nsobject( 65 using scoped_nsprotocol<id>::scoped_nsprotocol;
151 id object = nil,
152 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
153 : scoped_nsprotocol<id>(object, policy) {
154 }
155
156 scoped_nsobject(const scoped_nsobject<id>& that)
157 : scoped_nsprotocol<id>(that) {
158 }
159
160 scoped_nsobject& operator=(const scoped_nsobject<id>& that) {
161 scoped_nsprotocol<id>::operator=(that);
162 return *this;
163 }
164 };
165
166 // Do not use scoped_nsobject for NSAutoreleasePools, use
167 // ScopedNSAutoreleasePool instead. This is a compile time check. See details
168 // at top of header.
169 template<>
170 class scoped_nsobject<NSAutoreleasePool> {
171 private:
172 explicit scoped_nsobject(NSAutoreleasePool* object = nil,
173 base::scoped_policy::OwnershipPolicy policy =
174 base::scoped_policy::ASSUME);
175 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject);
176 }; 66 };
177 67
178 } // namespace base 68 } // namespace base
179 69
180 #endif // MINI_CHROMIUM_BASE_MAC_SCOPED_NSOBJECT_H_ 70 #endif // MINI_CHROMIUM_BASE_MAC_SCOPED_NSOBJECT_H_
OLDNEW
« no previous file with comments | « base/mac/scoped_launch_data.h ('k') | base/mac/scoped_typeref.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698