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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 10 | #ifndef SkSkTScopedPtr_DEFINED |
| 11 | #define SkSkTScopedPtr_DEFINED |
| 12 | |
| 13 | #include "SkTemplates.h" |
| 14 | |
| 15 | template<typename T> |
| 16 | class SkTScopedComPtr : SkNoncopyable { |
| 17 | private: |
| 18 | T *fPtr; |
| 19 | |
| 20 | public: |
| 21 | explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { } |
| 22 | ~SkTScopedComPtr() { |
| 23 | if (NULL != fPtr) { |
| 24 | fPtr->Release(); |
| 25 | fPtr = NULL; |
| 26 | } |
| 27 | } |
| 28 | T &operator*() const { return *fPtr; } |
| 29 | T *operator->() const { return fPtr; } |
| 30 | /** |
| 31 | * Returns the address of the underlying pointer. |
| 32 | * This is dangerous -- it breaks encapsulation and the reference escapes. |
| 33 | * Must only be used on instances currently pointing to NULL, |
| 34 | * and only to initialize the instance. |
| 35 | */ |
| 36 | T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; } |
| 37 | T *get() const { return fPtr; } |
| 38 | }; |
| 39 | |
| 40 | #endif |