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

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') | no next file with comments »
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
106 namespace subtle {
107 class RefCountedBase;
108 class RefCountedThreadSafeBase;
109 } // namespace subtle
110
111 namespace internal {
112
113 template <typename T> struct IsNotRefCounted {
114 enum {
115 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value &&
116 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>::
117 value
118 };
119 };
120
121 } // namespace internal
122 } // namespace base
101 123
102 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> 124 // 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). 125 // automatically deletes the pointer it holds (if any).
104 // That is, scoped_ptr<T> owns the T object that it points to. 126 // 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. 127 // 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 128 // Also like T*, scoped_ptr<T> is thread-compatible, and once you
107 // dereference it, you get the thread safety guarantees of T. 129 // dereference it, you get the thread safety guarantees of T.
108 // 130 //
109 // The size of a scoped_ptr is small: 131 // The size of a scoped_ptr is small:
110 // sizeof(scoped_ptr<C>) == sizeof(C*) 132 // sizeof(scoped_ptr<C>) == sizeof(C*)
111 template <class C> 133 template <class C>
112 class scoped_ptr { 134 class scoped_ptr {
113 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) 135 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
114 136
137 COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value,
138 C_is_refcounted_type_and_needs_scoped_refptr);
139
115 public: 140 public:
116 141
117 // The element type 142 // The element type
118 typedef C element_type; 143 typedef C element_type;
119 144
120 // Constructor. Defaults to initializing with NULL. 145 // Constructor. Defaults to initializing with NULL.
121 // There is no way to create an uninitialized scoped_ptr. 146 // There is no way to create an uninitialized scoped_ptr.
122 // The input parameter must be allocated with new. 147 // The input parameter must be allocated with new.
123 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } 148 explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
124 149
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { 487 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
463 return p == b.get(); 488 return p == b.get();
464 } 489 }
465 490
466 template<class C, class FP> inline 491 template<class C, class FP> inline
467 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { 492 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
468 return p != b.get(); 493 return p != b.get();
469 } 494 }
470 495
471 #endif // BASE_MEMORY_SCOPED_PTR_H_ 496 #endif // BASE_MEMORY_SCOPED_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.nc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698