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() { |
bungeman@google.com | a9e586a | 2011-08-09 19:25:05 +0000 | [diff] [blame] | 23 | this->reset(); |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 24 | } |
| 25 | T &operator*() const { return *fPtr; } |
| 26 | T *operator->() const { return fPtr; } |
| 27 | /** |
| 28 | * Returns the address of the underlying pointer. |
| 29 | * This is dangerous -- it breaks encapsulation and the reference escapes. |
| 30 | * Must only be used on instances currently pointing to NULL, |
| 31 | * and only to initialize the instance. |
| 32 | */ |
| 33 | T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; } |
| 34 | T *get() const { return fPtr; } |
bungeman@google.com | a9e586a | 2011-08-09 19:25:05 +0000 | [diff] [blame] | 35 | void reset() { |
| 36 | if (NULL != this->fPtr) { |
| 37 | this->fPtr->Release(); |
| 38 | this->fPtr = NULL; |
| 39 | } |
| 40 | } |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | #endif |