1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_APPLE_SCOPED_NSOBJECT_H_
6 #define BASE_APPLE_SCOPED_NSOBJECT_H_
7
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>
12
13 #include <type_traits>
14
15 #include "base/apple/scoped_typeref.h"
16 #include "base/base_export.h"
17 #include "base/compiler_specific.h"
18
19 #if HAS_FEATURE(objc_arc)
20 #error "Do not use scoped_nsobject in ARC code; use __strong instead."
21 #endif
22
23 @class NSAutoreleasePool;
24
25 namespace base::apple {
26
27 // scoped_nsobject<> is patterned after std::unique_ptr<>, but maintains
28 // ownership of an NSObject subclass object. Style deviations here are solely
29 // for compatibility with std::unique_ptr<>'s interface, with which everyone is
30 // already familiar.
31 //
32 // scoped_nsobject<> takes ownership of an object (in the constructor or in
33 // reset()) by taking over the caller's existing ownership claim. The caller
34 // must own the object it gives to scoped_nsobject<>, and relinquishes an
35 // ownership claim to that object. scoped_nsobject<> does not call -retain,
36 // callers have to call this manually if appropriate.
37 //
38 // scoped_nsprotocol<> has the same behavior as scoped_nsobject, but can be used
39 // with protocols.
40 //
41 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For C++ code use
42 // NSAutoreleasePool; for Objective-C(++) code use @autoreleasepool instead. We
43 // check for bad uses of scoped_nsobject and NSAutoreleasePool at compile time
44 // with a template specialization (see below).
45
46 namespace internal {
47
48 template <typename NST>
49 struct ScopedNSProtocolTraits {
InvalidValueScopedNSProtocolTraits50 static NST InvalidValue() { return nil; }
RetainScopedNSProtocolTraits51 static NST Retain(NST nst) { return [nst retain]; }
ReleaseScopedNSProtocolTraits52 static void Release(NST nst) { [nst release]; }
53 };
54
55 } // namespace internal
56
57 template <typename NST>
58 class scoped_nsprotocol
59 : public ScopedTypeRef<NST, internal::ScopedNSProtocolTraits<NST>> {
60 public:
61 using ScopedTypeRef<NST,
62 internal::ScopedNSProtocolTraits<NST>>::ScopedTypeRef;
63
64 // Shift reference to the autorelease pool to be released later.
autorelease()65 NST autorelease() { return [this->release() autorelease]; }
66 };
67
68 // Free functions
69 template <class C>
swap(scoped_nsprotocol<C> & p1,scoped_nsprotocol<C> & p2)70 void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) {
71 p1.swap(p2);
72 }
73
74 template <class C>
75 bool operator==(C p1, const scoped_nsprotocol<C>& p2) {
76 return p1 == p2.get();
77 }
78
79 template <class C>
80 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) {
81 return p1 != p2.get();
82 }
83
84 template <typename NST>
85 class scoped_nsobject : public scoped_nsprotocol<NST*> {
86 public:
87 using scoped_nsprotocol<NST*>::scoped_nsprotocol;
88
89 static_assert(std::is_same_v<NST, NSAutoreleasePool> == false,
90 "Use @autoreleasepool instead");
91 };
92
93 // Specialization to make scoped_nsobject<id> work.
94 template <>
95 class scoped_nsobject<id> : public scoped_nsprotocol<id> {
96 public:
97 using scoped_nsprotocol<id>::scoped_nsprotocol;
98 };
99
100 } // namespace base::apple
101
102 #endif // BASE_APPLE_SCOPED_NSOBJECT_H_
103