Index: include/core/SkWriter32.h |
diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h |
index b193cbe7c407663f7a82a613b8a65b5e1e637a47..cb541749dbacded9ef80bc2279dcb3705172aa9d 100644 |
--- a/include/core/SkWriter32.h |
+++ b/include/core/SkWriter32.h |
@@ -79,9 +79,17 @@ public: |
return p; |
} |
- // Read or write 4 bytes at offset, which must be a multiple of 4 <= size(). |
- uint32_t read32At(size_t offset) { return this->atOffset(offset); } |
- void write32At(size_t offset, uint32_t val) { this->atOffset(offset) = val; } |
+ // Read/overwrite a record at offset, which must be a multiple of 4. |
+ // These calls are valid only when the record was written atomically |
+ // using the write* methods below. |
+ template<typename T> |
+ const T& readTAt(size_t offset) const { |
+ return this->atOffset<T>(offset); |
+ } |
+ template<typename T> |
+ void overwriteTAt(const T& value, size_t offset) { |
mtklein
2014/02/10 22:37:34
I have a slight preference for it to go back the o
f(malita)
2014/02/11 03:30:56
Seemed more natural to follow the name hints ("ove
|
+ this->atOffset<T>(offset) = value; |
+ } |
bool writeBool(bool value) { |
this->write32(value); |
@@ -238,18 +246,20 @@ public: |
} |
private: |
- uint32_t& atOffset(size_t offset) { |
+ template<typename T> |
+ T& atOffset(size_t offset) const { |
SkASSERT(SkAlign4(offset) == offset); |
- const int count = SkToInt(offset/4); |
- SkASSERT(count < fCount); |
+ const int count = SkToInt(offset / 4); |
+ SkASSERT(offset + sizeof(T) <= (unsigned)fCount * 4); |
if (count < this->externalCount()) { |
- return fExternal[count]; |
+ // A record should not span fExternal->fInternal. |
+ SkASSERT(offset + sizeof(T) <= (unsigned)this->externalCount() * 4); |
+ return *(T*)&fExternal[count]; |
} |
- return fInternal[count - this->externalCount()]; |
+ return *(T*)&fInternal[count - this->externalCount()]; |
} |
- |
// Number of uint32_t written into fExternal. <= fExternalLimit. |
int externalCount() const { return fCount - fInternal.count(); } |