diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2011-12-09 13:51:48 +0100 |
---|---|---|
committer | Hicks James <jamey.hicks@nokia.com> | 2011-12-09 16:00:44 +0100 |
commit | c11d21bb54cd277a34f394e603e57a8bd8438043 (patch) | |
tree | 490543ca57fbc2312386a9e2a47a3cce53bdfb17 | |
parent | ef7fd40ebc9efecd5686d588f98f172e96e2c288 (diff) |
Small cleanup
Remove a bit of duplicated code.
Change-Id: I8c540a64bea887793665e1a0bf99bc88f91b7019
Reviewed-by: Hicks James <jamey.hicks@nokia.com>
-rw-r--r-- | src/qjson_p.h | 16 | ||||
-rw-r--r-- | src/qjsonvalue.cpp | 20 | ||||
-rw-r--r-- | src/qjsonvalue.h | 3 | ||||
-rw-r--r-- | src/qjsonwriter.cpp | 4 |
4 files changed, 14 insertions, 29 deletions
diff --git a/src/qjson_p.h b/src/qjson_p.h index 31c24bf..2b8757d 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -322,8 +322,7 @@ struct Value QString toString(const Base *b) const; String asString(const Base *b) const; Latin1String asLatin1String(const Base *b) const; - Array *array(const Base *b) const; - Object *object(const Base *b) const; + Base *objectOrArray(const Base *b) const; inline void setNull() { val = 0; @@ -485,19 +484,12 @@ inline Latin1String Value::asLatin1String(const Base *b) const return Latin1String(c, l); } -inline Array *Value::array(const Base *b) const +inline Base *Value::objectOrArray(const Base *b) const { - Q_ASSERT(type == ArrayValue); - return reinterpret_cast<Array *>(data(b)); + Q_ASSERT(type == ArrayValue || type == ObjectValue); + return reinterpret_cast<Base *>(data(b)); } -inline Object *Value::object(const Base *b) const -{ - Q_ASSERT(type == ObjectValue); - return reinterpret_cast<Object *>(data(b)); -} - - struct Data { inline Data(char *raw, int a) : alloc(a), needsCompaction(false), rawData(raw) diff --git a/src/qjsonvalue.cpp b/src/qjsonvalue.cpp index 7a8a045..d7a3f70 100644 --- a/src/qjsonvalue.cpp +++ b/src/qjsonvalue.cpp @@ -40,12 +40,9 @@ JsonValue::JsonValue(Data *data, Base *base, const Value &v) break; } case ArrayValue: - d = data; - array = v.array(base); - break; case ObjectValue: d = data; - object = v.object(base); + this->base = v.objectOrArray(base); break; } if (d) @@ -136,9 +133,7 @@ JsonValue &JsonValue::operator =(const JsonValue &other) if (d && !d->ref.deref()) delete d; - t = other.t; d = other.d; - dbl = other.dbl; if (d) d->ref.ref(); @@ -342,9 +337,8 @@ int JsonValue::requiredStorage(bool *compressed) const return qStringSize(s, *compressed); } case ArrayValue: - return array ? array->size : sizeof(Array); case ObjectValue: - return object ? object->size : sizeof(Object); + return base ? base->size : sizeof(Base); case UndefinedValue: case NullValue: case BooleanValue: @@ -386,13 +380,11 @@ void JsonValue::copyData(char *dest, bool compressed) const copyString(dest, str, compressed); break; } - case ArrayValue:{ - const Base *b = object ? object : &emptyObject; - memcpy(dest, b, b->size); - break; - } + case ArrayValue: case ObjectValue: { - const Base *b = array ? array : &emptyArray; + const Base *b = base; + if (!b) + b = (t == ArrayValue ? &emptyArray : &emptyObject); memcpy(dest, b, b->size); break; } diff --git a/src/qjsonvalue.h b/src/qjsonvalue.h index 6ace201..ff528ae 100644 --- a/src/qjsonvalue.h +++ b/src/qjsonvalue.h @@ -62,12 +62,13 @@ private: void copyData(char *dest, bool compressed) const; ValueType t; - Data *d; // needed for Object and Array + Data *d; // needed for Objects and Arrays union { char raw[sizeof(double)]; bool b; double dbl; QStringData *stringData; + Base *base; Object *object; Array *array; }; diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp index ce162ba..f4df29e 100644 --- a/src/qjsonwriter.cpp +++ b/src/qjsonwriter.cpp @@ -151,13 +151,13 @@ static void valueToJson(const Base *b, const Value &v, QByteArray &json, int ind break; case ArrayValue: json += "[\n"; - arrayContentToJson(v.array(b), json, indent + 1); + arrayContentToJson(static_cast<Array *>(v.objectOrArray(b)), json, indent + 1); json += QByteArray(4*indent, ' '); json += "]"; break; case ObjectValue: json += "{\n"; - objectContentToJson(v.object(b), json, indent + 1); + objectContentToJson(static_cast<Object *>(v.objectOrArray(b)), json, indent + 1); json += QByteArray(4*indent, ' '); json += "}"; break; |