[apps] GlobalCtxt::SetExpForFunctionRecord used in StorageCartesianFunction

This commit is contained in:
Léa Saviot
2018-10-10 10:49:00 +02:00
committed by Émilie Feral
parent 1cd5145d3f
commit 5a9c0b1b84
5 changed files with 25 additions and 35 deletions

View File

@@ -119,28 +119,26 @@ Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForActualSymbol(co
return Ion::Storage::sharedStorage()->createRecordWithExtension(symbol.name(), expExtension, expression.addressInPool(), expression.size());
}
Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForFunction(const Expression & expressionToStore, const SymbolAbstract & symbol, Ion::Storage::Record previousRecord) {
size_t expressionToStoreSize = expressionToStore.isUninitialized() ? 0 : expressionToStore.size();
size_t newDataSize = sizeof(StorageCartesianFunction::CartesianFunctionRecordData) + expressionToStoreSize;
Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForFunctionRecord(Expression expressionToStore, const char * baseName, Ion::Storage::Record previousRecord) {
Ion::Storage::Record recordToSet = previousRecord;
Ion::Storage::Record::ErrorStatus error = Ion::Storage::Record::ErrorStatus::None;
if (Ion::Storage::FullNameHasExtension(previousRecord.fullName(), funcExtension, strlen(funcExtension))) {
// The previous record was also a function: we want to keep its metadata
Ion::Storage::Record::Data newData = previousRecord.value();
newData.size = newDataSize;
error = previousRecord.setValue(newData);
} else {
if (!Ion::Storage::FullNameHasExtension(previousRecord.fullName(), funcExtension, strlen(funcExtension))) {
// The previous record was not a function. Destroy it and create the new record.
previousRecord.destroy();
StorageCartesianFunction::CartesianFunctionRecordData newData;
error = Ion::Storage::sharedStorage()->createRecordWithExtension(symbol.name(), funcExtension, &newData, newDataSize);
}
if (error == Ion::Storage::Record::ErrorStatus::None && !expressionToStore.isUninitialized()) {
void * newDataExpressionAddress = StorageCartesianFunction(Ion::Storage::sharedStorage()->recordBaseNamedWithExtension(symbol.name(), funcExtension)).expressionAddress();
memcpy(newDataExpressionAddress, expressionToStore.addressInPool(), expressionToStore.size());
StorageCartesianFunction newModel = StorageCartesianFunction::NewModel(&error);
if (error == Ion::Storage::Record::ErrorStatus::None) {
return error;
}
recordToSet = newModel.record();
}
error = StorageCartesianFunction(recordToSet).setExpressionContent(expressionToStore);
return error;
}
Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForFunction(const Expression & expressionToStore, const SymbolAbstract & symbol, Ion::Storage::Record previousRecord) {
return SetExpressionForFunctionRecord(expressionToStore, symbol.name(), previousRecord);
}
Ion::Storage::Record GlobalContext::RecordWithBaseName(const char * name) {
const char * extensions[2] = {expExtension, funcExtension/*, seqExtension*/};
return Ion::Storage::sharedStorage()->recordBaseNamedWithExtensions(name, extensions, 2);

View File

@@ -28,6 +28,9 @@ public:
static Poincare::Expression ExpressionFromSymbolRecord(Ion::Storage::Record record);
static Poincare::Expression ExpressionFromFunctionRecord(Ion::Storage::Record record);
// Set expression for record
static Ion::Storage::Record::ErrorStatus SetExpressionForFunctionRecord(Poincare::Expression e, Ion::Storage::Record record);
/* Expression for symbol
* The expression recorded in global context is already an expression.
* Otherwise, we would need the context and the angle unit to evaluate it */

View File

@@ -32,12 +32,14 @@ void StorageCartesianFunction::DefaultName(char buffer[], size_t bufferSize) {
strlcpy(&buffer[dotCharIndex+1], GlobalContext::funcExtension, bufferSize - (dotCharIndex+1));
}
StorageCartesianFunction StorageCartesianFunction::NewModel() {
StorageCartesianFunction StorageCartesianFunction::NewModel(Ion::Storage::Record::ErrorStatus & error) {
char nameBuffer[100];
DefaultName(nameBuffer, 100);
CartesianFunctionRecordData data;
Ion::Storage::Record::ErrorStatus r = Ion::Storage::sharedStorage()->createRecordWithFullName(nameBuffer, &data, sizeof(data));
assert(r == Ion::Storage::Record::ErrorStatus::None); // TODO not a valid assertion!
*error = Ion::Storage::sharedStorage()->createRecordWithFullName(nameBuffer, &data, sizeof(data));
if (*error != Ion::Storage::Record::ErrorStatus::None()) {
return StorageCartesianFunction();
}
return StorageCartesianFunction(Ion::Storage::sharedStorage()->recordNamed(nameBuffer));
}
@@ -82,23 +84,10 @@ Expression::Coordinate2D StorageCartesianFunction::nextIntersectionFrom(double s
Expression reducedExp = reducedExpression(context);
return reducedExp.nextIntersection(symbol(), start, step, max, *context, Preferences::sharedPreferences()->angleUnit(), reducedExp);
}
void StorageCartesianFunction::setContent(const char * c) {
// Compute the expression to store
Expression expressionToStore = StorageExpressionModel::expressionToStoreFromString(c);
// Prepare the new data to store
Ion::Storage::Record::Data newData = record().value();
size_t expressionToStoreSize = expressionToStore.isUninitialized() ? 0 : expressionToStore.size();
newData.size = sizeof(CartesianFunctionRecordData) + expressionToStoreSize;
// Set the data
Ion::Storage::Record::ErrorStatus error = record().setValue(newData);
assert(error == Ion::Storage::Record::ErrorStatus::None); //TODO remove assertion and handle case
// Copy the expression if needed
if (!expressionToStore.isUninitialized()) {
memcpy(expressionAddress(),expressionToStore.addressInPool(), expressionToStore.size());
}
Ion::Storage::Record::ErrorStatus error = GlobalContext::SetExpressionForFunctionRecord(expressionToStore, record());
StorageExpressionModel::didSetContentData();
}

View File

@@ -11,7 +11,7 @@ class StorageCartesianFunction : public StorageFunction {
public:
static const char * Extension() { return GlobalContext::funcExtension; }
static void DefaultName(char buffer[], size_t bufferSize);
static StorageCartesianFunction NewModel();
static StorageCartesianFunction NewModel(Ion::Storage::Record::ErrorStatus & error);
StorageCartesianFunction(const char * text = nullptr) :
StorageFunction(text)
{}

View File

@@ -43,7 +43,7 @@ Expression Expression::parse(char const * string) {
return expression;
}
const Expression Expression::ExpressionFromAddress(const void * address, size_t size) {
Expression Expression::ExpressionFromAddress(const void * address, size_t size) {
if (address == nullptr || size == 0) {
return Expression();
}