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

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

Issue 1551943002: Rewrite most of the scopers in //base/mac to use ScopedTypeRef or ScopedGeneric. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix iOS 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_MAC_SCOPED_TYPEREF_H_ 5 #ifndef BASE_MAC_SCOPED_TYPEREF_H_
6 #define BASE_MAC_SCOPED_TYPEREF_H_ 6 #define BASE_MAC_SCOPED_TYPEREF_H_
7 7
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_policy.h" 10 #include "base/memory/scoped_policy.h"
11 11
12 namespace base { 12 namespace base {
13 13
14 // ScopedTypeRef<> is patterned after scoped_ptr<>, but maintains a ownership 14 // ScopedTypeRef<> is patterned after scoped_ptr<>, but maintains a ownership
15 // of a reference to any type that is maintained by Retain and Release methods. 15 // of a reference to any type that is maintained by Retain and Release methods.
16 // 16 //
17 // The Traits structure must provide the Retain and Release methods for type T. 17 // The Traits structure must provide the Retain and Release methods for type T.
18 // A default ScopedTypeRefTraits is used but not defined, and should be defined 18 // A default ScopedTypeRefTraits is used but not defined, and should be defined
19 // for each type to use this interface. For example, an appropriate definition 19 // for each type to use this interface. For example, an appropriate definition
20 // of ScopedTypeRefTraits for CGLContextObj would be: 20 // of ScopedTypeRefTraits for CGLContextObj would be:
21 // 21 //
22 // template<> 22 // template<>
23 // struct ScopedTypeRefTraits<CGLContextObj> { 23 // struct ScopedTypeRefTraits<CGLContextObj> {
24 // static CGLContextObj InvalidValue() { return nullptr; } 24 // static CGLContextObj InvalidValue() { return nullptr; }
25 // static void Retain(CGLContextObj object) { CGLContextRetain(object); } 25 // static CGLContextObj Retain(CGLContextObj object) {
Mark Mentovai 2016/01/04 16:09:23 I think it’s easier to have these return void and
Robert Sesek 2016/01/04 16:11:52 I had to make this change for ScopedBlock, since t
26 // CGLContextRetain(object);
27 // return object;
28 // }
26 // static void Release(CGLContextObj object) { CGLContextRelease(object); } 29 // static void Release(CGLContextObj object) { CGLContextRelease(object); }
27 // }; 30 // };
28 // 31 //
29 // For the many types that have pass-by-pointer create functions, the function 32 // For the many types that have pass-by-pointer create functions, the function
30 // InitializeInto() is provided to allow direct initialization and assumption 33 // InitializeInto() is provided to allow direct initialization and assumption
31 // of ownership of the object. For example, continuing to use the above 34 // of ownership of the object. For example, continuing to use the above
32 // CGLContextObj specialization: 35 // CGLContextObj specialization:
33 // 36 //
34 // base::ScopedTypeRef<CGLContextObj> context; 37 // base::ScopedTypeRef<CGLContextObj> context;
35 // CGLCreateContext(pixel_format, share_group, context.InitializeInto()); 38 // CGLCreateContext(pixel_format, share_group, context.InitializeInto());
(...skipping 12 matching lines...) Expand all
48 template<typename T, typename Traits = ScopedTypeRefTraits<T>> 51 template<typename T, typename Traits = ScopedTypeRefTraits<T>>
49 class ScopedTypeRef { 52 class ScopedTypeRef {
50 public: 53 public:
51 typedef T element_type; 54 typedef T element_type;
52 55
53 ScopedTypeRef( 56 ScopedTypeRef(
54 T object = Traits::InvalidValue(), 57 T object = Traits::InvalidValue(),
55 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) 58 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
56 : object_(object) { 59 : object_(object) {
57 if (object_ && policy == base::scoped_policy::RETAIN) 60 if (object_ && policy == base::scoped_policy::RETAIN)
58 Traits::Retain(object_); 61 object_ = Traits::Retain(object_);
59 } 62 }
60 63
61 ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) 64 ScopedTypeRef(const ScopedTypeRef<T, Traits>& that)
62 : object_(that.object_) { 65 : object_(that.object_) {
63 if (object_) 66 if (object_)
64 Traits::Retain(object_); 67 object_ = Traits::Retain(object_);
65 } 68 }
66 69
67 ~ScopedTypeRef() { 70 ~ScopedTypeRef() {
68 if (object_) 71 if (object_)
69 Traits::Release(object_); 72 Traits::Release(object_);
70 } 73 }
71 74
72 ScopedTypeRef& operator=(const ScopedTypeRef<T, Traits>& that) { 75 ScopedTypeRef& operator=(const ScopedTypeRef<T, Traits>& that) {
73 reset(that.get(), base::scoped_policy::RETAIN); 76 reset(that.get(), base::scoped_policy::RETAIN);
74 return *this; 77 return *this;
75 } 78 }
76 79
77 // This is to be used only to take ownership of objects that are created 80 // This is to be used only to take ownership of objects that are created
78 // by pass-by-pointer create functions. To enforce this, require that the 81 // by pass-by-pointer create functions. To enforce this, require that the
79 // object be reset to NULL before this may be used. 82 // object be reset to NULL before this may be used.
80 T* InitializeInto() WARN_UNUSED_RESULT { 83 T* InitializeInto() WARN_UNUSED_RESULT {
81 DCHECK(!object_); 84 DCHECK(!object_);
82 return &object_; 85 return &object_;
83 } 86 }
84 87
85 void reset(T object = Traits::InvalidValue(), 88 void reset(T object = Traits::InvalidValue(),
86 base::scoped_policy::OwnershipPolicy policy = 89 base::scoped_policy::OwnershipPolicy policy =
87 base::scoped_policy::ASSUME) { 90 base::scoped_policy::ASSUME) {
88 if (object && policy == base::scoped_policy::RETAIN) 91 if (object && policy == base::scoped_policy::RETAIN)
89 Traits::Retain(object); 92 object = Traits::Retain(object);
90 if (object_) 93 if (object_)
91 Traits::Release(object_); 94 Traits::Release(object_);
92 object_ = object; 95 object_ = object;
93 } 96 }
94 97
95 bool operator==(T that) const { 98 bool operator==(T that) const {
96 return object_ == that; 99 return object_ == that;
97 } 100 }
98 101
99 bool operator!=(T that) const { 102 bool operator!=(T that) const {
(...skipping 23 matching lines...) Expand all
123 return temp; 126 return temp;
124 } 127 }
125 128
126 private: 129 private:
127 T object_; 130 T object_;
128 }; 131 };
129 132
130 } // namespace base 133 } // namespace base
131 134
132 #endif // BASE_MAC_SCOPED_TYPEREF_H_ 135 #endif // BASE_MAC_SCOPED_TYPEREF_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698