Flutter Engine
scoped_nsobject.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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.
4 
5 #ifndef FLUTTER_FML_PLATFORM_DARWIN_SCOPED_NSOBJECT_H_
6 #define FLUTTER_FML_PLATFORM_DARWIN_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 "flutter/fml/compiler_specific.h"
14 #include "flutter/fml/macros.h"
15 
16 @class NSAutoreleasePool;
17 
18 namespace fml {
19 
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
23 // familiar.
24 //
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.
30 //
31 // scoped_nsprotocol<> has the same behavior as scoped_nsobject, but can be used
32 // with protocols.
33 //
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).
39 
40 template <typename NST>
42  public:
43  explicit scoped_nsprotocol(NST object = nil) : object_(object) {}
44 
45  scoped_nsprotocol(const scoped_nsprotocol<NST>& that) : object_([that.object_ retain]) {}
46 
47  template <typename NSU>
48  // NOLINTNEXTLINE(google-explicit-constructor)
49  scoped_nsprotocol(const scoped_nsprotocol<NSU>& that) : object_([that.get() retain]) {}
50 
51  ~scoped_nsprotocol() { [object_ release]; }
52 
54  reset([that.get() retain]);
55  return *this;
56  }
57 
58  void reset(NST object = nil) {
59  // We intentionally do not check that object != object_ as the caller must
60  // either already have an ownership claim over whatever it passes to this
61  // method, or call it with the |RETAIN| policy which will have ensured that
62  // the object is retained once more when reaching this point.
63  [object_ release];
64  object_ = object;
65  }
66 
67  bool operator==(NST that) const { return object_ == that; }
68  bool operator!=(NST that) const { return object_ != that; }
69 
70  operator NST() const { return object_; } // NOLINT(google-explicit-constructor)
71 
72  NST get() const { return object_; }
73 
74  void swap(scoped_nsprotocol& that) {
75  NST temp = that.object_;
76  that.object_ = object_;
77  object_ = temp;
78  }
79 
80  // Shift reference to the autorelease pool to be released later.
81  NST autorelease() { return [release() autorelease]; }
82 
83  private:
84  NST object_;
85 
86  // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a
87  // wrapper for [object_ release]. To force a scoped_nsprotocol<> to call
88  // [object_ release], use scoped_nsprotocol<>::reset().
89  [[nodiscard]] NST release() {
90  NST temp = object_;
91  object_ = nil;
92  return temp;
93  }
94 };
95 
96 // Free functions
97 template <class C>
99  p1.swap(p2);
100 }
101 
102 template <class C>
103 bool operator==(C p1, const scoped_nsprotocol<C>& p2) {
104  return p1 == p2.get();
105 }
106 
107 template <class C>
108 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) {
109  return p1 != p2.get();
110 }
111 
112 template <typename NST>
113 class scoped_nsobject : public scoped_nsprotocol<NST*> {
114  public:
115  explicit scoped_nsobject(NST* object = nil) : scoped_nsprotocol<NST*>(object) {}
116 
118 
119  template <typename NSU>
120  // NOLINTNEXTLINE(google-explicit-constructor)
122 
125  return *this;
126  }
127 };
128 
129 // Specialization to make scoped_nsobject<id> work.
130 template <>
131 class scoped_nsobject<id> : public scoped_nsprotocol<id> {
132  public:
133  explicit scoped_nsobject(id object = nil) : scoped_nsprotocol<id>(object) {}
134 
136 
137  template <typename NSU>
138  // NOLINTNEXTLINE(google-explicit-constructor)
140 
143  return *this;
144  }
145 };
146 
147 // Do not use scoped_nsobject for NSAutoreleasePools, use
148 // ScopedNSAutoreleasePool instead. This is a compile time check. See details
149 // at top of header.
150 template <>
151 class scoped_nsobject<NSAutoreleasePool> {
152  private:
153  explicit scoped_nsobject(NSAutoreleasePool* object = nil);
155 };
156 
157 } // namespace fml
158 
159 #endif // FLUTTER_FML_PLATFORM_DARWIN_SCOPED_NSOBJECT_H_
scoped_nsobject(const scoped_nsobject< NSU > &that)
void reset(NST object=nil)
scoped_nsobject(NST *object=nil)
scoped_nsprotocol(const scoped_nsprotocol< NST > &that)
Definition: ascii_trie.cc:9
scoped_nsobject(const scoped_nsobject< NST > &that)
scoped_nsobject(const scoped_nsobject< id > &that)
void swap(scoped_nsprotocol &that)
scoped_nsprotocol(NST object=nil)
scoped_nsprotocol(const scoped_nsprotocol< NSU > &that)
scoped_nsobject & operator=(const scoped_nsobject< id > &that)
bool operator!=(NST that) const
int32_t id
scoped_nsprotocol & operator=(const scoped_nsprotocol< NST > &that)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
scoped_nsobject(const scoped_nsobject< NSU > &that)
scoped_nsobject & operator=(const scoped_nsobject< NST > &that)
bool operator==(NST that) const