bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkSkTScopedPtr_DEFINED |
| 18 | #define SkSkTScopedPtr_DEFINED |
| 19 | |
| 20 | #include "SkTemplates.h" |
| 21 | |
| 22 | template<typename T> |
| 23 | class SkTScopedComPtr : SkNoncopyable { |
| 24 | private: |
| 25 | T *fPtr; |
| 26 | |
| 27 | public: |
| 28 | explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { } |
| 29 | ~SkTScopedComPtr() { |
| 30 | if (NULL != fPtr) { |
| 31 | fPtr->Release(); |
| 32 | fPtr = NULL; |
| 33 | } |
| 34 | } |
| 35 | T &operator*() const { return *fPtr; } |
| 36 | T *operator->() const { return fPtr; } |
| 37 | /** |
| 38 | * Returns the address of the underlying pointer. |
| 39 | * This is dangerous -- it breaks encapsulation and the reference escapes. |
| 40 | * Must only be used on instances currently pointing to NULL, |
| 41 | * and only to initialize the instance. |
| 42 | */ |
| 43 | T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; } |
| 44 | T *get() const { return fPtr; } |
| 45 | }; |
| 46 | |
| 47 | #endif |