diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-06-12 20:47:53 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-06-12 20:47:53 +0000 |
commit | 51c62a6cfadc302bb65bb5a98e358c93223dc73f (patch) | |
tree | 34371459b9ab82133c9a2b723029fadc2fc3ef3c /include | |
parent | 64a0ec36555352ec31aa7c5a7630a5d042b010ba (diff) |
add readPtr and writePtr to SkReader32 and SkWriter32
add template helper SkSWriter32, which allocates initial storage buffer
Review URL: https://codereview.appspot.com/6299075
git-svn-id: http://skia.googlecode.com/svn/trunk@4237 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include')
-rw-r--r-- | include/core/SkReader32.h | 14 | ||||
-rw-r--r-- | include/core/SkWriter32.h | 29 |
2 files changed, 42 insertions, 1 deletions
diff --git a/include/core/SkReader32.h b/include/core/SkReader32.h index 9e25e84a55..c127d8988c 100644 --- a/include/core/SkReader32.h +++ b/include/core/SkReader32.h @@ -58,7 +58,19 @@ public: SkASSERT(fCurr <= fStop); return value; } - + + void* readPtr() { + void* ptr; + // we presume this "if" is resolved at compile-time + if (4 == sizeof(void*)) { + ptr = *(void**)fCurr; + } else { + memcpy(&ptr, fCurr, sizeof(void*)); + } + fCurr += sizeof(void*); + return ptr; + } + SkScalar readScalar() { SkASSERT(ptr_align_4(fCurr)); SkScalar value = *(const SkScalar*)fCurr; diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h index 9abaa467d2..793e815b3d 100644 --- a/include/core/SkWriter32.h +++ b/include/core/SkWriter32.h @@ -77,6 +77,17 @@ public: *(int32_t*)this->reserve(sizeof(value)) = value; } + void writePtr(void* ptr) { + // Since we "know" that we're always 4-byte aligned, we can tell the + // compiler that here, by assigning to an int32 ptr. + int32_t* addr = (int32_t*)this->reserve(sizeof(void*)); + if (4 == sizeof(void*)) { + *(void**)addr = ptr; + } else { + memcpy(addr, &ptr, sizeof(void*)); + } + } + void writeScalar(SkScalar value) { *(SkScalar*)this->reserve(sizeof(value)) = value; } @@ -176,4 +187,22 @@ private: Block* newBlock(size_t bytes); }; +/** + * Helper class to allocated SIZE bytes as part of the writer, and to provide + * that storage to the constructor as its initial storage buffer. + * + * This wrapper ensures proper alignment rules are met for the storage. + */ +template <size_t SIZE> class SkSWriter32 : public SkWriter32 { +public: + SkSWriter32(size_t minSize) : SkWriter32(minSize, fData.fStorage, SIZE) {} + +private: + union { + void* fPtrAlignment; + double fDoubleAlignment; + char fStorage[SIZE]; + } fData; +}; + #endif |