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