diff options
author | 2013-05-07 15:28:15 +0000 | |
---|---|---|
committer | 2013-05-07 15:28:15 +0000 | |
commit | b5e34e22aa0e019e25f9f913f0e119a9a97e5562 (patch) | |
tree | c064647d66956478b92bf2adcb16025daafc29b3 /include/core | |
parent | 2983ddd4b57a05e3262fe2c1964a34f1717a5a1c (diff) |
XPS ttc handling.
R=reed@google.com
Author: bungeman@google.com
Review URL: https://chromiumcodereview.appspot.com/14873006
git-svn-id: http://skia.googlecode.com/svn/trunk@9037 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/core')
-rw-r--r-- | include/core/SkTemplates.h | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h index 566834cab3..5eb7885425 100644 --- a/include/core/SkTemplates.h +++ b/include/core/SkTemplates.h @@ -46,6 +46,24 @@ template <typename T> struct SkTConstType<T, true> { }; ///@} +/** + * 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 @@ private: 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 @@ public: 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 @@ public: 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 { |