| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 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 SkTScopedComPtr_DEFINED | 8 #ifndef SkTScopedComPtr_DEFINED |
| 9 #define SkTScopedComPtr_DEFINED | 9 #define SkTScopedComPtr_DEFINED |
| 10 | 10 |
| 11 #include "../../private/SkLeanWindows.h" | 11 #include "SkLeanWindows.h" |
| 12 | 12 |
| 13 #ifdef SK_BUILD_FOR_WIN | 13 #ifdef SK_BUILD_FOR_WIN |
| 14 | 14 |
| 15 template<typename T> | 15 template<typename T> |
| 16 class SkBlockComRef : public T { | 16 class SkBlockComRef : public T { |
| 17 private: | 17 private: |
| 18 virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0; | 18 virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0; |
| 19 virtual ULONG STDMETHODCALLTYPE Release(void) = 0; | 19 virtual ULONG STDMETHODCALLTYPE Release(void) = 0; |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 template<typename T> T* SkRefComPtr(T* ptr) { | 22 template<typename T> T* SkRefComPtr(T* ptr) { |
| 23 ptr->AddRef(); | 23 ptr->AddRef(); |
| 24 return ptr; | 24 return ptr; |
| 25 } | 25 } |
| 26 | 26 |
| 27 template<typename T> T* SkSafeRefComPtr(T* ptr) { | 27 template<typename T> T* SkSafeRefComPtr(T* ptr) { |
| 28 if (ptr) { | 28 if (ptr) { |
| 29 ptr->AddRef(); | 29 ptr->AddRef(); |
| 30 } | 30 } |
| 31 return ptr; | 31 return ptr; |
| 32 } | 32 } |
| 33 | 33 |
| 34 template<typename T> | 34 template<typename T> |
| 35 class SkTScopedComPtr : SkNoncopyable { | 35 class SkTScopedComPtr : SkNoncopyable { |
| 36 private: | 36 private: |
| 37 T *fPtr; | 37 T *fPtr; |
| 38 | 38 |
| 39 public: | 39 public: |
| 40 explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { } | 40 explicit SkTScopedComPtr(T *ptr = nullptr) : fPtr(ptr) { } |
| 41 ~SkTScopedComPtr() { | 41 |
| 42 this->reset(); | 42 ~SkTScopedComPtr() { this->reset();} |
| 43 } | 43 |
| 44 T &operator*() const { SkASSERT(fPtr != NULL); return *fPtr; } | 44 T &operator*() const { SkASSERT(fPtr != nullptr); return *fPtr; } |
| 45 SkBlockComRef<T> *operator->() const { | 45 |
| 46 return static_cast<SkBlockComRef<T>*>(fPtr); | 46 explicit operator bool() const { return fPtr != nullptr; } |
| 47 } | 47 |
| 48 SkBlockComRef<T> *operator->() const { return static_cast<SkBlockComRef<T>*>
(fPtr); } |
| 49 |
| 48 /** | 50 /** |
| 49 * Returns the address of the underlying pointer. | 51 * Returns the address of the underlying pointer. |
| 50 * This is dangerous -- it breaks encapsulation and the reference escapes. | 52 * This is dangerous -- it breaks encapsulation and the reference escapes. |
| 51 * Must only be used on instances currently pointing to NULL, | 53 * Must only be used on instances currently pointing to NULL, |
| 52 * and only to initialize the instance. | 54 * and only to initialize the instance. |
| 53 */ | 55 */ |
| 54 T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; } | 56 T **operator&() { SkASSERT(fPtr == nullptr); return &fPtr; } |
| 57 |
| 55 T *get() const { return fPtr; } | 58 T *get() const { return fPtr; } |
| 59 |
| 56 void reset() { | 60 void reset() { |
| 57 if (this->fPtr) { | 61 if (this->fPtr) { |
| 58 this->fPtr->Release(); | 62 this->fPtr->Release(); |
| 59 this->fPtr = NULL; | 63 this->fPtr = nullptr; |
| 60 } | 64 } |
| 61 } | 65 } |
| 62 | 66 |
| 63 void swap(SkTScopedComPtr<T>& that) { | 67 void swap(SkTScopedComPtr<T>& that) { |
| 64 T* temp = this->fPtr; | 68 T* temp = this->fPtr; |
| 65 this->fPtr = that.fPtr; | 69 this->fPtr = that.fPtr; |
| 66 that.fPtr = temp; | 70 that.fPtr = temp; |
| 67 } | 71 } |
| 68 | 72 |
| 69 T* release() { | 73 T* release() { |
| 70 T* temp = this->fPtr; | 74 T* temp = this->fPtr; |
| 71 this->fPtr = NULL; | 75 this->fPtr = nullptr; |
| 72 return temp; | 76 return temp; |
| 73 } | 77 } |
| 74 }; | 78 }; |
| 75 | 79 |
| 76 #endif // SK_BUILD_FOR_WIN | 80 #endif // SK_BUILD_FOR_WIN |
| 77 #endif // SkTScopedComPtr_DEFINED | 81 #endif // SkTScopedComPtr_DEFINED |
| OLD | NEW |