aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/utils/win/SkTScopedComPtr.h
blob: e0d1634cb83c2738e7913954ab065c07d4256afe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#ifndef SkSkTScopedPtr_DEFINED
#define SkSkTScopedPtr_DEFINED

#include "SkTemplates.h"

template<typename T>
class SkTScopedComPtr : SkNoncopyable {
private:
    T *fPtr;

public:
    explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { }
    ~SkTScopedComPtr() {
        this->reset();
    }
    T &operator*() const { return *fPtr; }
    T *operator->() const { return fPtr; }
    /**
     * Returns the address of the underlying pointer.
     * This is dangerous -- it breaks encapsulation and the reference escapes.
     * Must only be used on instances currently pointing to NULL,
     * and only to initialize the instance.
     */
    T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; }
    T *get() const { return fPtr; }
    void reset() {
        if (NULL != this->fPtr) {
            this->fPtr->Release();
            this->fPtr = NULL;
        }
    }
};

#endif