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

Unified Diff: include/core/SkTemplates.h

Issue 14873006: XPS ttc handling. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Fix clang error about typename and dependent type. 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,24 @@
};
///@}
+/**
+ * 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<typename SkTConstType<char, SkTIsConst<D>::value>::type*>(ptr) + byteOffset
+ );
+}
+
/** \class SkAutoTCallVProc
Call a function when this goes out of scope. The template uses two
@@ -252,12 +270,18 @@
char fStorage[N * sizeof(T)];
};
-/** Allocate a temp array on the stack/heap.
- Does NOT call any constructors/destructors on T (i.e. T must be POD)
-*/
+/** Manages an array of T elements, freeing the array in the destructor.
+ * Does NOT call any constructors/destructors on T (T must be POD).
+ */
template <typename T> class SkAutoTMalloc : SkNoncopyable {
public:
- SkAutoTMalloc(size_t count) {
+ /** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */
+ explicit SkAutoTMalloc(T* ptr = NULL) {
+ fPtr = ptr;
+ }
+
+ /** Allocates space for 'count' Ts. */
+ explicit SkAutoTMalloc(size_t count) {
fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
}
@@ -265,8 +289,13 @@
sk_free(fPtr);
}
- // doesn't preserve contents
- void reset (size_t count) {
+ /** Resize the memory area pointed to by the current ptr preserving contents. */
+ void realloc(size_t count) {
+ fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T)));
+ }
+
+ /** Resize the memory area pointed to by the current ptr without preserving contents. */
+ void reset(size_t count) {
sk_free(fPtr);
fPtr = fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
}
@@ -289,8 +318,19 @@
return fPtr[index];
}
+ /**
+ * Transfer ownership of the ptr 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* ptr = fPtr;
+ fPtr = NULL;
+ return ptr;
+ }
+
private:
- T* fPtr;
+ T* fPtr;
};
template <size_t N, typename T> class SK_API SkAutoSTMalloc : SkNoncopyable {
« 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