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

Unified Diff: include/core/SkWriter32.h

Issue 130913018: Templetized SkWriter32 readTAt() & overwriteTAt() (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Rebased Created 6 years, 10 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 | « no previous file | src/core/SkMatrixClipStateMgr.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkWriter32.h
diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h
index 7d8d764e5944123a0819204dd0863f6a1b84bacb..586fe9fb08e585d9db8ddc21eefbbfcc368b04cf 100644
--- a/include/core/SkWriter32.h
+++ b/include/core/SkWriter32.h
@@ -73,17 +73,26 @@ public:
return (uint32_t*)(fData + offset);
}
- // Read or write 4 bytes at offset, which must be a multiple of 4 <= size().
- uint32_t read32At(size_t offset) {
+ /**
+ * Read a T record at offset, which must be a multiple of 4. Only legal if the record
+ * was writtern atomically using the write methods below.
+ */
+ template<typename T>
+ const T& readTAt(size_t offset) const {
SkASSERT(SkAlign4(offset) == offset);
SkASSERT(offset < fUsed);
- return *(uint32_t*)(fData + offset);
+ return *(T*)(fData + offset);
}
- void write32At(size_t offset, uint32_t val) {
+ /**
+ * Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record
+ * was writtern atomically using the write methods below.
+ */
+ template<typename T>
+ void overwriteTAt(size_t offset, const T& value) {
SkASSERT(SkAlign4(offset) == offset);
SkASSERT(offset < fUsed);
- *(uint32_t*)(fData + offset) = val;
+ *(T*)(fData + offset) = value;
}
bool writeBool(bool value) {
@@ -168,14 +177,11 @@ public:
* filled in with zeroes.
*/
uint32_t* reservePad(size_t size) {
- uint32_t* p = this->reserve(SkAlign4(size));
- uint8_t* tail = (uint8_t*)p + size;
- switch (SkAlign4(size) - size) {
- default: SkDEBUGFAIL("SkAlign4(x) - x should always be 0, 1, 2, or 3.");
- case 3: *tail++ = 0x00; // fallthrough is intentional
- case 2: *tail++ = 0x00; // fallthrough is intentional
- case 1: *tail++ = 0x00;
- case 0: ;/*nothing to do*/
+ size_t alignedSize = SkAlign4(size);
+ uint32_t* p = this->reserve(alignedSize);
+ if (alignedSize != size) {
+ SkASSERT(alignedSize >= 4);
+ p[alignedSize / 4 - 1] = 0;
}
return p;
}
« no previous file with comments | « no previous file | src/core/SkMatrixClipStateMgr.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698