Update NotConstraint to use custom allocator, with better encapsulation

This commit is contained in:
Tristan Penman 2015-12-31 07:44:30 +11:00
parent 05ba5bf711
commit 99a5c2f604
4 changed files with 46 additions and 20 deletions

View File

@ -421,17 +421,30 @@ struct MultipleOfConstraint: BasicConstraint<MultipleOfConstraint>
};
/**
* @brief Represents a 'not' constraint.
* @brief Represents a 'not' constraint
*/
struct NotConstraint: BasicConstraint<NotConstraint>
class NotConstraint: public BasicConstraint<NotConstraint>
{
NotConstraint(const Subschema *schema)
: schema(schema) { }
public:
NotConstraint()
: subschema(NULL) { }
NotConstraint(const NotConstraint &other)
: schema(other.schema) { }
NotConstraint(CustomAlloc allocFn, CustomFree freeFn)
: BasicConstraint(allocFn, freeFn),
subschema(NULL) { }
const Subschema *schema;
const Subschema * getSubschema() const
{
return subschema;
}
void setSubschema(const Subschema *subschema)
{
this->subschema = subschema;
}
private:
const Subschema *subschema;
};
/**

View File

@ -16,15 +16,14 @@ struct MinItemsConstraint;
struct MinLengthConstraint;
struct MinPropertiesConstraint;
struct MultipleOfConstraint;
struct NotConstraint;
struct PatternConstraint;
struct PropertiesConstraint;
class AllOfConstraint;
class AnyOfConstraint;
class DependenciesConstraint;
class LinearItemsConstraint;
class NotConstraint;
class OneOfConstraint;
class RequiredConstraint;
class SingularItemsConstraint;

View File

@ -1153,10 +1153,13 @@ private:
typename FetchDocumentFunction<AdapterType>::Type > fetchDoc)
{
if (node.maybeObject()) {
const Subschema *childSubschema = rootSchema.createSubschema();
populateSchema<AdapterType>(rootSchema, rootNode, node,
*childSubschema, currentScope, nodePath, fetchDoc);
return constraints::NotConstraint(childSubschema);
const Subschema *subschema = rootSchema.createSubschema();
constraints::NotConstraint constraint;
constraint.setSubschema(subschema);
populateSchema<AdapterType>(rootSchema, rootNode, node, *subschema,
currentScope, nodePath, fetchDoc);
return constraint;
}
throw std::runtime_error("Expected object value for 'not' constraint.");

View File

@ -683,21 +683,32 @@ public:
}
/**
* @brief Validate against the not constraint represented by a
* NotConstraint object.
* @brief Validate a value against a NotConstraint
*
* @param constraint Constraint that the target must validate against.
* If the subschema NotConstraint currently holds a NULL pointer, the
* schema will be treated like the empty schema. Therefore validation
* will always fail.
*
* @return true if the constraint is satisfied, false otherwise.
* @param constraint Constraint that the target must validate against
*
* @return \c true if the constraint is satisfied; \c false otherwise
*/
virtual bool visit(const NotConstraint &constraint)
{
ValidationVisitor<AdapterType> v(target, context, strictTypes, NULL);
const Subschema *subschema = constraint.getSubschema();
if (!subschema) {
// Treat NULL pointer like empty schema
return false;
}
if (v.validateSchema(*constraint.schema)) {
ValidationVisitor<AdapterType> v(target, context, strictTypes, NULL);
if (v.validateSchema(*subschema)) {
if (results) {
results->pushError(context, "Target should not validate against schema specified in 'not' constraint.");
results->pushError(context,
"Target should not validate against schema "
"specified in 'not' constraint.");
}
return false;
}