// See also SkTScopedPtr.
template <typename T> class SkAutoTDelete : SkNoncopyable {
public:
- SkAutoTDelete(T* obj, bool deleteWhenDone = true) : fObj(obj) {
- fDeleteWhenDone = deleteWhenDone;
+ SkAutoTDelete(T* obj) : fObj(obj) {}
+ ~SkAutoTDelete() { delete fObj; }
+
+ T* get() const { return fObj; }
+ T& operator*() const { SkASSERT(fObj); return *fObj; }
+ T* operator->() const { SkASSERT(fObj); return fObj; }
+
+ /**
+ * Delete the owned object, setting the internal pointer to NULL.
+ */
+ void free() {
+ delete fObj;
+ fObj = NULL;
}
- ~SkAutoTDelete() { if (fDeleteWhenDone) delete fObj; }
- T* get() const { return fObj; }
- void free() { delete fObj; fObj = NULL; }
- T* detach() { T* obj = fObj; fObj = NULL; return obj; }
+ /**
+ * Transfer ownership of the object to the caller, setting the internal
+ * pointer to NULL. Note that this differs from get(), which also returns
+ * the pointer, but it does not transfer ownership.
+ */
+ T* detach() {
+ T* obj = fObj;
+ fObj = NULL;
+ return obj;
+ }
private:
T* fObj;
- bool fDeleteWhenDone;
};
template <typename T> class SkAutoTDeleteArray : SkNoncopyable {