epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 9 | #ifndef SkSkTScopedPtr_DEFINED |
| 10 | #define SkSkTScopedPtr_DEFINED |
| 11 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 12 | #include "SkTypes.h" |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 13 | #include "SkTemplates.h" |
| 14 | |
| 15 | template<typename T> |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 16 | class SkBlockComRef : public T { |
| 17 | private: |
| 18 | virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0; |
| 19 | virtual ULONG STDMETHODCALLTYPE Release(void) = 0; |
| 20 | }; |
| 21 | |
| 22 | template<typename T> |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 23 | class SkTScopedComPtr : SkNoncopyable { |
| 24 | private: |
| 25 | T *fPtr; |
| 26 | |
| 27 | public: |
| 28 | explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { } |
| 29 | ~SkTScopedComPtr() { |
bungeman@google.com | a9e586a | 2011-08-09 19:25:05 +0000 | [diff] [blame] | 30 | this->reset(); |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 31 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame^] | 32 | T &operator*() const { SkASSERT(fPtr != NULL); return *fPtr; } |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 33 | SkBlockComRef<T> *operator->() const { |
| 34 | return static_cast<SkBlockComRef<T>*>(fPtr); |
| 35 | } |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Returns the address of the underlying pointer. |
| 38 | * This is dangerous -- it breaks encapsulation and the reference escapes. |
| 39 | * Must only be used on instances currently pointing to NULL, |
| 40 | * and only to initialize the instance. |
| 41 | */ |
| 42 | T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; } |
| 43 | T *get() const { return fPtr; } |
bungeman@google.com | a9e586a | 2011-08-09 19:25:05 +0000 | [diff] [blame] | 44 | void reset() { |
| 45 | if (NULL != this->fPtr) { |
| 46 | this->fPtr->Release(); |
| 47 | this->fPtr = NULL; |
| 48 | } |
| 49 | } |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 50 | |
| 51 | void swap(SkTScopedComPtr<T>& that) { |
| 52 | T* temp = this->fPtr; |
| 53 | this->fPtr = that.fPtr; |
| 54 | that.fPtr = temp; |
| 55 | } |
| 56 | |
| 57 | T* release() { |
| 58 | T* temp = this->fPtr; |
| 59 | this->fPtr = NULL; |
| 60 | return temp; |
| 61 | } |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | #endif |