[apps/shared/expression_model_store] Add context parameter to model testing

This commit is contained in:
Ruben Dashyan
2019-09-10 11:50:18 +02:00
committed by LeaNumworks
parent 893131ed08
commit 56a3fcfacb
3 changed files with 23 additions and 15 deletions

View File

@@ -40,7 +40,7 @@ void ExpressionModelStore::tidy() {
resetMemoizedModelsExceptRecord();
}
int ExpressionModelStore::numberOfModelsSatisfyingTest(ModelTest test) const {
int ExpressionModelStore::numberOfModelsSatisfyingTest(ModelTest test, void * context) const {
int count = 0;
int index = 0;
Ion::Storage::Record record;
@@ -49,15 +49,15 @@ int ExpressionModelStore::numberOfModelsSatisfyingTest(ModelTest test) const {
if (record.isNull()) {
break;
}
if (test(privateModelForRecord(record))) {
if (test(privateModelForRecord(record), context)) {
count++;
}
} while (true);
return count;
}
Ion::Storage::Record ExpressionModelStore::recordSatisfyingTestAtIndex(int i, ModelTest test) const {
assert(0 <= i && i < numberOfModelsSatisfyingTest(test));
Ion::Storage::Record ExpressionModelStore::recordSatisfyingTestAtIndex(int i, ModelTest test, void * context) const {
assert(0 <= i && i < numberOfModelsSatisfyingTest(test, context));
int count = 0;
int index = 0;
Ion::Storage::Record record;
@@ -67,7 +67,7 @@ Ion::Storage::Record ExpressionModelStore::recordSatisfyingTestAtIndex(int i, Mo
assert(false);
break;
}
if (test(privateModelForRecord(record))) {
if (test(privateModelForRecord(record), context)) {
if (i == count) {
break;
}

View File

@@ -19,9 +19,13 @@ public:
// By default, the number of models is not bounded
virtual int maxNumberOfModels() const { return -1; }
int numberOfModels() const;
int numberOfDefinedModels() const { return numberOfModelsSatisfyingTest(&isModelDefined); }
Ion::Storage::Record recordAtIndex(int i) const;
Ion::Storage::Record definedRecordAtIndex(int i) const { return recordSatisfyingTestAtIndex(i, &isModelDefined); }
int numberOfDefinedModels() const {
return numberOfModelsSatisfyingTest(&isModelDefined, nullptr);
}
Ion::Storage::Record definedRecordAtIndex(int i) const {
return recordSatisfyingTestAtIndex(i, &isModelDefined, nullptr);
}
ExpiringPointer<ExpressionModelHandle> modelForRecord(Ion::Storage::Record record) const { return ExpiringPointer<ExpressionModelHandle>(privateModelForRecord(record)); }
// Add and Remove
@@ -35,10 +39,10 @@ public:
protected:
constexpr static int k_maxNumberOfMemoizedModels = 10;
int maxNumberOfMemoizedModels() const { return maxNumberOfModels() < 0 ? k_maxNumberOfMemoizedModels : maxNumberOfModels(); }
typedef bool (*ModelTest)(ExpressionModelHandle * model);
int numberOfModelsSatisfyingTest(ModelTest test) const;
Ion::Storage::Record recordSatisfyingTestAtIndex(int i, ModelTest test) const;
static bool isModelDefined(ExpressionModelHandle * model) {
typedef bool (*ModelTest)(ExpressionModelHandle * model, void * context);
int numberOfModelsSatisfyingTest(ModelTest test, void * context) const;
Ion::Storage::Record recordSatisfyingTestAtIndex(int i, ModelTest test, void * context) const;
static bool isModelDefined(ExpressionModelHandle * model, void * context) {
return model->isDefined();
}
ExpressionModelHandle * privateModelForRecord(Ion::Storage::Record record) const;

View File

@@ -13,13 +13,17 @@ class FunctionStore : public ExpressionModelStore {
public:
FunctionStore() : ExpressionModelStore() {}
uint32_t storeChecksum();
int numberOfActiveFunctions() const { return numberOfModelsSatisfyingTest(&isFunctionActive); }
Ion::Storage::Record activeRecordAtIndex(int i) const { return recordSatisfyingTestAtIndex(i, &isFunctionActive); }
int numberOfActiveFunctions() const {
return numberOfModelsSatisfyingTest(&isFunctionActive, nullptr);
}
Ion::Storage::Record activeRecordAtIndex(int i) const {
return recordSatisfyingTestAtIndex(i, &isFunctionActive, nullptr);
}
ExpiringPointer<Function> modelForRecord(Ion::Storage::Record record) const { return ExpiringPointer<Function>(static_cast<Function *>(privateModelForRecord(record))); }
protected:
static bool isFunctionActive(ExpressionModelHandle * model) {
static bool isFunctionActive(ExpressionModelHandle * model, void * context) {
// An active function must be defined
return isModelDefined(model) && static_cast<Function *>(model)->isActive();
return isModelDefined(model, context) && static_cast<Function *>(model)->isActive();
}
};