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

Unified Diff: include/core/SkWriter32.h

Issue 130913018: Templetized SkWriter32 readTAt() & overwriteTAt() (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Win build fix 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/SkPictureRecord.h » ('j') | src/core/SkPictureRecord.h » ('J')
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 b193cbe7c407663f7a82a613b8a65b5e1e637a47..902c2335fcd733a34f378dcfbb2de77e24e7d325 100644
--- a/include/core/SkWriter32.h
+++ b/include/core/SkWriter32.h
@@ -79,9 +79,23 @@ 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 or (over)write a record at offset, which must be a multiple of 4.
+ // These calls are valid only if the same record type has been previously written
+ // atomically at the given offset.
mtklein 2014/02/06 21:38:16 Now that we're looking at it again, it might make
mtklein 2014/02/06 21:38:16 This atomically isn't necessarily congruent to any
f(malita) 2014/02/10 16:10:35 Sounds good.
f(malita) 2014/02/10 16:10:35 Good catch - I hadn't realized some of the writeXY
+ template<typename T>
+ inline const T& readTAt(size_t offset) const {
+ return this->atOffset<T>(offset);
+ }
+
+ template<typename T>
+ inline void writeTAt(size_t offset, T value) {
+ this->atOffset<T>(offset) = value;
+ }
+
+ template<typename T>
+ inline void writeT(const T& value) {
mtklein 2014/02/06 21:38:16 My only reservation about doing this is that it al
f(malita) 2014/02/10 16:10:35 Ah, nice - overloading is not shun upon in Skia. I
+ *(T*)this->reserve(sizeof(value)) = value;
+ }
bool writeBool(bool value) {
this->write32(value);
@@ -104,26 +118,6 @@ public:
*(int32_t*)this->reserve(sizeof(value)) = value;
}
- void writePtr(void* value) {
- *(void**)this->reserve(sizeof(value)) = value;
- }
-
- void writeScalar(SkScalar value) {
- *(SkScalar*)this->reserve(sizeof(value)) = value;
- }
-
- void writePoint(const SkPoint& pt) {
- *(SkPoint*)this->reserve(sizeof(pt)) = pt;
- }
-
- void writeRect(const SkRect& rect) {
- *(SkRect*)this->reserve(sizeof(rect)) = rect;
- }
-
- void writeIRect(const SkIRect& rect) {
- *(SkIRect*)this->reserve(sizeof(rect)) = rect;
- }
-
void writeRRect(const SkRRect& rrect) {
rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
}
@@ -238,18 +232,20 @@ public:
}
private:
- uint32_t& atOffset(size_t offset) {
+ template<typename T>
+ inline 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 << 2);
mtklein 2014/02/06 21:38:16 * 4 would be a bit more consistent with the rest o
f(malita) 2014/02/10 16:10:35 Will do.
if (count < this->externalCount()) {
- return fExternal[count];
+ // A record should not span fExternal->fInternal.
+ SkASSERT(offset + sizeof(T) <= (unsigned)this->externalCount() << 2);
+ 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(); }
« no previous file with comments | « no previous file | src/core/SkPictureRecord.h » ('j') | src/core/SkPictureRecord.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698