blob: e0d1634cb83c2738e7913954ab065c07d4256afe [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bungeman@google.com9df621d2011-06-23 21:43:52 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.com9df621d2011-06-23 21:43:52 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bungeman@google.com9df621d2011-06-23 21:43:52 +000010#ifndef SkSkTScopedPtr_DEFINED
11#define SkSkTScopedPtr_DEFINED
12
13#include "SkTemplates.h"
14
15template<typename T>
16class SkTScopedComPtr : SkNoncopyable {
17private:
18 T *fPtr;
19
20public:
21 explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { }
22 ~SkTScopedComPtr() {
bungeman@google.coma9e586a2011-08-09 19:25:05 +000023 this->reset();
bungeman@google.com9df621d2011-06-23 21:43:52 +000024 }
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.coma9e586a2011-08-09 19:25:05 +000035 void reset() {
36 if (NULL != this->fPtr) {
37 this->fPtr->Release();
38 this->fPtr = NULL;
39 }
40 }
bungeman@google.com9df621d2011-06-23 21:43:52 +000041};
42
43#endif