diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-04 12:04:29 +0100 |
---|---|---|
committer | Jamey Hicks <jamey.hicks@nokia.com> | 2012-01-04 14:52:59 +0100 |
commit | 66e156b99086c496319d0521910c68096879cef0 (patch) | |
tree | ab7e1e5d3f182baa72a94361f50e1f96795e61d8 | |
parent | 6a6c9eeb4026eae42f306e4f39943a20e83d3e9b (diff) |
De-inline some code
These larger methods probably don't make sense to be
inline, and where cluttering the private header.
Change-Id: I863cf5d216b1e5c85cb975663985cb248480286f
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
-rw-r--r-- | src/qjson.cpp | 74 | ||||
-rw-r--r-- | src/qjson_p.h | 119 | ||||
-rw-r--r-- | src/qjsonvalue.cpp | 2 | ||||
-rw-r--r-- | src/qjsonwriter.cpp | 2 |
4 files changed, 84 insertions, 113 deletions
diff --git a/src/qjson.cpp b/src/qjson.cpp index 06aaad1..8261703 100644 --- a/src/qjson.cpp +++ b/src/qjson.cpp @@ -134,6 +134,49 @@ void Data::validate() valid = res ? Validated : Invalid; } + +int Base::reserveSpace(uint dataSize, int posInTable, uint numItems) +{ + Q_ASSERT(posInTable >= 0 && posInTable <= (int)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 < (int)numItems; ++i) + table()[posInTable + i] = off; + length += numItems; + size += dataSize + numItems * sizeof(offset); + return off; +} + +void Base::removeItems(int pos, int numItems) +{ + Q_ASSERT(pos >= 0 && pos <= (int)length); + if (pos + numItems < (int)length) + memmove(table() + pos, table() + pos + numItems, (length - pos - numItems)*sizeof(offset)); + length -= numItems; +} + + + +int Object::indexOf(const QString &key) +{ + for (int i = 0; i < (int)length; ++i) { + Entry *e = entryAt(i); + if (e->value.latinKey) { + if (e->shallowLatin1Key() == key) + return i; + } else { + if (e->shallowKey() == key) + return i; + } + } + return -1; +} + + bool Object::isValid() const { if (tableOffset + length*sizeof(offset) > size) @@ -154,6 +197,7 @@ bool Object::isValid() const } + bool Array::isValid() const { if (tableOffset + length*sizeof(offset) > size) @@ -166,6 +210,36 @@ bool Array::isValid() const return true; } + +int Value::usedStorage(const Base *b) const +{ + int s = 0; + switch (type) { + case NumberValue: + if (latinOrIntValue) + break; + s = sizeof(double); + break; + case StringValue: { + char *d = data(b); + if (latinOrIntValue) + s = sizeof(ushort) + *(ushort *)d; + else + s = sizeof(int) + sizeof(ushort)*(*(int *)d); + break; + } + case ArrayValue: + case ObjectValue: + s = objectOrArray(b)->size; + break; + case NullValue: + case BooleanValue: + default: + break; + } + return alignedSize(s); +} + bool Value::isValid(const Base *b) const { int offset = 0; diff --git a/src/qjson_p.h b/src/qjson_p.h index 7f773f2..f0aa5a3 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -306,34 +306,13 @@ struct Base offset tableOffset; // content follows here - bool isObject() const { return is_object; } - bool isArray() const { return !is_object; } + inline bool isObject() const { return is_object; } + inline bool isArray() const { return !is_object; } inline offset *table() const { return (offset *) (((char *) this) + tableOffset); } - int reserveSpace(uint dataSize, int posInTable, uint numItems) - { - Q_ASSERT(posInTable >= 0 && posInTable <= (int)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 < (int)numItems; ++i) - table()[posInTable + i] = off; - length += numItems; - size += dataSize + numItems * sizeof(offset); - return off; - } - - void removeItems(int pos, int numItems) - { - Q_ASSERT(pos >= 0 && pos <= (int)length); - if (pos + numItems < (int)length) - memmove(table() + pos, table() + pos + numItems, (length - pos - numItems)*sizeof(offset)); - length -= numItems; - } + int reserveSpace(uint dataSize, int posInTable, uint numItems); + void removeItems(int pos, int numItems); }; struct Object : public Base @@ -342,7 +321,7 @@ struct Object : public Base Entry *entryAt(int i) const { return reinterpret_cast<Entry *>(((char *)this) + table()[i]); } - inline int indexOf(const QString &key); + int indexOf(const QString &key); bool isValid() const; }; @@ -376,7 +355,7 @@ struct Value inline char *data(const Base *b) const { return ((char *)b) + val; } - inline int usedStorage(const Base *b) const; + int usedStorage(const Base *b) const; bool toBoolean() const; double toNumber(const Base *b) const; @@ -387,44 +366,6 @@ struct Value Base *objectOrArray(const Base *b) const; bool isValid(const Base *b) const; - - inline void setNull() { - val = 0; - latinOrIntValue = 0; - latinKey = false; - type = NullValue; - } - - inline void setBool(bool b) { - val = b; - latinOrIntValue = false; - latinKey = false; - type = BooleanValue; - } - - inline void setInt(int i) { - Q_ASSERT(i < 1e27 && i > -1e27); - int_val = i; - latinOrIntValue = true; - latinKey = false; - type = NumberValue; - } - - inline void setString(Base *b, offset off, const QString &str) { - val = off; - latinOrIntValue = false; - latinKey = false; - type = StringValue; - memcpy(((char *)b) + off, str.constData(), str.length()*sizeof(ushort)); - } - - inline void setObject(Base *b, offset off, const Object *o) { - val = off; - latinOrIntValue = false; - latinKey = false; - type = StringValue; - memcpy(((char *)b) + off, o, o->size); - } }; inline Value Array::at(int i) const @@ -482,21 +423,6 @@ struct Entry { } }; -inline int Object::indexOf(const QString &key) -{ - for (int i = 0; i < (int)length; ++i) { - Entry *e = entryAt(i); - if (e->value.latinKey) { - if (e->shallowLatin1Key() == key) - return i; - } else { - if (e->shallowKey() == key) - return i; - } - } - return -1; -} - struct Header { uint tag; // 'qbjs' uint version; // 1 @@ -504,36 +430,6 @@ struct Header { }; -inline int Value::usedStorage(const Base *b) const -{ - int s = 0; - switch (type) { - case NumberValue: - if (latinOrIntValue) - break; - s = sizeof(double); - break; - case StringValue: { - char *d = data(b); - if (latinOrIntValue) - s = sizeof(ushort) + *(ushort *)d; - else - s = sizeof(int) + sizeof(ushort)*(*(int *)d); - break; - } - case ArrayValue: - case ObjectValue: - s = objectOrArray(b)->size; - break; - case NullValue: - case BooleanValue: - default: - break; - } - return alignedSize(s); -} - - inline bool Value::toBoolean() const { Q_ASSERT(type == BooleanValue); @@ -653,7 +549,8 @@ struct Data { return JsonArray(const_cast<Data *>(this), a); } - Data *detach(Base *b, int reserve = 0) { + Data *detach(Base *b, int reserve = 0) + { int size = sizeof(Header) + b->size + reserve; char *raw = (char *)malloc(size); memcpy(raw + sizeof(Header), b, b->size); diff --git a/src/qjsonvalue.cpp b/src/qjsonvalue.cpp index 3277997..d92b567 100644 --- a/src/qjsonvalue.cpp +++ b/src/qjsonvalue.cpp @@ -69,7 +69,7 @@ JsonValue::JsonValue(Data *data, Base *base, const Value &v) dbl = 0; break; case BooleanValue: - b = v.val; + b = v.toBoolean(); break; case NumberValue: dbl = v.toNumber(base); diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp index 8c67813..3959021 100644 --- a/src/qjsonwriter.cpp +++ b/src/qjsonwriter.cpp @@ -180,7 +180,7 @@ static void valueToJson(const Base *b, const Value &v, QByteArray &json, int ind ValueType type = (ValueType)v.type; switch (type) { case BooleanValue: - json += v.val ? "true" : "false"; + json += v.toBoolean() ? "true" : "false"; break; case NumberValue: json += QByteArray::number(v.toNumber(b)); |