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

Side by Side Diff: third_party/base/nonstd_unique_ptr.h

Issue 1358163002: Add nonstd::unique_ptr move assigment operator. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: reset(that.release()) and same for []s. Created 5 years, 2 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
OLDNEW
1 // Copyright 2013 Google Inc. All Rights Reserved. 1 // Copyright 2013 Google Inc. All Rights Reserved.
2 // 2 //
3 // Redistribution and use in source and binary forms, with or without 3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are 4 // modification, are permitted provided that the following conditions are
5 // met: 5 // met:
6 // 6 //
7 // * Redistributions of source code must retain the above copyright 7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer. 8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above 9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer 10 // copyright notice, this list of conditions and the following disclaimer
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 // this->reset(this->get()) works. 169 // this->reset(this->get()) works.
170 void reset(C* p = NULL) { 170 void reset(C* p = NULL) {
171 if (p != ptr_) { 171 if (p != ptr_) {
172 enum { type_must_be_complete = sizeof(C) }; 172 enum { type_must_be_complete = sizeof(C) };
173 C* old_ptr = ptr_; 173 C* old_ptr = ptr_;
174 ptr_ = p; 174 ptr_ = p;
175 delete old_ptr; 175 delete old_ptr;
176 } 176 }
177 } 177 }
178 178
179 // Move assignment.
180 unique_ptr<C>& operator=(unique_ptr<C>&& that) {
181 if (that.ptr_ != ptr_)
182 reset(that.release());
183 return *this;
184 }
185
179 private: 186 private:
180 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't 187 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't
181 // make sense, and if C2 == C, it still doesn't make sense because you should 188 // make sense, and if C2 == C, it still doesn't make sense because you should
182 // never have the same object owned by two different unique_ptrs. 189 // never have the same object owned by two different unique_ptrs.
183 template <class C2> bool operator==(unique_ptr<C2> const& p2) const; 190 template <class C2> bool operator==(unique_ptr<C2> const& p2) const;
184 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const; 191 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const;
185 192
186 // Disallow evil constructors. It doesn't make sense to make a copy of 193 // Disallow evil constructors. It doesn't make sense to make a copy of
187 // something that's allegedly unique. 194 // something that's allegedly unique.
188 unique_ptr(const unique_ptr&) = delete; 195 unique_ptr(const unique_ptr&) = delete;
(...skipping 26 matching lines...) Expand all
215 // this->reset(this->get()) works. 222 // this->reset(this->get()) works.
216 void reset(C* p = NULL) { 223 void reset(C* p = NULL) {
217 if (p != ptr_) { 224 if (p != ptr_) {
218 enum { type_must_be_complete = sizeof(C) }; 225 enum { type_must_be_complete = sizeof(C) };
219 C* old_ptr = ptr_; 226 C* old_ptr = ptr_;
220 ptr_ = p; 227 ptr_ = p;
221 delete[] old_ptr; 228 delete[] old_ptr;
222 } 229 }
223 } 230 }
224 231
232 // Move assignment.
233 unique_ptr<C>& operator=(unique_ptr<C>&& that) {
234 if (that.ptr_ != ptr_)
235 reset(that.release());
236 return *this;
237 }
238
225 // Support indexing since it is holding array. 239 // Support indexing since it is holding array.
226 C& operator[] (size_t i) { return ptr_[i]; } 240 C& operator[] (size_t i) { return ptr_[i]; }
227 241
228 private: 242 private:
229 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't 243 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't
230 // make sense, and if C2 == C, it still doesn't make sense because you should 244 // make sense, and if C2 == C, it still doesn't make sense because you should
231 // never have the same object owned by two different unique_ptrs. 245 // never have the same object owned by two different unique_ptrs.
232 template <class C2> bool operator==(unique_ptr<C2> const& p2) const; 246 template <class C2> bool operator==(unique_ptr<C2> const& p2) const;
233 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const; 247 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const;
234 248
(...skipping 15 matching lines...) Expand all
250 } 264 }
251 265
252 template <class C> 266 template <class C>
253 bool operator!=(C* p1, const unique_ptr<C>& p2) { 267 bool operator!=(C* p1, const unique_ptr<C>& p2) {
254 return p1 != p2.get(); 268 return p1 != p2.get();
255 } 269 }
256 270
257 } // namespace nonstd 271 } // namespace nonstd
258 272
259 #endif // NONSTD_UNIQUE_PTR_H_ 273 #endif // NONSTD_UNIQUE_PTR_H_
OLDNEW
« pdfium.gyp ('K') | « pdfium.gyp ('k') | third_party/base/nonstd_unique_ptr_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698