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 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 // This class acts like ScopedPtr with a custom deleter (although is slightly | 17 // This class acts like unique_ptr with a custom deleter (although is slightly |
18 // 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 |
19 // 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 |
20 // object has some kind of "invalid" value. | 20 // object has some kind of "invalid" value. |
21 // | 21 // |
22 // 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 |
23 // 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 |
24 // move, etc. and get almost identical semantics that people are used to from | 24 // move, etc. and get almost identical semantics that people are used to from |
25 // scoped_ptr. | 25 // unique_ptr. |
26 // | 26 // |
27 // It is intended that you will typedef this class with an appropriate deleter | 27 // It is intended that you will typedef this class with an appropriate deleter |
28 // to implement clean up tasks for objects that act like pointers from a | 28 // to implement clean up tasks for objects that act like pointers from a |
29 // resource management standpoint but aren't, such as file descriptors and | 29 // resource management standpoint but aren't, such as file descriptors and |
30 // various types of operating system handles. Using scoped_ptr for these | 30 // various types of operating system handles. Using unique_ptr for these |
31 // things requires that you keep a pointer to the handle valid for the lifetime | 31 // things requires that you keep a pointer to the handle valid for the lifetime |
32 // of the scoper (which is easy to mess up). | 32 // of the scoper (which is easy to mess up). |
33 // | 33 // |
34 // For an object to be able to be put into a ScopedGeneric, it must support | 34 // For an object to be able to be put into a ScopedGeneric, it must support |
35 // standard copyable semantics and have a specific "invalid" value. The traits | 35 // standard copyable semantics and have a specific "invalid" value. The traits |
36 // must define a free function and also the invalid value to assign for | 36 // must define a free function and also the invalid value to assign for |
37 // default-constructed and released objects. | 37 // default-constructed and released objects. |
38 // | 38 // |
39 // struct FooScopedTraits { | 39 // struct FooScopedTraits { |
40 // // It's assumed that this is a fast inline function with little-to-no | 40 // // It's assumed that this is a fast inline function with little-to-no |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 FreeIfNecessary(); | 90 FreeIfNecessary(); |
91 } | 91 } |
92 | 92 |
93 // operator=. Allows assignment from a ScopedGeneric rvalue. | 93 // operator=. Allows assignment from a ScopedGeneric rvalue. |
94 ScopedGeneric& operator=(ScopedGeneric<T, Traits>&& rvalue) { | 94 ScopedGeneric& operator=(ScopedGeneric<T, Traits>&& rvalue) { |
95 reset(rvalue.release()); | 95 reset(rvalue.release()); |
96 return *this; | 96 return *this; |
97 } | 97 } |
98 | 98 |
99 // Frees the currently owned object, if any. Then takes ownership of a new | 99 // Frees the currently owned object, if any. Then takes ownership of a new |
100 // object, if given. Self-resets are not allowd as on scoped_ptr. See | 100 // object, if given. Self-resets are not allowd as on unique_ptr. See |
101 // http://crbug.com/162971 | 101 // http://crbug.com/162971 |
102 void reset(const element_type& value = traits_type::InvalidValue()) { | 102 void reset(const element_type& value = traits_type::InvalidValue()) { |
103 if (data_.generic != traits_type::InvalidValue() && data_.generic == value) | 103 if (data_.generic != traits_type::InvalidValue() && data_.generic == value) |
104 abort(); | 104 abort(); |
105 FreeIfNecessary(); | 105 FreeIfNecessary(); |
106 data_.generic = value; | 106 data_.generic = value; |
107 } | 107 } |
108 | 108 |
109 void swap(ScopedGeneric& other) { | 109 void swap(ScopedGeneric& other) { |
110 // Standard swap idiom: 'using std::swap' ensures that std::swap is | 110 // Standard swap idiom: 'using std::swap' ensures that std::swap is |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 } | 173 } |
174 | 174 |
175 template<class T, class Traits> | 175 template<class T, class Traits> |
176 bool operator!=(const T& value, const ScopedGeneric<T, Traits>& scoped) { | 176 bool operator!=(const T& value, const ScopedGeneric<T, Traits>& scoped) { |
177 return value != scoped.get(); | 177 return value != scoped.get(); |
178 } | 178 } |
179 | 179 |
180 } // namespace base | 180 } // namespace base |
181 | 181 |
182 #endif // BASE_SCOPED_GENERIC_H_ | 182 #endif // BASE_SCOPED_GENERIC_H_ |
OLD | NEW |