Commit f9d1e423 authored by Milo Yip's avatar Milo Yip

Refactoring: remove GenericSchemaDocument::document_

parent 13ee68c9
...@@ -344,7 +344,7 @@ public: ...@@ -344,7 +344,7 @@ public:
typedef GenericValue<EncodingType, AllocatorType> SValue; typedef GenericValue<EncodingType, AllocatorType> SValue;
friend class GenericSchemaDocument<ValueType, AllocatorType>; friend class GenericSchemaDocument<ValueType, AllocatorType>;
Schema(SchemaDocumentType* document, const PointerType& p, const ValueType& value, AllocatorType* allocator) : Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) :
allocator_(allocator), allocator_(allocator),
enum_(), enum_(),
enumCount_(), enumCount_(),
...@@ -405,12 +405,12 @@ public: ...@@ -405,12 +405,12 @@ public:
} }
} }
AssignIfExist(allOf_, document, p, value, GetAllOfString()); AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document);
AssignIfExist(anyOf_, document, p, value, GetAnyOfString()); AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document);
AssignIfExist(oneOf_, document, p, value, GetOneOfString()); AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document);
if (const ValueType* v = GetMember(value, GetNotString())) { if (const ValueType* v = GetMember(value, GetNotString())) {
document->CreateSchema(&not_, p.Append(GetNotString(), allocator_), *v); schemaDocument->CreateSchema(&not_, p.Append(GetNotString(), allocator_), *v, document);
notValidatorIndex_ = validatorCount_; notValidatorIndex_ = validatorCount_;
validatorCount_++; validatorCount_++;
} }
...@@ -458,7 +458,7 @@ public: ...@@ -458,7 +458,7 @@ public:
for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) { for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) {
SizeType index; SizeType index;
if (FindPropertyIndex(itr->name, &index)) if (FindPropertyIndex(itr->name, &index))
document->CreateSchema(&properties_[index].schema, q.Append(itr->name, allocator_), itr->value); schemaDocument->CreateSchema(&properties_[index].schema, q.Append(itr->name, allocator_), itr->value, document);
} }
} }
...@@ -470,7 +470,7 @@ public: ...@@ -470,7 +470,7 @@ public:
for (ConstMemberIterator itr = v->MemberBegin(); itr != v->MemberEnd(); ++itr) { for (ConstMemberIterator itr = v->MemberBegin(); itr != v->MemberEnd(); ++itr) {
new (&patternProperties_[patternPropertyCount_]) PatternProperty(); new (&patternProperties_[patternPropertyCount_]) PatternProperty();
patternProperties_[patternPropertyCount_].pattern = CreatePattern(itr->name); patternProperties_[patternPropertyCount_].pattern = CreatePattern(itr->name);
document->CreateSchema(&patternProperties_[patternPropertyCount_].schema, q.Append(itr->name, allocator_), itr->value); schemaDocument->CreateSchema(&patternProperties_[patternPropertyCount_].schema, q.Append(itr->name, allocator_), itr->value, document);
patternPropertyCount_++; patternPropertyCount_++;
} }
} }
...@@ -502,7 +502,7 @@ public: ...@@ -502,7 +502,7 @@ public:
} }
else if (itr->value.IsObject()) { else if (itr->value.IsObject()) {
hasSchemaDependencies_ = true; hasSchemaDependencies_ = true;
document->CreateSchema(&properties_[sourceIndex].dependenciesSchema, q.Append(itr->name, allocator_), itr->value); schemaDocument->CreateSchema(&properties_[sourceIndex].dependenciesSchema, q.Append(itr->name, allocator_), itr->value, document);
properties_[sourceIndex].dependenciesValidatorIndex = validatorCount_; properties_[sourceIndex].dependenciesValidatorIndex = validatorCount_;
validatorCount_++; validatorCount_++;
} }
...@@ -514,7 +514,7 @@ public: ...@@ -514,7 +514,7 @@ public:
if (v->IsBool()) if (v->IsBool())
additionalProperties_ = v->GetBool(); additionalProperties_ = v->GetBool();
else if (v->IsObject()) else if (v->IsObject())
document->CreateSchema(&additionalPropertiesSchema_, p.Append(GetAdditionalPropertiesString(), allocator_), *v); schemaDocument->CreateSchema(&additionalPropertiesSchema_, p.Append(GetAdditionalPropertiesString(), allocator_), *v, document);
} }
AssignIfExist(minProperties_, value, GetMinPropertiesString()); AssignIfExist(minProperties_, value, GetMinPropertiesString());
...@@ -524,12 +524,12 @@ public: ...@@ -524,12 +524,12 @@ public:
if (const ValueType* v = GetMember(value, GetItemsString())) { if (const ValueType* v = GetMember(value, GetItemsString())) {
PointerType q = p.Append(GetItemsString(), allocator_); PointerType q = p.Append(GetItemsString(), allocator_);
if (v->IsObject()) // List validation if (v->IsObject()) // List validation
document->CreateSchema(&itemsList_, q, *v); schemaDocument->CreateSchema(&itemsList_, q, *v, document);
else if (v->IsArray()) { // Tuple validation else if (v->IsArray()) { // Tuple validation
itemsTuple_ = static_cast<const Schema**>(allocator_->Malloc(sizeof(const Schema*) * v->Size())); itemsTuple_ = static_cast<const Schema**>(allocator_->Malloc(sizeof(const Schema*) * v->Size()));
SizeType index = 0; SizeType index = 0;
for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr, index++) for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr, index++)
document->CreateSchema(&itemsTuple_[itemsTupleCount_++], q.Append(index, allocator_), *itr); schemaDocument->CreateSchema(&itemsTuple_[itemsTupleCount_++], q.Append(index, allocator_), *itr, document);
} }
} }
...@@ -540,7 +540,7 @@ public: ...@@ -540,7 +540,7 @@ public:
if (v->IsBool()) if (v->IsBool())
additionalItems_ = v->GetBool(); additionalItems_ = v->GetBool();
else if (v->IsObject()) else if (v->IsObject())
document->CreateSchema(&additionalItemsSchema_, p.Append(GetAdditionalItemsString(), allocator_), *v); schemaDocument->CreateSchema(&additionalItemsSchema_, p.Append(GetAdditionalItemsString(), allocator_), *v, document);
} }
AssignIfExist(uniqueItems_, value, GetUniqueItemsString()); AssignIfExist(uniqueItems_, value, GetUniqueItemsString());
...@@ -944,7 +944,7 @@ private: ...@@ -944,7 +944,7 @@ private:
}; };
static const SchemaType* GetTypeless() { static const SchemaType* GetTypeless() {
static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), 0); static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0);
return &typeless; return &typeless;
} }
...@@ -977,8 +977,8 @@ private: ...@@ -977,8 +977,8 @@ private:
out = static_cast<SizeType>(v->GetUint64()); out = static_cast<SizeType>(v->GetUint64());
} }
template <typename DocumentType, typename ValueType, typename PointerType> template <typename ValueType, typename PointerType>
void AssignIfExist(SchemaArray& out, const DocumentType& document, const PointerType& p, const ValueType& value, const ValueType& name) { void AssignIfExist(SchemaArray& out, SchemaDocumentType& schemaDocument, const PointerType& p, const ValueType& value, const ValueType& name, const ValueType& document) {
if (const ValueType* v = GetMember(value, name)) { if (const ValueType* v = GetMember(value, name)) {
if (v->IsArray() && v->Size() > 0) { if (v->IsArray() && v->Size() > 0) {
PointerType q = p.Append(name, allocator_); PointerType q = p.Append(name, allocator_);
...@@ -986,7 +986,7 @@ private: ...@@ -986,7 +986,7 @@ private:
out.schemas = static_cast<const Schema**>(allocator_->Malloc(out.count * sizeof(const Schema*))); out.schemas = static_cast<const Schema**>(allocator_->Malloc(out.count * sizeof(const Schema*)));
memset(out.schemas, 0, sizeof(Schema*)* out.count); memset(out.schemas, 0, sizeof(Schema*)* out.count);
for (SizeType i = 0; i < out.count; i++) for (SizeType i = 0; i < out.count; i++)
document->CreateSchema(&out.schemas[i], q.Append(i, allocator_), (*v)[i]); schemaDocument.CreateSchema(&out.schemas[i], q.Append(i, allocator_), (*v)[i], document);
out.begin = validatorCount_; out.begin = validatorCount_;
validatorCount_ += out.count; validatorCount_ += out.count;
} }
...@@ -1328,7 +1328,6 @@ public: ...@@ -1328,7 +1328,6 @@ public:
\param allocator An optional allocator instance for allocating memory. Can be null. \param allocator An optional allocator instance for allocating memory. Can be null.
*/ */
GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) :
document_(&document),
remoteProvider_(remoteProvider), remoteProvider_(remoteProvider),
allocator_(allocator), allocator_(allocator),
ownAllocator_(), ownAllocator_(),
...@@ -1341,7 +1340,7 @@ public: ...@@ -1341,7 +1340,7 @@ public:
// Generate root schema, it will call CreateSchema() to create sub-schemas, // Generate root schema, it will call CreateSchema() to create sub-schemas,
// And call AddRefSchema() if there are $ref. // And call AddRefSchema() if there are $ref.
CreateSchemaRecursive(&root_, PointerType(), static_cast<const ValueType&>(document)); CreateSchemaRecursive(&root_, PointerType(), document, document);
// Resolve $ref // Resolve $ref
while (!schemaRef_.Empty()) { while (!schemaRef_.Empty()) {
...@@ -1395,30 +1394,30 @@ private: ...@@ -1395,30 +1394,30 @@ private:
bool owned; bool owned;
}; };
void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v) { void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) {
if (schema) if (schema)
*schema = SchemaType::GetTypeless(); *schema = SchemaType::GetTypeless();
if (v.GetType() == kObjectType) { if (v.GetType() == kObjectType) {
const SchemaType* s = GetSchema(pointer); const SchemaType* s = GetSchema(pointer);
if (!s) if (!s)
CreateSchema(schema, pointer, v); CreateSchema(schema, pointer, v, document);
else if (schema) else if (schema)
*schema = s; *schema = s;
for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value); CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document);
} }
else if (v.GetType() == kArrayType) else if (v.GetType() == kArrayType)
for (SizeType i = 0; i < v.Size(); i++) for (SizeType i = 0; i < v.Size(); i++)
CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i]); CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document);
} }
void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v) { void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) {
RAPIDJSON_ASSERT(pointer.IsValid()); RAPIDJSON_ASSERT(pointer.IsValid());
if (v.IsObject()) { if (v.IsObject()) {
if (!HandleRefSchema(pointer, schema, v)) { if (!HandleRefSchema(pointer, schema, v, document)) {
SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, allocator_); SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_);
new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(pointer, s, true, allocator_); new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(pointer, s, true, allocator_);
if (schema) if (schema)
*schema = s; *schema = s;
...@@ -1426,7 +1425,7 @@ private: ...@@ -1426,7 +1425,7 @@ private:
} }
} }
bool HandleRefSchema(const PointerType& source, const SchemaType** schema, const ValueType& v) { bool HandleRefSchema(const PointerType& source, const SchemaType** schema, const ValueType& v, const ValueType& document) {
static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' }; static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' };
static const ValueType kRefValue(kRefString, 4); static const ValueType kRefValue(kRefString, 4);
...@@ -1459,8 +1458,8 @@ private: ...@@ -1459,8 +1458,8 @@ private:
else if (s[i] == '#') { // Local reference, defer resolution else if (s[i] == '#') { // Local reference, defer resolution
PointerType pointer(&s[i], len - i, allocator_); PointerType pointer(&s[i], len - i, allocator_);
if (pointer.IsValid()) { if (pointer.IsValid()) {
if (const ValueType* nv = pointer.Get(*document_)) if (const ValueType* nv = pointer.Get(document))
if (HandleRefSchema(source, schema, *nv)) if (HandleRefSchema(source, schema, *nv, document))
return true; return true;
new (schemaRef_.template Push<SchemaRefEntry>()) SchemaRefEntry(source, pointer, schema, allocator_); new (schemaRef_.template Push<SchemaRefEntry>()) SchemaRefEntry(source, pointer, schema, allocator_);
...@@ -1489,7 +1488,6 @@ private: ...@@ -1489,7 +1488,6 @@ private:
static const size_t kInitialSchemaMapSize = 64; static const size_t kInitialSchemaMapSize = 64;
static const size_t kInitialSchemaRefSize = 64; static const size_t kInitialSchemaRefSize = 64;
const ValueType* document_; //!< Only temporarily for constructor
IRemoteSchemaDocumentProviderType* remoteProvider_; IRemoteSchemaDocumentProviderType* remoteProvider_;
Allocator *allocator_; Allocator *allocator_;
Allocator *ownAllocator_; Allocator *ownAllocator_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment