Index: include/core/SkTemplates.h |
=================================================================== |
--- include/core/SkTemplates.h (revision 9016) |
+++ include/core/SkTemplates.h (working copy) |
@@ -46,6 +46,23 @@ |
}; |
///@} |
+/** |
+ * Returns a pointer to a D which comes immediately after S[count]. |
+ */ |
+template <typename D, typename S> static D* SkTAfter(S* ptr, size_t count = 1) { |
+ return reinterpret_cast<D*>(ptr + count); |
+} |
+ |
+/** |
+ * Returns a pointer to a D which comes byteOffset bytes after S. |
+ */ |
+template <typename D, typename S> static D* SkTAddOffset(S* ptr, size_t byteOffset) { |
+ // The intermediate char* has the same const-ness as D as this produces better error messages. |
+ // This relies on the fact that reinterpret_cast can add constness, but cannot remove it. |
+ return reinterpret_cast<D*>( |
+ reinterpret_cast<SkTConstType<char, SkTIsConst<D>::value>::type*>(ptr) + byteOffset); |
+} |
+ |
/** \class SkAutoTCallVProc |
Call a function when this goes out of scope. The template uses two |
@@ -80,6 +97,42 @@ |
T* fObj; |
}; |
+template <typename T> class SkAutoTFree : SkNoncopyable { |
+public: |
+ explicit SkAutoTFree(T* ptr = NULL) : fPtr(ptr) { } |
+ ~SkAutoTFree() { sk_free(fPtr); } |
+ |
+ /** Return the current buffer, or null. */ |
+ T* get() const { return fPtr; } |
+ |
+ /** Assign a new ptr allocated with sk_malloc (or null), and return the |
+ previous ptr. Note it is the caller's responsibility to sk_free the |
+ returned ptr. |
+ */ |
+ T* set(T* ptr) { |
reed1
2013/05/06 18:09:58
Do we have this pattern (where we return the old a
bungeman-skia
2013/05/06 20:48:45
Hmmm... not really. This is mostly copy-pasta from
|
+ T* prev = fPtr; |
+ fPtr = ptr; |
+ return prev; |
+ } |
+ |
+ /** Transfer ownership of the current ptr to the caller, setting the |
+ internal reference to null. Note the caller is reponsible for calling |
+ sk_free on the returned address. |
+ */ |
+ T* detach() { return this->set(NULL); } |
+ |
+ /** Free the current buffer, and set the internal reference to NULL. Same |
+ as calling sk_free(detach()) |
+ */ |
+ void free() { |
+ sk_free(fPtr); |
+ fPtr = NULL; |
+ } |
+ |
+private: |
+ T* fPtr; |
+}; |
+ |
template <typename T> class SkAutoTDelete : SkNoncopyable { |
public: |
SkAutoTDelete(T* obj = NULL) : fObj(obj) {} |