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

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

Issue 1760453004: Add operator* and operator safe-bool to sk_sp. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Correct signature, write test. 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') | tests/RefCntTest.cpp » ('J')
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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 //////////////////////////////////////////////////////////////////////////////// /////////////////// 231 //////////////////////////////////////////////////////////////////////////////// ///////////////////
232 232
233 /** 233 /**
234 * Shared pointer class to wrap classes that support a ref()/unref() interface. 234 * Shared pointer class to wrap classes that support a ref()/unref() interface.
235 * 235 *
236 * This can be used for classes inheriting from SkRefCnt, but it also works for other 236 * This can be used for classes inheriting from SkRefCnt, but it also works for other
237 * classes that match the interface, but have different internal choices: e.g. the hosted class 237 * classes that match the interface, but have different internal choices: e.g. the hosted class
238 * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk _sp. 238 * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk _sp.
239 */ 239 */
240 template <typename T> class sk_sp { 240 template <typename T> class sk_sp {
241 using unspecified_bool_type = T* sk_sp::*;
mtklein 2016/03/02 20:05:10 // safe bool idiom. obsolete with explicit operat
bungeman-skia 2016/03/02 20:32:58 Done.
241 public: 242 public:
242 sk_sp() : fPtr(nullptr) {} 243 sk_sp() : fPtr(nullptr) {}
243 sk_sp(std::nullptr_t) : fPtr(nullptr) {} 244 sk_sp(std::nullptr_t) : fPtr(nullptr) {}
244 245
245 /** 246 /**
246 * Shares the underlying object by calling ref(), so that both the argument and the newly 247 * Shares the underlying object by calling ref(), so that both the argument and the newly
247 * created sk_sp both have a reference to it. 248 * created sk_sp both have a reference to it.
248 */ 249 */
249 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} 250 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {}
250 template <typename U, 251 template <typename U,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 } 310 }
310 311
311 bool operator==(std::nullptr_t) const { return this->get() == nullptr; } 312 bool operator==(std::nullptr_t) const { return this->get() == nullptr; }
312 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } 313 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; }
313 314
314 template <typename U> 315 template <typename U>
315 bool operator==(const sk_sp<U>& that) const { return this->get() == that.get (); } 316 bool operator==(const sk_sp<U>& that) const { return this->get() == that.get (); }
316 template <typename U> 317 template <typename U>
317 bool operator!=(const sk_sp<U>& that) const { return this->get() != that.get (); } 318 bool operator!=(const sk_sp<U>& that) const { return this->get() != that.get (); }
318 319
320 T& operator*() const {
321 SkASSERT(this->get() != nullptr);
322 return *this->get();
323 }
324
319 // MSVC 2013 does not work correctly with explicit operator bool. 325 // MSVC 2013 does not work correctly with explicit operator bool.
320 // https://chromium-cpp.appspot.com/#core-blacklist 326 // https://chromium-cpp.appspot.com/#core-blacklist
327 // When explicit operator bool can be used, remove operator! and operator un specified_bool_type.
321 //explicit operator bool() const { return this->get() != nullptr; } 328 //explicit operator bool() const { return this->get() != nullptr; }
322 329 operator unspecified_bool_type() const { return this->get() ? &sk_sp::fPtr : nullptr; }
mtklein 2016/03/02 20:05:10 ... ? &fPtr : nullptr ... ?
bungeman-skia 2016/03/02 20:32:58 So it turns out the type of '&fPtr' here is 'T*con
323 bool operator!() const { return this->get() == nullptr; } 330 bool operator!() const { return this->get() == nullptr; }
324 331
325 T* get() const { return fPtr; } 332 T* get() const { return fPtr; }
326 T* operator->() const { return fPtr; } 333 T* operator->() const { return fPtr; }
327 334
328 /** 335 /**
329 * Adopt the new bare pointer, and call unref() on any previously held obje ct (if not null). 336 * Adopt the new bare pointer, and call unref() on any previously held obje ct (if not null).
330 * No call to ref() will be made. 337 * No call to ref() will be made.
331 */ 338 */
332 void reset(T* ptr = nullptr) { 339 void reset(T* ptr = nullptr) {
(...skipping 17 matching lines...) Expand all
350 private: 357 private:
351 T* fPtr; 358 T* fPtr;
352 }; 359 };
353 360
354 template <typename T, typename... Args> 361 template <typename T, typename... Args>
355 sk_sp<T> sk_make_sp(Args&&... args) { 362 sk_sp<T> sk_make_sp(Args&&... args) {
356 return sk_sp<T>(new T(std::forward<Args>(args)...)); 363 return sk_sp<T>(new T(std::forward<Args>(args)...));
357 } 364 }
358 365
359 #endif 366 #endif
OLDNEW
« no previous file with comments | « no previous file | tests/RefCntTest.cpp » ('j') | tests/RefCntTest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698