blob: 68a0a81336f7262d7d4b15148d08e412b0120524 [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() {
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