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

Side by Side Diff: base/linked_ptr.h

Issue 249363004: Revert of Style cleanup in preparation for auto-linting base/. (Closed)
Patch Set: Created 6 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
« no previous file with comments | « base/lazy_instance_unittest.cc ('k') | base/logging_win.cc » ('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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 // A "smart" pointer type with reference tracking. Every pointer to a 5 // A "smart" pointer type with reference tracking. Every pointer to a
6 // particular object is kept on a circular linked list. When the last pointer 6 // particular object is kept on a circular linked list. When the last pointer
7 // to an object is destroyed or reassigned, the object is deleted. 7 // to an object is destroyed or reassigned, the object is deleted.
8 // 8 //
9 // Used properly, this deletes the object when the last reference goes away. 9 // Used properly, this deletes the object when the last reference goes away.
10 // There are several caveats: 10 // There are several caveats:
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 public: 78 public:
79 typedef T element_type; 79 typedef T element_type;
80 80
81 // Take over ownership of a raw pointer. This should happen as soon as 81 // Take over ownership of a raw pointer. This should happen as soon as
82 // possible after the object is created. 82 // possible after the object is created.
83 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } 83 explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
84 ~linked_ptr() { depart(); } 84 ~linked_ptr() { depart(); }
85 85
86 // Copy an existing linked_ptr<>, adding ourselves to the list of references. 86 // Copy an existing linked_ptr<>, adding ourselves to the list of references.
87 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } 87 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
88 88 linked_ptr(linked_ptr const& ptr) { DCHECK_NE(&ptr, this); copy(&ptr); }
89 linked_ptr(linked_ptr const& ptr) {
90 DCHECK_NE(&ptr, this);
91 copy(&ptr);
92 }
93 89
94 // Assignment releases the old value and acquires the new. 90 // Assignment releases the old value and acquires the new.
95 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { 91 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
96 depart(); 92 depart();
97 copy(&ptr); 93 copy(&ptr);
98 return *this; 94 return *this;
99 } 95 }
100 96
101 linked_ptr& operator=(linked_ptr const& ptr) { 97 linked_ptr& operator=(linked_ptr const& ptr) {
102 if (&ptr != this) { 98 if (&ptr != this) {
103 depart(); 99 depart();
104 copy(&ptr); 100 copy(&ptr);
105 } 101 }
106 return *this; 102 return *this;
107 } 103 }
108 104
109 // Smart pointer members. 105 // Smart pointer members.
110 void reset(T* ptr = NULL) { 106 void reset(T* ptr = NULL) { depart(); capture(ptr); }
111 depart();
112 capture(ptr);
113 }
114 T* get() const { return value_; } 107 T* get() const { return value_; }
115 T* operator->() const { return value_; } 108 T* operator->() const { return value_; }
116 T& operator*() const { return *value_; } 109 T& operator*() const { return *value_; }
117 // Release ownership of the pointed object and returns it. 110 // Release ownership of the pointed object and returns it.
118 // Sole ownership by this linked_ptr object is required. 111 // Sole ownership by this linked_ptr object is required.
119 T* release() { 112 T* release() {
120 bool last = link_.depart(); 113 bool last = link_.depart();
121 CHECK(last); 114 CHECK(last);
122 T* v = value_; 115 T* v = value_;
123 value_ = NULL; 116 value_ = NULL;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 165
173 // A function to convert T* into linked_ptr<T> 166 // A function to convert T* into linked_ptr<T>
174 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation 167 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
175 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 168 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
176 template <typename T> 169 template <typename T>
177 linked_ptr<T> make_linked_ptr(T* ptr) { 170 linked_ptr<T> make_linked_ptr(T* ptr) {
178 return linked_ptr<T>(ptr); 171 return linked_ptr<T>(ptr);
179 } 172 }
180 173
181 #endif // BASE_LINKED_PTR_H_ 174 #endif // BASE_LINKED_PTR_H_
OLDNEW
« no previous file with comments | « base/lazy_instance_unittest.cc ('k') | base/logging_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698