| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Scopers help you manage ownership of a pointer, helping you easily manage a | 5 // Scopers help you manage ownership of a pointer, helping you easily manage a |
| 6 // pointer within a scope, and automatically destroying the pointer at the end | 6 // pointer within a scope, and automatically destroying the pointer at the end |
| 7 // of a scope. There are two main classes you will use, which correspond to the | 7 // of a scope. There are two main classes you will use, which correspond to the |
| 8 // operators new/delete and new[]/delete[]. | 8 // operators new/delete and new[]/delete[]. |
| 9 // | 9 // |
| 10 // Example usage (scoped_ptr<T>): | 10 // Example usage (scoped_ptr<T>): |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // scoped_ptr<FooParent> parent(std::move(foo)); | 85 // scoped_ptr<FooParent> parent(std::move(foo)); |
| 86 | 86 |
| 87 #ifndef BASE_MEMORY_SCOPED_PTR_H_ | 87 #ifndef BASE_MEMORY_SCOPED_PTR_H_ |
| 88 #define BASE_MEMORY_SCOPED_PTR_H_ | 88 #define BASE_MEMORY_SCOPED_PTR_H_ |
| 89 | 89 |
| 90 // This is an implementation designed to match the anticipated future TR2 | 90 // This is an implementation designed to match the anticipated future TR2 |
| 91 // implementation of the scoped_ptr class. | 91 // implementation of the scoped_ptr class. |
| 92 | 92 |
| 93 // TODO(dcheng): Clean up these headers, but there are likely lots of existing | 93 // TODO(dcheng): Clean up these headers, but there are likely lots of existing |
| 94 // IWYU violations. | 94 // IWYU violations. |
| 95 #include <stddef.h> | |
| 96 #include <stdlib.h> | 95 #include <stdlib.h> |
| 97 | 96 |
| 98 #include <memory> | 97 #include <memory> |
| 99 #include <utility> | |
| 100 | |
| 101 #include "base/macros.h" | |
| 102 | 98 |
| 103 namespace base { | 99 namespace base { |
| 104 | 100 |
| 105 // Function object which invokes 'free' on its parameter, which must be | 101 // Function object which invokes 'free' on its parameter, which must be |
| 106 // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: | 102 // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: |
| 107 // | 103 // |
| 108 // scoped_ptr<int, base::FreeDeleter> foo_ptr( | 104 // scoped_ptr<int, base::FreeDeleter> foo_ptr( |
| 109 // static_cast<int*>(malloc(sizeof(int)))); | 105 // static_cast<int*>(malloc(sizeof(int)))); |
| 110 struct FreeDeleter { | 106 struct FreeDeleter { |
| 111 inline void operator()(void* ptr) const { | 107 inline void operator()(void* ptr) const { |
| 112 free(ptr); | 108 free(ptr); |
| 113 } | 109 } |
| 114 }; | 110 }; |
| 115 | 111 |
| 116 } // namespace base | 112 } // namespace base |
| 117 | 113 |
| 118 template <typename T, typename D = std::default_delete<T>> | 114 template <typename T, typename D = std::default_delete<T>> |
| 119 using scoped_ptr = std::unique_ptr<T, D>; | 115 using scoped_ptr = std::unique_ptr<T, D>; |
| 120 | 116 |
| 121 // A function to convert T* into scoped_ptr<T> | 117 // A function to convert T* into scoped_ptr<T> |
| 122 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 118 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 123 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 119 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
| 124 template <typename T> | 120 template <typename T> |
| 125 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 121 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
| 126 return scoped_ptr<T>(ptr); | 122 return scoped_ptr<T>(ptr); |
| 127 } | 123 } |
| 128 | 124 |
| 129 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 125 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
| OLD | NEW |