gfx/skia/trunk/include/utils/win/SkTScopedComPtr.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/utils/win/SkTScopedComPtr.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +#ifndef SkTScopedComPtr_DEFINED
    1.13 +#define SkTScopedComPtr_DEFINED
    1.14 +
    1.15 +#include "SkTypes.h"
    1.16 +#include "SkTemplates.h"
    1.17 +
    1.18 +template<typename T>
    1.19 +class SkBlockComRef : public T {
    1.20 +private:
    1.21 +    virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0;
    1.22 +    virtual ULONG STDMETHODCALLTYPE Release(void) = 0;
    1.23 +};
    1.24 +
    1.25 +template<typename T> T* SkRefComPtr(T* ptr) {
    1.26 +    ptr->AddRef();
    1.27 +    return ptr;
    1.28 +}
    1.29 +
    1.30 +template<typename T> T* SkSafeRefComPtr(T* ptr) {
    1.31 +    if (ptr) {
    1.32 +        ptr->AddRef();
    1.33 +    }
    1.34 +    return ptr;
    1.35 +}
    1.36 +
    1.37 +template<typename T>
    1.38 +class SkTScopedComPtr : SkNoncopyable {
    1.39 +private:
    1.40 +    T *fPtr;
    1.41 +
    1.42 +public:
    1.43 +    explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { }
    1.44 +    ~SkTScopedComPtr() {
    1.45 +        this->reset();
    1.46 +    }
    1.47 +    T &operator*() const { SkASSERT(fPtr != NULL); return *fPtr; }
    1.48 +    SkBlockComRef<T> *operator->() const {
    1.49 +        return static_cast<SkBlockComRef<T>*>(fPtr);
    1.50 +    }
    1.51 +    /**
    1.52 +     * Returns the address of the underlying pointer.
    1.53 +     * This is dangerous -- it breaks encapsulation and the reference escapes.
    1.54 +     * Must only be used on instances currently pointing to NULL,
    1.55 +     * and only to initialize the instance.
    1.56 +     */
    1.57 +    T **operator&() { SkASSERT(fPtr == NULL); return &fPtr; }
    1.58 +    T *get() const { return fPtr; }
    1.59 +    void reset() {
    1.60 +        if (NULL != this->fPtr) {
    1.61 +            this->fPtr->Release();
    1.62 +            this->fPtr = NULL;
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    void swap(SkTScopedComPtr<T>& that) {
    1.67 +        T* temp = this->fPtr;
    1.68 +        this->fPtr = that.fPtr;
    1.69 +        that.fPtr = temp;
    1.70 +    }
    1.71 +
    1.72 +    T* release() {
    1.73 +        T* temp = this->fPtr;
    1.74 +        this->fPtr = NULL;
    1.75 +        return temp;
    1.76 +    }
    1.77 +};
    1.78 +
    1.79 +#endif

mercurial