diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2011-11-23 13:35:43 +0100 |
---|---|---|
committer | Knoll Lars <lars.knoll@nokia.com> | 2011-11-23 14:10:42 +0100 |
commit | 52e3c65a9eb1739f1bf833ba8969135a53d06fac (patch) | |
tree | 0e91c88d59f60283eba5b6de60495c584e745ab9 | |
parent | c7a4fd41a9443edd6057586cfeb8578e32d38c7a (diff) |
Code cleanups
Create a common base class for Object and Array,
since they contain the same values.
Move the resizing and table moving logic into
the common Base class.
This will in the next step simplify the implementation
of the Object.
Change-Id: I3c54e2572d6db72b271268eb867150809bf3da3f
Reviewed-by: Knoll Lars <lars.knoll@nokia.com>
-rw-r--r-- | src/qbinaryjson_p.h | 59 | ||||
-rw-r--r-- | src/qbinaryjsonarray.cpp | 32 |
2 files changed, 51 insertions, 40 deletions
diff --git a/src/qbinaryjson_p.h b/src/qbinaryjson_p.h index 05c19bd..a0365f6 100644 --- a/src/qbinaryjson_p.h +++ b/src/qbinaryjson_p.h @@ -117,19 +117,6 @@ struct Value }; -struct Array { - uint size; // in bytes - uint length; - uint unused; - offset index; // offset to index. usually follows the values - - inline offset *valueIndex() { return (offset *) (((char *) this) + index); } - inline Value *at(int i) { - return reinterpret_cast<Value *>(((char *)this) + valueIndex()[i]); - } -}; - - struct Entry { uint size; offset valueOffset; @@ -148,16 +135,52 @@ struct Entry { } }; -struct Object { +struct Base +{ uint size; uint length; uint unused; offset tableOffset; - // Entry list follows here + // content follows here + + inline offset *table() { return (offset *) (((char *) this) + tableOffset); } + + void reserveSpace(uint dataSize, int posInTable, uint numItems) + { + Q_ASSERT(posInTable >= 0 && posInTable <= length); + + offset off = tableOffset; + // move table to new position + memmove((char *)(table() + posInTable + numItems) + dataSize, table() + posInTable, (length - posInTable)*sizeof(offset)); + memmove((char *)(table()) + dataSize, table(), posInTable*sizeof(offset)); + tableOffset += dataSize; + for (int i = 0; i < numItems; ++i) + table()[posInTable + i] = off; + length += numItems; + size += dataSize + numItems * sizeof(offset); + } + + void removeItems(int pos, int numItems) + { + Q_ASSERT(pos >= 0 && pos <= length); + if (pos + numItems < length) + memmove(table() + pos, table() + pos + numItems, (length - pos - numItems)*sizeof(offset)); + length -= numItems; + } +}; + +struct Object : public Base +{ + Entry *entryAt(int i) { + return reinterpret_cast<Entry *>(((char *)this) + table()[i]); + } +}; - inline offset *entryTable() { return (offset *) (((char *) this) + tableOffset); } - Entry *entryAt(uint i) { - return reinterpret_cast<Entry *>(((char *)this) + entryTable()[i]); + +struct Array : public Base +{ + inline Value *at(int i) { + return reinterpret_cast<Value *>(((char *)this) + table()[i]); } }; diff --git a/src/qbinaryjsonarray.cpp b/src/qbinaryjsonarray.cpp index d1ed354..e3eb347 100644 --- a/src/qbinaryjsonarray.cpp +++ b/src/qbinaryjsonarray.cpp @@ -78,19 +78,14 @@ JsonValue JsonArray::last() const void JsonArray::append(const JsonValue &value) { Value *v = value.v; - uint sizeadd = v->size + sizeof(offset); - detach(sizeadd); // offset for the new index entry + detach(v->size + sizeof(offset)); // offset for the new index entry if (!a->length) - a->index = sizeof(Array); + a->tableOffset = sizeof(Array); - offset off = d->offsetOf(a->valueIndex()); - memmove(((char *)a->valueIndex()) + v->size, a->valueIndex(), a->length*sizeof(offset)); - memcpy(a->valueIndex(), v, v->size); - a->index += v->size; - a->valueIndex()[a->length] = off; - ++a->length; - a->size += sizeadd; + int pos = a->length; + a->reserveSpace(v->size, pos, 1); + memcpy(a->at(pos), v, v->size); } void JsonArray::removeAt(int i) @@ -99,9 +94,7 @@ void JsonArray::removeAt(int i) return; detach(); - memmove(a->valueIndex() + i, a->valueIndex() + i + 1, (a->length - i - 1)*sizeof(offset)); - --a->length; - + a->removeItems(i, 1); d->needsCompaction = true; } @@ -129,15 +122,10 @@ void JsonArray::insert(int i, const JsonValue &value) detach(v->size + sizeof(offset)); // offset for the new index entry if (!a->length) - a->index = sizeof(Array); + a->tableOffset = sizeof(Array); - offset off = d->offsetOf(a->valueIndex()); - memmove(((char *)a->valueIndex()) + v->size, a->valueIndex(), a->length*sizeof(offset)); - memcpy(a->valueIndex(), v, v->size); - a->index += v->size; - memmove(a->valueIndex() + i + 1, a->valueIndex() + i, (a->length - i - 1)*sizeof(offset)); - a->valueIndex()[i] = off; - ++a->length; + a->reserveSpace(v->size, i, 1); + memcpy(a->at(i), v, v->size); } //JsonValue &JsonArray::operator[](int i) @@ -169,7 +157,7 @@ void JsonArray::detach(uint reserve) a = (Array *)malloc(alloc); a->size = sizeof(Array); a->unused = 0; - a->index = sizeof(Array); + a->tableOffset = sizeof(Array); a->length = 0; d = new Data((char *)a, alloc); d->ref.ref(); |