Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(338)

Side by Side Diff: base/memory/scoped_ptr.h

Issue 9958150: Make sure that scoped_ptr<> cannot be used with ref-counted objects. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.nc » ('j') | base/memory/scoped_ptr_unittest.nc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 the 5 // Scopers help you manage ownership of a pointer, helping you easily manage the
6 // a pointer within a scope, and automatically destroying the pointer at the 6 // a pointer within a scope, and automatically destroying the pointer at the
7 // end of a scope. There are two main classes you will use, which correspond 7 // end of a scope. There are two main classes you will use, which correspond
8 // to the operators new/delete and new[]/delete[]. 8 // to the operators new/delete and new[]/delete[].
9 // 9 //
10 // Example usage (scoped_ptr): 10 // Example usage (scoped_ptr):
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 #pragma once 89 #pragma once
90 90
91 // This is an implementation designed to match the anticipated future TR2 91 // This is an implementation designed to match the anticipated future TR2
92 // implementation of the scoped_ptr class, and its closely-related brethren, 92 // implementation of the scoped_ptr class, and its closely-related brethren,
93 // scoped_array, scoped_ptr_malloc. 93 // scoped_array, scoped_ptr_malloc.
94 94
95 #include <assert.h> 95 #include <assert.h>
96 #include <stddef.h> 96 #include <stddef.h>
97 #include <stdlib.h> 97 #include <stdlib.h>
98 98
99 #include "base/basictypes.h"
99 #include "base/compiler_specific.h" 100 #include "base/compiler_specific.h"
100 #include "base/move.h" 101 #include "base/move.h"
102 #include "base/template_util.h"
103
104 namespace base {
105 namespace subtle {
106 class RefCountedBase;
107 class RefCountedThreadSafeBase;
108 } // namespace subtle
109
110 namespace internal {
111
112 template <typename T> struct IsNotRefCounted {
113 enum {
114 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value &&
115 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>::
116 value
117 };
118 };
119
120 } // namespace internal
121 } // namespace base
101 122
102 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> 123 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
103 // automatically deletes the pointer it holds (if any). 124 // automatically deletes the pointer it holds (if any).
104 // That is, scoped_ptr<T> owns the T object that it points to. 125 // That is, scoped_ptr<T> owns the T object that it points to.
105 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. 126 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
106 // Also like T*, scoped_ptr<T> is thread-compatible, and once you 127 // Also like T*, scoped_ptr<T> is thread-compatible, and once you
107 // dereference it, you get the thread safety guarantees of T. 128 // dereference it, you get the thread safety guarantees of T.
108 // 129 //
109 // The size of a scoped_ptr is small: 130 // The size of a scoped_ptr is small:
110 // sizeof(scoped_ptr<C>) == sizeof(C*) 131 // sizeof(scoped_ptr<C>) == sizeof(C*)
111 template <class C> 132 template <class C>
112 class scoped_ptr { 133 class scoped_ptr {
113 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) 134 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
114 135
136 COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value,
137 C_is_refcounted_type_and_needs_scoped_refptr);
138
115 public: 139 public:
116 140
117 // The element type 141 // The element type
118 typedef C element_type; 142 typedef C element_type;
119 143
120 // Constructor. Defaults to initializing with NULL. 144 // Constructor. Defaults to initializing with NULL.
121 // There is no way to create an uninitialized scoped_ptr. 145 // There is no way to create an uninitialized scoped_ptr.
122 // The input parameter must be allocated with new. 146 // The input parameter must be allocated with new.
123 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } 147 explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
124 148
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { 486 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
463 return p == b.get(); 487 return p == b.get();
464 } 488 }
465 489
466 template<class C, class FP> inline 490 template<class C, class FP> inline
467 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { 491 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
468 return p != b.get(); 492 return p != b.get();
469 } 493 }
470 494
471 #endif // BASE_MEMORY_SCOPED_PTR_H_ 495 #endif // BASE_MEMORY_SCOPED_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.nc » ('j') | base/memory/scoped_ptr_unittest.nc » ('J')

Powered by Google App Engine
This is Rietveld 408576698