Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Unified Diff: include/core/SkTemplates.h

Issue 14873006: XPS ttc handling. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Remove experimental non-GUID GUID code. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gyp/xps.gyp ('k') | include/device/xps/SkConstexprMath.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {}
« no previous file with comments | « gyp/xps.gyp ('k') | include/device/xps/SkConstexprMath.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698