Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2011 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | #ifndef SkTScopedComPtr_DEFINED |
michael@0 | 10 | #define SkTScopedComPtr_DEFINED |
michael@0 | 11 | |
michael@0 | 12 | #include "SkTypes.h" |
michael@0 | 13 | #include "SkTemplates.h" |
michael@0 | 14 | |
michael@0 | 15 | template<typename T> |
michael@0 | 16 | class SkBlockComRef : public T { |
michael@0 | 17 | private: |
michael@0 | 18 | virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0; |
michael@0 | 19 | virtual ULONG STDMETHODCALLTYPE Release(void) = 0; |
michael@0 | 20 | }; |
michael@0 | 21 | |
michael@0 | 22 | template<typename T> T* SkRefComPtr(T* ptr) { |
michael@0 | 23 | ptr->AddRef(); |
michael@0 | 24 | return ptr; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | template<typename T> T* SkSafeRefComPtr(T* ptr) { |
michael@0 | 28 | if (ptr) { |
michael@0 | 29 | ptr->AddRef(); |
michael@0 | 30 | } |
michael@0 | 31 | return ptr; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | template<typename T> |
michael@0 | 35 | class SkTScopedComPtr : SkNoncopyable { |
michael@0 | 36 | private: |
michael@0 | 37 | T *fPtr; |
michael@0 | 38 | |
michael@0 | 39 | public: |
michael@0 | 40 | explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { } |
michael@0 | 41 | ~SkTScopedComPtr() { |
michael@0 | 42 | this->reset(); |
michael@0 | 43 | } |
michael@0 | 44 | T &operator*() const { SkASSERT(fPtr != NULL); return *fPtr; } |
michael@0 | 45 | SkBlockComRef<T> *operator->() const { |
michael@0 | 46 | return static_cast<SkBlockComRef<T>*>(fPtr); |
michael@0 | 47 | } |
michael@0 | 48 | /** |
michael@0 | 49 | * Returns the address of the underlying pointer. |
michael@0 | 50 | * This is dangerous -- it breaks encapsulation and the reference escapes. |
michael@0 | 51 | * Must only be used on instances currently pointing to NULL, |
michael@0 | 52 | * and only to initialize the instance. |
michael@0 | 53 | */ |
michael@0 | 54 | T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; } |
michael@0 | 55 | T *get() const { return fPtr; } |
michael@0 | 56 | void reset() { |
michael@0 | 57 | if (NULL != this->fPtr) { |
michael@0 | 58 | this->fPtr->Release(); |
michael@0 | 59 | this->fPtr = NULL; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | void swap(SkTScopedComPtr<T>& that) { |
michael@0 | 64 | T* temp = this->fPtr; |
michael@0 | 65 | this->fPtr = that.fPtr; |
michael@0 | 66 | that.fPtr = temp; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | T* release() { |
michael@0 | 70 | T* temp = this->fPtr; |
michael@0 | 71 | this->fPtr = NULL; |
michael@0 | 72 | return temp; |
michael@0 | 73 | } |
michael@0 | 74 | }; |
michael@0 | 75 | |
michael@0 | 76 | #endif |