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

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

Issue 1762273004: Check pointers convertible not values in sk_sp. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 | no next file » | 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 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 using unspecified_bool_type = T* sk_sp::*; 242 using unspecified_bool_type = T* sk_sp::*;
243 public: 243 public:
244 sk_sp() : fPtr(nullptr) {} 244 sk_sp() : fPtr(nullptr) {}
245 sk_sp(std::nullptr_t) : fPtr(nullptr) {} 245 sk_sp(std::nullptr_t) : fPtr(nullptr) {}
246 246
247 /** 247 /**
248 * Shares the underlying object by calling ref(), so that both the argument and the newly 248 * Shares the underlying object by calling ref(), so that both the argument and the newly
249 * created sk_sp both have a reference to it. 249 * created sk_sp both have a reference to it.
250 */ 250 */
251 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} 251 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {}
252 template <typename U, 252 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U* , T*>::value>>
253 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
254 sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {} 253 sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {}
255 254
256 /** 255 /**
257 * Move the underlying object from the argument to the newly created sk_sp. Afterwards only 256 * Move the underlying object from the argument to the newly created sk_sp. Afterwards only
258 * the new sk_sp will have a reference to the object, and the argument will point to null. 257 * the new sk_sp will have a reference to the object, and the argument will point to null.
259 * No call to ref() or unref() will be made. 258 * No call to ref() or unref() will be made.
260 */ 259 */
261 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} 260 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {}
262 template <typename U, 261 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U* , T*>::value>>
263 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
264 sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {} 262 sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {}
265 263
266 /** 264 /**
267 * Adopt the bare pointer into the newly created sk_sp. 265 * Adopt the bare pointer into the newly created sk_sp.
268 * No call to ref() or unref() will be made. 266 * No call to ref() or unref() will be made.
269 */ 267 */
270 explicit sk_sp(T* obj) : fPtr(obj) {} 268 explicit sk_sp(T* obj) : fPtr(obj) {}
271 269
272 /** 270 /**
273 * Calls unref() on the underlying object pointer. 271 * Calls unref() on the underlying object pointer.
274 */ 272 */
275 ~sk_sp() { 273 ~sk_sp() {
276 SkSafeUnref(fPtr); 274 SkSafeUnref(fPtr);
277 } 275 }
278 276
279 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; } 277 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; }
280 278
281 /** 279 /**
282 * Shares the underlying object referenced by the argument by calling ref() on it. If this 280 * Shares the underlying object referenced by the argument by calling ref() on it. If this
283 * sk_sp previously had a reference to an object (i.e. not null) it will ca ll unref() on that 281 * sk_sp previously had a reference to an object (i.e. not null) it will ca ll unref() on that
284 * object. 282 * object.
285 */ 283 */
286 sk_sp<T>& operator=(const sk_sp<T>& that) { 284 sk_sp<T>& operator=(const sk_sp<T>& that) {
287 this->reset(SkSafeRef(that.get())); 285 this->reset(SkSafeRef(that.get()));
288 return *this; 286 return *this;
289 } 287 }
290 template <typename U, 288 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U* , T*>::value>>
291 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
292 sk_sp<T>& operator=(const sk_sp<U>& that) { 289 sk_sp<T>& operator=(const sk_sp<U>& that) {
293 this->reset(SkSafeRef(that.get())); 290 this->reset(SkSafeRef(that.get()));
294 return *this; 291 return *this;
295 } 292 }
296 293
297 /** 294 /**
298 * 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
299 * 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()
300 * will be made. 297 * will be made.
301 */ 298 */
302 sk_sp<T>& operator=(sk_sp<T>&& that) { 299 sk_sp<T>& operator=(sk_sp<T>&& that) {
303 this->reset(that.release()); 300 this->reset(that.release());
304 return *this; 301 return *this;
305 } 302 }
306 template <typename U, 303 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U* , T*>::value>>
307 typename = skstd::enable_if_t<skstd::is_convertible<U, T>::value>>
308 sk_sp<T>& operator=(sk_sp<U>&& that) { 304 sk_sp<T>& operator=(sk_sp<U>&& that) {
309 this->reset(that.release()); 305 this->reset(that.release());
310 return *this; 306 return *this;
311 } 307 }
312 308
313 bool operator==(std::nullptr_t) const { return this->get() == nullptr; } 309 bool operator==(std::nullptr_t) const { return this->get() == nullptr; }
314 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } 310 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; }
315 311
316 template <typename U> 312 template <typename U>
317 bool operator==(const sk_sp<U>& that) const { return this->get() == that.get (); } 313 bool operator==(const sk_sp<U>& that) const { return this->get() == that.get (); }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 * The caller must assume ownership of the object, and manage its reference count directly. 345 * The caller must assume ownership of the object, and manage its reference count directly.
350 * No call to unref() will be made. 346 * No call to unref() will be made.
351 */ 347 */
352 T* SK_WARN_UNUSED_RESULT release() { 348 T* SK_WARN_UNUSED_RESULT release() {
353 T* ptr = fPtr; 349 T* ptr = fPtr;
354 fPtr = nullptr; 350 fPtr = nullptr;
355 return ptr; 351 return ptr;
356 } 352 }
357 353
358 private: 354 private:
359 T* fPtr; 355 T* fPtr;
360 }; 356 };
361 357
362 template <typename T, typename... Args> 358 template <typename T, typename... Args>
363 sk_sp<T> sk_make_sp(Args&&... args) { 359 sk_sp<T> sk_make_sp(Args&&... args) {
364 return sk_sp<T>(new T(std::forward<Args>(args)...)); 360 return sk_sp<T>(new T(std::forward<Args>(args)...));
365 } 361 }
366 362
367 #endif 363 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698