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

Side by Side Diff: include/core/SkRefCnt.h

Issue 1752093002: sk_sp: Covariant Move Constructor and Move Assignment (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: no more space between > > Created 4 years, 9 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 | « no previous file | tests/RefCntTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkRefCnt_DEFINED 8 #ifndef SkRefCnt_DEFINED
9 #define SkRefCnt_DEFINED 9 #define SkRefCnt_DEFINED
10 10
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 template <typename T> class sk_sp { 239 template <typename T> class sk_sp {
240 public: 240 public:
241 sk_sp() : fPtr(nullptr) {} 241 sk_sp() : fPtr(nullptr) {}
242 sk_sp(std::nullptr_t) : fPtr(nullptr) {} 242 sk_sp(std::nullptr_t) : fPtr(nullptr) {}
243 243
244 /** 244 /**
245 * Shares the underlying object by calling ref(), so that both the argument and the newly 245 * Shares the underlying object by calling ref(), so that both the argument and the newly
246 * created sk_sp both have a reference to it. 246 * created sk_sp both have a reference to it.
247 */ 247 */
248 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} 248 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {}
249 template <typename U,
250 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
251 sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {}
249 252
250 /** 253 /**
251 * Move the underlying object from the argument to the newly created sk_sp. Afterwards only 254 * Move the underlying object from the argument to the newly created sk_sp. Afterwards only
252 * the new sk_sp will have a reference to the object, and the argument will point to null. 255 * the new sk_sp will have a reference to the object, and the argument will point to null.
253 * No call to ref() or unref() will be made. 256 * No call to ref() or unref() will be made.
254 */ 257 */
255 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} 258 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {}
259 template <typename U,
260 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
261 sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {}
256 262
257 /** 263 /**
258 * Adopt the bare pointer into the newly created sk_sp. 264 * Adopt the bare pointer into the newly created sk_sp.
259 * No call to ref() or unref() will be made. 265 * No call to ref() or unref() will be made.
260 */ 266 */
261 explicit sk_sp(T* obj) : fPtr(obj) {} 267 explicit sk_sp(T* obj) : fPtr(obj) {}
262 268
263 /** 269 /**
264 * Calls unref() on the underlying object pointer. 270 * Calls unref() on the underlying object pointer.
265 */ 271 */
266 ~sk_sp() { 272 ~sk_sp() {
267 SkSafeUnref(fPtr); 273 SkSafeUnref(fPtr);
268 } 274 }
269 275
270 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); } 276 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; }
271 277
272 /** 278 /**
273 * Shares the underlying object referenced by the argument by calling ref() on it. If this 279 * Shares the underlying object referenced by the argument by calling ref() on it. If this
274 * sk_sp previously had a reference to an object (i.e. not null) it will ca ll unref() on that 280 * sk_sp previously had a reference to an object (i.e. not null) it will ca ll unref() on that
275 * object. 281 * object.
276 */ 282 */
277 sk_sp<T>& operator=(const sk_sp<T>& that) { 283 sk_sp<T>& operator=(const sk_sp<T>& that) {
278 this->reset(SkSafeRef(that.get())); 284 this->reset(SkSafeRef(that.get()));
279 return *this; 285 return *this;
280 } 286 }
287 template <typename U,
288 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
289 sk_sp<T>& operator=(const sk_sp<U>& that) {
290 this->reset(SkSafeRef(that.get()));
291 return *this;
292 }
281 293
282 /** 294 /**
283 * Move the underlying object from the argument to the sk_sp. If the sk_sp previously held 295 * Move the underlying object from the argument to the sk_sp. If the sk_sp previously held
284 * a reference to another object, unref() will be called on that object. No call to ref() 296 * a reference to another object, unref() will be called on that object. No call to ref()
285 * will be made. 297 * will be made.
286 */ 298 */
287 sk_sp<T>& operator=(sk_sp<T>&& that) { 299 sk_sp<T>& operator=(sk_sp<T>&& that) {
288 this->reset(that.release()); 300 this->reset(that.release());
289 return *this; 301 return *this;
290 } 302 }
303 template <typename U,
304 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
305 sk_sp<T>& operator=(sk_sp<U>&& that) {
306 this->reset(that.release());
307 return *this;
308 }
291 309
292 bool operator==(std::nullptr_t) const { return this->get() == nullptr; } 310 bool operator==(std::nullptr_t) const { return this->get() == nullptr; }
293 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } 311 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; }
294 312
295 bool operator==(const sk_sp<T>& that) const { return this->get() == that.get (); } 313 template <typename U>
296 bool operator!=(const sk_sp<T>& that) const { return this->get() != that.get (); } 314 bool operator==(const sk_sp<U>& that) const { return this->get() == that.get (); }
315 template <typename U>
316 bool operator!=(const sk_sp<U>& that) const { return this->get() != that.get (); }
297 317
298 explicit operator bool() const { return this->get() != nullptr; } 318 explicit operator bool() const { return this->get() != nullptr; }
299 319
300 T* get() const { return fPtr; } 320 T* get() const { return fPtr; }
301 T* operator->() const { return fPtr; } 321 T* operator->() const { return fPtr; }
302 322
303 /** 323 /**
304 * Adopt the new bare pointer, and call unref() on any previously held obje ct (if not null). 324 * Adopt the new bare pointer, and call unref() on any previously held obje ct (if not null).
305 * No call to ref() will be made. 325 * No call to ref() will be made.
306 */ 326 */
(...skipping 13 matching lines...) Expand all
320 T* ptr = fPtr; 340 T* ptr = fPtr;
321 fPtr = nullptr; 341 fPtr = nullptr;
322 return ptr; 342 return ptr;
323 } 343 }
324 344
325 private: 345 private:
326 T* fPtr; 346 T* fPtr;
327 }; 347 };
328 348
329 #endif 349 #endif
OLDNEW
« no previous file with comments | « no previous file | tests/RefCntTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698