Base SkAutoTUnref on skstd::unique_ptr.

To further consolidate the various unique owning classes, this bases
SkAutoTUnref on skstd::unique_ptr. Users are updated because of two
breaking changes, swap now takes a reference and reset no longer
returns its argument.

Review URL: https://codereview.chromium.org/1370803002
This commit is contained in:
bungeman 2015-10-01 12:28:49 -07:00 committed by Commit bot
parent 52f8deba60
commit 77a53de20d
6 changed files with 22 additions and 46 deletions

View File

@ -9,6 +9,7 @@
#define SkRefCnt_DEFINED
#include "../private/SkAtomics.h"
#include "../private/SkUniquePtr.h"
#include "SkTypes.h"
/** \class SkRefCntBase
@ -143,7 +144,7 @@ class SK_API SkRefCnt : public SkRefCntBase { };
} while (0)
/** Call obj->ref() and return obj. The obj must not be NULL.
/** Call obj->ref() and return obj. The obj must not be nullptr.
*/
template <typename T> static inline T* SkRef(T* obj) {
SkASSERT(obj);
@ -171,51 +172,25 @@ template <typename T> static inline void SkSafeUnref(T* obj) {
template<typename T> static inline void SkSafeSetNull(T*& obj) {
if (obj) {
obj->unref();
obj = NULL;
obj = nullptr;
}
}
///////////////////////////////////////////////////////////////////////////////
template <typename T> struct SkTUnref {
void operator()(T* t) { t->unref(); }
};
/**
* Utility class that simply unref's its argument in the destructor.
*/
template <typename T> class SkAutoTUnref : SkNoncopyable {
template <typename T> class SkAutoTUnref : public skstd::unique_ptr<T, SkTUnref<T>> {
public:
explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {}
~SkAutoTUnref() { SkSafeUnref(fObj); }
explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>(obj) {}
T* get() const { return fObj; }
T* reset(T* obj) {
SkSafeUnref(fObj);
fObj = obj;
return obj;
}
void swap(SkAutoTUnref* other) {
T* tmp = fObj;
fObj = other->fObj;
other->fObj = tmp;
}
/**
* Return the hosted object (which may be null), transferring ownership.
* The reference count is not modified, and the internal ptr is set to NULL
* so unref() will not be called in our destructor. A subsequent call to
* detach() will do nothing and return null.
*/
T* detach() {
T* obj = fObj;
fObj = NULL;
return obj;
}
T* operator->() const { return fObj; }
operator T*() const { return fObj; }
private:
T* fObj;
T* detach() { return this->release(); }
operator T*() const { return this->get(); }
};
// Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :(

View File

@ -180,7 +180,7 @@ bool operator==(const SkPath& a, const SkPath& b) {
void SkPath::swap(SkPath& that) {
if (this != &that) {
fPathRef.swap(&that.fPathRef);
fPathRef.swap(that.fPathRef);
SkTSwap<int>(fLastMoveToIndex, that.fLastMoveToIndex);
SkTSwap<uint8_t>(fFillType, that.fFillType);
SkTSwap<uint8_t>(fConvexity, that.fConvexity);

View File

@ -231,7 +231,7 @@ GrTexture* GaussianBlur(GrContext* context,
dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
SkMatrix::I(), dstRect, srcRect);
srcDrawContext.swap(&dstDrawContext);
srcDrawContext.swap(dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
@ -254,7 +254,7 @@ GrTexture* GaussianBlur(GrContext* context,
convolve_gaussian_2d(dstDrawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect);
srcDrawContext.swap(&dstDrawContext);
srcDrawContext.swap(dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
@ -286,7 +286,7 @@ GrTexture* GaussianBlur(GrContext* context,
srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
cropToRect);
srcDrawContext.swap(&dstDrawContext);
srcDrawContext.swap(dstDrawContext);
srcTexture = dstTexture;
srcRect = dstRect;
SkTSwap(dstTexture, tempTexture);
@ -319,7 +319,7 @@ GrTexture* GaussianBlur(GrContext* context,
dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
cropToRect);
srcDrawContext.swap(&dstDrawContext);
srcDrawContext.swap(dstDrawContext);
srcTexture = dstTexture;
srcRect = dstRect;
SkTSwap(dstTexture, tempTexture);
@ -355,7 +355,7 @@ GrTexture* GaussianBlur(GrContext* context,
dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
SkMatrix::I(), dstRect, srcRect);
srcDrawContext.swap(&dstDrawContext);
srcDrawContext.swap(dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);

View File

@ -92,7 +92,8 @@ DEF_TEST(FontHostStream, reporter) {
SkTypeface* origTypeface = paint.getTypeface();
SkAutoTUnref<SkTypeface> aur;
if (nullptr == origTypeface) {
origTypeface = aur.reset(SkTypeface::RefDefault());
aur.reset(SkTypeface::RefDefault());
origTypeface = aur.get();
}
int ttcIndex;

View File

@ -35,7 +35,7 @@ namespace sk_tools {
SkASSERT(pict != nullptr);
// Only work with absolute widths (as opposed to percentages).
SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
fPicture.reset(pict)->ref();
fPicture.reset(SkRef(pict));
this->CopyString(&fWritePath, writePath);
this->CopyString(&fMismatchPath, mismatchPath);
this->CopyString(&fInputFilename, inputFilename);

View File

@ -76,7 +76,7 @@ void PictureRenderer::init(const SkPicture* pict,
return;
}
fPicture.reset(pict)->ref();
fPicture.reset(SkRef(pict));
fCanvas.reset(this->setupCanvas());
}
@ -503,7 +503,7 @@ void TiledPictureRenderer::init(const SkPicture* pict, const SkString* writePath
// Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
// used by bench_pictures.
fPicture.reset(pict)->ref();
fPicture.reset(SkRef(pict));
this->CopyString(&fWritePath, writePath);
this->CopyString(&fMismatchPath, mismatchPath);
this->CopyString(&fInputFilename, inputFilename);