e029440758
BUG= R=djsollen@google.com, reed@google.com, vandebo@chromium.org Author: mtklein@google.com Review URL: https://chromiumcodereview.appspot.com/23621005 git-svn-id: http://skia.googlecode.com/svn/trunk@11016 2bbb7eff-a529-9590-31e7-b0007b416f81
77 lines
1.7 KiB
C++
77 lines
1.7 KiB
C++
|
|
/*
|
|
* 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 SkTScopedComPtr_DEFINED
|
|
#define SkTScopedComPtr_DEFINED
|
|
|
|
#include "SkTypes.h"
|
|
#include "SkTemplates.h"
|
|
|
|
template<typename T>
|
|
class SkBlockComRef : public T {
|
|
private:
|
|
virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0;
|
|
virtual ULONG STDMETHODCALLTYPE Release(void) = 0;
|
|
};
|
|
|
|
template<typename T> T* SkRefComPtr(T* ptr) {
|
|
ptr->AddRef();
|
|
return ptr;
|
|
}
|
|
|
|
template<typename T> T* SkSafeRefComPtr(T* ptr) {
|
|
if (ptr) {
|
|
ptr->AddRef();
|
|
}
|
|
return ptr;
|
|
}
|
|
|
|
template<typename T>
|
|
class SkTScopedComPtr : SkNoncopyable {
|
|
private:
|
|
T *fPtr;
|
|
|
|
public:
|
|
explicit SkTScopedComPtr(T *ptr = NULL) : fPtr(ptr) { }
|
|
~SkTScopedComPtr() {
|
|
this->reset();
|
|
}
|
|
T &operator*() const { SkASSERT(fPtr != NULL); return *fPtr; }
|
|
SkBlockComRef<T> *operator->() const {
|
|
return static_cast<SkBlockComRef<T>*>(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;
|
|
}
|
|
}
|
|
|
|
void swap(SkTScopedComPtr<T>& that) {
|
|
T* temp = this->fPtr;
|
|
this->fPtr = that.fPtr;
|
|
that.fPtr = temp;
|
|
}
|
|
|
|
T* release() {
|
|
T* temp = this->fPtr;
|
|
this->fPtr = NULL;
|
|
return temp;
|
|
}
|
|
};
|
|
|
|
#endif
|