OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
Avi (use Gerrit)
2014/02/07 01:45:29
no (c)
ccameron
2014/02/07 06:57:49
Done.
| |
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_MAC_SCOPED_TYPEREF_H_ | |
6 #define BASE_MAC_SCOPED_TYPEREF_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/compiler_specific.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_policy.h" | |
12 | |
13 namespace base { | |
14 | |
15 // ScopedTypeRef<> is a generalized form of ScopedCFTypeRef<>, for use with | |
Avi (use Gerrit)
2014/02/07 01:45:29
OTOH, you could look at ScopedCFTypeRef as being a
ccameron
2014/02/07 06:57:49
Good call. Change ScopedCFTypeRef to be a subclass
| |
16 // types that have have type-specific retain and release functions (e.g, | |
17 // CGLContextObj, which requires use of CGLReleaseContext instead of CFRelease). | |
18 // | |
19 // This type may be initialized in the same ways as ScopedCFTypeRef<>, with | |
20 // parameterized ownership. | |
21 // | |
22 // Additionally, for the many types that have pass-by-pointer constructor | |
23 // functions, the operator& is overloaded, to allow direct initialization | |
24 // and assumption of ownership of the object, e.g: | |
25 // | |
26 // base::ScopedTypeRef<CGLContextObj, CGLRetainContext, CGLReleaseContext> | |
27 // context; | |
28 // CGLCreateContext(pixel_format, share_group, &context); | |
29 | |
30 template<typename T, T Retain(T), void Release(T)> | |
31 class ScopedTypeRef { | |
32 public: | |
33 typedef T element_type; | |
34 | |
35 ScopedTypeRef() : object_(NULL) {} | |
36 | |
37 explicit ScopedTypeRef( | |
38 T object, | |
39 base::scoped_policy::OwnershipPolicy policy) | |
40 : object_(object) { | |
41 if (object_ && policy == base::scoped_policy::RETAIN) | |
42 Retain(object_); | |
43 } | |
44 | |
45 ScopedTypeRef(const ScopedTypeRef<T, Retain, Release>& that) | |
46 : object_(that.object_) { | |
47 if (object_) | |
48 Retain(object_); | |
49 } | |
50 | |
51 ~ScopedTypeRef() { | |
52 if (object_) | |
53 Release(object_); | |
54 } | |
55 | |
56 // This is to be used only to take ownership of objects that are created | |
57 // by pass-by-pointer constructor functions. To enforce this, require that | |
58 // the object be reset to NULL before this may be used. | |
59 T* operator&() WARN_UNUSED_RESULT { | |
60 DCHECK(!object_); | |
61 return &object_; | |
62 } | |
63 | |
64 ScopedTypeRef& operator=(const ScopedTypeRef<T, Retain, Release>& that) { | |
65 reset(that.get(), base::scoped_policy::RETAIN); | |
66 return *this; | |
67 } | |
68 | |
69 void reset(T object = NULL, | |
70 base::scoped_policy::OwnershipPolicy policy = | |
71 base::scoped_policy::ASSUME) { | |
72 if (object && policy == base::scoped_policy::RETAIN) | |
73 Retain(object); | |
74 if (object_) | |
75 Release(object_); | |
76 object_ = object; | |
77 } | |
78 | |
79 bool operator==(T that) const { | |
80 return object_ == that; | |
81 } | |
82 | |
83 bool operator!=(T that) const { | |
84 return object_ != that; | |
85 } | |
86 | |
87 operator T() const { | |
88 return object_; | |
89 } | |
90 | |
91 T get() const { | |
92 return object_; | |
93 } | |
94 | |
95 void swap(ScopedTypeRef& that) { | |
96 T temp = that.object_; | |
97 that.object_ = object_; | |
98 object_ = temp; | |
99 } | |
100 | |
101 // ScopedTypeRef<>::release() is like scoped_ptr<>::release. It is NOT | |
102 // a wrapper for Release(). To force a ScopedTypeRef<> object to call | |
103 // Release(), use ScopedTypeRef<>::reset(). | |
104 T release() WARN_UNUSED_RESULT { | |
105 T temp = object_; | |
106 object_ = NULL; | |
107 return temp; | |
108 } | |
109 | |
110 private: | |
111 T object_; | |
112 }; | |
113 | |
114 } // namespace base | |
115 | |
116 #endif // BASE_MAC_SCOPED_TYPEREF_H_ | |
OLD | NEW |