OLD | NEW |
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_SCOPED_GENERIC_H_ | 5 #ifndef BASE_SCOPED_GENERIC_H_ |
6 #define BASE_SCOPED_GENERIC_H_ | 6 #define BASE_SCOPED_GENERIC_H_ |
7 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/move.h" | |
15 | 14 |
16 namespace base { | 15 namespace base { |
17 | 16 |
18 // This class acts like ScopedPtr with a custom deleter (although is slightly | 17 // This class acts like ScopedPtr with a custom deleter (although is slightly |
19 // less fancy in some of the more escoteric respects) except that it keeps a | 18 // less fancy in some of the more escoteric respects) except that it keeps a |
20 // copy of the object rather than a pointer, and we require that the contained | 19 // copy of the object rather than a pointer, and we require that the contained |
21 // object has some kind of "invalid" value. | 20 // object has some kind of "invalid" value. |
22 // | 21 // |
23 // Defining a scoper based on this class allows you to get a scoper for | 22 // Defining a scoper based on this class allows you to get a scoper for |
24 // non-pointer types without having to write custom code for set, reset, and | 23 // non-pointer types without having to write custom code for set, reset, and |
(...skipping 22 matching lines...) Expand all Loading... |
47 // | 46 // |
48 // // This free function will not be called if f == InvalidValue()! | 47 // // This free function will not be called if f == InvalidValue()! |
49 // static void Free(int f) { | 48 // static void Free(int f) { |
50 // ::FreeFoo(f); | 49 // ::FreeFoo(f); |
51 // } | 50 // } |
52 // }; | 51 // }; |
53 // | 52 // |
54 // typedef ScopedGeneric<int, FooScopedTraits> ScopedFoo; | 53 // typedef ScopedGeneric<int, FooScopedTraits> ScopedFoo; |
55 template<typename T, typename Traits> | 54 template<typename T, typename Traits> |
56 class ScopedGeneric { | 55 class ScopedGeneric { |
57 DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(ScopedGeneric) | |
58 | |
59 private: | 56 private: |
60 // This must be first since it's used inline below. | 57 // This must be first since it's used inline below. |
61 // | 58 // |
62 // Use the empty base class optimization to allow us to have a D | 59 // Use the empty base class optimization to allow us to have a D |
63 // member, while avoiding any space overhead for it when D is an | 60 // member, while avoiding any space overhead for it when D is an |
64 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good | 61 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good |
65 // discussion of this technique. | 62 // discussion of this technique. |
66 struct Data : public Traits { | 63 struct Data : public Traits { |
67 explicit Data(const T& in) : generic(in) {} | 64 explicit Data(const T& in) : generic(in) {} |
68 Data(const T& in, const Traits& other) : Traits(other), generic(in) {} | 65 Data(const T& in, const Traits& other) : Traits(other), generic(in) {} |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 150 |
154 // Forbid comparison. If U != T, it totally doesn't make sense, and if U == | 151 // Forbid comparison. If U != T, it totally doesn't make sense, and if U == |
155 // T, it still doesn't make sense because you should never have the same | 152 // T, it still doesn't make sense because you should never have the same |
156 // object owned by two different ScopedGenerics. | 153 // object owned by two different ScopedGenerics. |
157 template <typename T2, typename Traits2> bool operator==( | 154 template <typename T2, typename Traits2> bool operator==( |
158 const ScopedGeneric<T2, Traits2>& p2) const; | 155 const ScopedGeneric<T2, Traits2>& p2) const; |
159 template <typename T2, typename Traits2> bool operator!=( | 156 template <typename T2, typename Traits2> bool operator!=( |
160 const ScopedGeneric<T2, Traits2>& p2) const; | 157 const ScopedGeneric<T2, Traits2>& p2) const; |
161 | 158 |
162 Data data_; | 159 Data data_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(ScopedGeneric); |
163 }; | 162 }; |
164 | 163 |
165 template<class T, class Traits> | 164 template<class T, class Traits> |
166 void swap(const ScopedGeneric<T, Traits>& a, | 165 void swap(const ScopedGeneric<T, Traits>& a, |
167 const ScopedGeneric<T, Traits>& b) { | 166 const ScopedGeneric<T, Traits>& b) { |
168 a.swap(b); | 167 a.swap(b); |
169 } | 168 } |
170 | 169 |
171 template<class T, class Traits> | 170 template<class T, class Traits> |
172 bool operator==(const T& value, const ScopedGeneric<T, Traits>& scoped) { | 171 bool operator==(const T& value, const ScopedGeneric<T, Traits>& scoped) { |
173 return value == scoped.get(); | 172 return value == scoped.get(); |
174 } | 173 } |
175 | 174 |
176 template<class T, class Traits> | 175 template<class T, class Traits> |
177 bool operator!=(const T& value, const ScopedGeneric<T, Traits>& scoped) { | 176 bool operator!=(const T& value, const ScopedGeneric<T, Traits>& scoped) { |
178 return value != scoped.get(); | 177 return value != scoped.get(); |
179 } | 178 } |
180 | 179 |
181 } // namespace base | 180 } // namespace base |
182 | 181 |
183 #endif // BASE_SCOPED_GENERIC_H_ | 182 #endif // BASE_SCOPED_GENERIC_H_ |
OLD | NEW |