From cff3d56f2277123d7b7edadc7da3e0f846d81482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 28 Feb 2019 16:43:15 +0100 Subject: [PATCH] [sequence] SequenceStore returns Sequence * instead of ExpiringPointer SequenceStore keep all its Sequences in an array which fix Sequence addresses --- apps/sequence/graph/graph_controller.cpp | 6 ++--- apps/sequence/graph/graph_view.cpp | 2 +- apps/sequence/graph/term_sum_controller.cpp | 2 +- apps/sequence/list/list_controller.cpp | 22 +++++++++---------- .../sequence/list/list_parameter_controller.h | 2 +- .../list/type_parameter_controller.cpp | 2 +- .../sequence/list/type_parameter_controller.h | 2 +- apps/sequence/sequence_context.cpp | 18 +++++++-------- apps/sequence/sequence_store.h | 5 ++++- apps/sequence/values/values_controller.cpp | 2 +- apps/shared/expiring_pointer.h | 8 +------ 11 files changed, 34 insertions(+), 37 deletions(-) diff --git a/apps/sequence/graph/graph_controller.cpp b/apps/sequence/graph/graph_controller.cpp index 1ba55c2b7..6e33b612f 100644 --- a/apps/sequence/graph/graph_controller.cpp +++ b/apps/sequence/graph/graph_controller.cpp @@ -32,7 +32,7 @@ float GraphController::interestingXMin() const { int nmin = INT_MAX; int nbOfActiveModels = functionStore()->numberOfActiveFunctions(); for (int i = 0; i < nbOfActiveModels; i++) { - ExpiringPointer s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i)); + Sequence * s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i)); nmin = minInt(nmin, s->initialRank()); } assert(nmin < INT_MAX); @@ -45,7 +45,7 @@ float GraphController::interestingXHalfRange() const { int nmax = 0; int nbOfActiveModels = functionStore()->numberOfActiveFunctions(); for (int i = 0; i < nbOfActiveModels; i++) { - ExpiringPointer s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i)); + Sequence * s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i)); int firstInterestingIndex = s->initialRank(); nmin = minInt(nmin, firstInterestingIndex); nmax = maxInt(nmax, firstInterestingIndex + standardRange); @@ -75,7 +75,7 @@ bool GraphController::moveCursorHorizontally(int direction) { if (x < 0.0) { return false; } - ExpiringPointer s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(indexFunctionSelectedByCursor())); + Sequence * s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(indexFunctionSelectedByCursor())); TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); double y = s->evaluateAtAbscissa(x, myApp->localContext()); m_cursor->moveTo(x, y); diff --git a/apps/sequence/graph/graph_view.cpp b/apps/sequence/graph/graph_view.cpp index 7611adfae..9912b2ade 100644 --- a/apps/sequence/graph/graph_view.cpp +++ b/apps/sequence/graph/graph_view.cpp @@ -16,7 +16,7 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const { StorageFunctionGraphView::drawRect(ctx, rect); for (int i = 0; i < m_sequenceStore->numberOfActiveFunctions(); i++) { Ion::Storage::Record record = m_sequenceStore->activeRecordAtIndex(i); - ExpiringPointer s = m_sequenceStore->modelForRecord(record);; + Sequence * s = m_sequenceStore->modelForRecord(record);; float rectXMin = pixelToFloat(Axis::Horizontal, rect.left() - k_externRectMargin); rectXMin = rectXMin < 0 ? 0 : rectXMin; float rectXMax = pixelToFloat(Axis::Horizontal, rect.right() + k_externRectMargin); diff --git a/apps/sequence/graph/term_sum_controller.cpp b/apps/sequence/graph/term_sum_controller.cpp index 15a018a93..be27cea2f 100644 --- a/apps/sequence/graph/term_sum_controller.cpp +++ b/apps/sequence/graph/term_sum_controller.cpp @@ -49,7 +49,7 @@ double TermSumController::cursorNextStep(double x, int direction) { } Layout TermSumController::createFunctionLayout(Shared::ExpiringPointer function) { - Shared::ExpiringPointer sequence = static_cast>(function); + Sequence * sequence = static_cast(function.pointer()); return sequence->nameLayout(); } diff --git a/apps/sequence/list/list_controller.cpp b/apps/sequence/list/list_controller.cpp index c5eec2f96..80117dec1 100644 --- a/apps/sequence/list/list_controller.cpp +++ b/apps/sequence/list/list_controller.cpp @@ -30,7 +30,7 @@ const char * ListController::title() { int ListController::numberOfExpressionRows() { int numberOfRows = 0; for (int i = 0; i < modelStore()->numberOfModels(); i++) { - ExpiringPointer sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(i)); + Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(i)); numberOfRows += sequence->numberOfElements(); } if (modelStore()->numberOfModels() == modelStore()->maxNumberOfModels()) { @@ -45,7 +45,7 @@ KDCoordinate ListController::expressionRowHeight(int j) { // Add sequence row return defaultHeight; } - ExpiringPointer sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(j))); + Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(j))); Layout layout = sequence->layout(); if (sequenceDefinitionForRow(j) == 1) { layout = sequence->firstInitialConditionLayout(); @@ -70,7 +70,7 @@ Toolbox * ListController::toolboxForInputEventHandler(InputEventHandler * textIn // Set extra cells int recurrenceDepth = -1; int sequenceDefinition = sequenceDefinitionForRow(selectedRow()); - ExpiringPointer sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(selectedRow()))); + Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(selectedRow()))); if (sequenceDefinition == 0) { recurrenceDepth = sequence->numberOfElements()-1; } @@ -88,7 +88,7 @@ void ListController::selectPreviousNewSequenceCell() { void ListController::editExpression(int sequenceDefinition, Ion::Events::Event event) { Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(selectedRow())); - ExpiringPointer sequence = modelStore()->modelForRecord(record); + Sequence * sequence = modelStore()->modelForRecord(record); char * initialText = nullptr; char initialTextContent[TextField::maxBufferSize()]; if (event == Ion::Events::OK || event == Ion::Events::EXE) { @@ -152,7 +152,7 @@ bool ListController::editInitialConditionOfSelectedRecordWithText(const char * t // Reset memoization of the selected cell which always corresponds to the k_memoizedCellsCount/2 memoized cell resetMemoizationForIndex(k_memoizedCellsCount/2); Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(selectedRow())); - ExpiringPointer sequence = modelStore()->modelForRecord(record); + Sequence * sequence = modelStore()->modelForRecord(record); Ion::Storage::Record::ErrorStatus error = firstInitialCondition? sequence->setFirstInitialConditionContent(text) : sequence->setSecondInitialConditionContent(text); return (error == Ion::Storage::Record::ErrorStatus::None); } @@ -195,7 +195,7 @@ void ListController::willDisplayTitleCellAtIndex(HighlightCell * cell, int j) { myCell->setBaseline(baseline(j)); // Set the layout Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(j)); - ExpiringPointer sequence = modelStore()->modelForRecord(record); + Sequence * sequence = modelStore()->modelForRecord(record); if (sequenceDefinitionForRow(j) == 0) { myCell->setLayout(sequence->definitionName()); } @@ -213,7 +213,7 @@ void ListController::willDisplayTitleCellAtIndex(HighlightCell * cell, int j) { void ListController::willDisplayExpressionCellAtIndex(HighlightCell * cell, int j) { FunctionExpressionCell * myCell = (FunctionExpressionCell *)cell; Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(j)); - ExpiringPointer sequence = modelStore()->modelForRecord(record); + Sequence * sequence = modelStore()->modelForRecord(record); if (sequenceDefinitionForRow(j) == 0) { myCell->setLayout(sequence->layout()); } @@ -239,7 +239,7 @@ int ListController::modelIndexForRow(int j) { int sequenceIndex = -1; do { sequenceIndex++; - ExpiringPointer sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(sequenceIndex)); + Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(sequenceIndex)); rowIndex += sequence->numberOfElements(); } while (rowIndex <= j); return sequenceIndex; @@ -258,13 +258,13 @@ int ListController::sequenceDefinitionForRow(int j) { } int rowIndex = 0; int sequenceIndex = -1; - ExpiringPointer sequence(nullptr); + Sequence * sequence; do { sequenceIndex++; sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(sequenceIndex)); rowIndex += sequence->numberOfElements(); } while (rowIndex <= j); - assert(!sequence.isNull()); + assert(sequence); return sequence->numberOfElements()-rowIndex+j; } @@ -279,7 +279,7 @@ void ListController::editExpression(Ion::Events::Event event) { void ListController::reinitSelectedExpression(ExpiringPointer model) { // Invalidate the sequences context cache static_cast(app())->localContext()->resetCache(); - ExpiringPointer sequence = static_cast>(model); + Sequence * sequence = static_cast(model.pointer()); switch (sequenceDefinitionForRow(selectedRow())) { case 1: if (sequence->firstInitialConditionExpressionClone().isUninitialized()) { diff --git a/apps/sequence/list/list_parameter_controller.h b/apps/sequence/list/list_parameter_controller.h index 803c12b8c..e65adc82e 100644 --- a/apps/sequence/list/list_parameter_controller.h +++ b/apps/sequence/list/list_parameter_controller.h @@ -30,7 +30,7 @@ private: constexpr static int k_totalNumberOfCell = 4; #endif int totalNumberOfCells() const override; - Shared::ExpiringPointer sequence() { return static_cast>(function()); } + Sequence * sequence() { return static_cast(function().pointer()); } bool hasInitialRankRow() const; MessageTableCellWithChevronAndExpression m_typeCell; MessageTableCellWithEditableText m_initialRankCell; diff --git a/apps/sequence/list/type_parameter_controller.cpp b/apps/sequence/list/type_parameter_controller.cpp index 3fe2e46c8..ab01f1c43 100644 --- a/apps/sequence/list/type_parameter_controller.cpp +++ b/apps/sequence/list/type_parameter_controller.cpp @@ -79,7 +79,7 @@ bool TypeParameterController::handleEvent(Ion::Events::Event event) { } assert(error == Ion::Storage::Record::ErrorStatus::None); Ion::Storage::Record record = sequenceStore()->recordAtIndex(sequenceStore()->numberOfModels()-1); - ExpiringPointer newSequence = sequenceStore()->modelForRecord(record); + Sequence * newSequence = sequenceStore()->modelForRecord(record); newSequence->setType((Sequence::Type)selectedRow()); app()->dismissModalViewController(); m_listController->editExpression(0, Ion::Events::OK); diff --git a/apps/sequence/list/type_parameter_controller.h b/apps/sequence/list/type_parameter_controller.h index 2dbd318e0..4c8a36136 100644 --- a/apps/sequence/list/type_parameter_controller.h +++ b/apps/sequence/list/type_parameter_controller.h @@ -28,7 +28,7 @@ public: void setRecord(Ion::Storage::Record record); private: StackViewController * stackController() const; - Shared::ExpiringPointer sequence() { + Sequence * sequence() { assert(!m_record.isNull()); return sequenceStore()->modelForRecord(m_record); } diff --git a/apps/sequence/sequence_context.cpp b/apps/sequence/sequence_context.cpp index 76d20b3a5..d6f6cac92 100644 --- a/apps/sequence/sequence_context.cpp +++ b/apps/sequence/sequence_context.cpp @@ -50,22 +50,22 @@ void TemplatedSequenceContext::step(SequenceStore * sequenceStore, SequenceCo } /* Evaluate new u(n) and v(n) */ - ExpiringPointer u = sequenceStore->numberOfModels() > 0 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(0)) : nullptr; - u = u.isNull() && u->isDefined() ? u : nullptr; - ExpiringPointer v = sequenceStore->numberOfModels() > 1 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(1)) : nullptr; - v = v.isNull() && v->isDefined() ? v : nullptr; + Sequence * u = sequenceStore->numberOfModels() > 0 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(0)) : nullptr; + u = u && u->isDefined() ? u : nullptr; + Sequence * v = sequenceStore->numberOfModels() > 1 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(1)) : nullptr; + v = v && v->isDefined() ? v : nullptr; /* Switch u & v if the name of u is v */ - if (!u.isNull() && u->fullName()[0] == SequenceStore::k_sequenceNames[1][0]) { - ExpiringPointer temp = u; + if (u && u->fullName()[0] == SequenceStore::k_sequenceNames[1][0]) { + Sequence * temp = u; u = v; v = temp; } /* Approximate u & v at the new rank. We evaluate u twice in case its * expression depends on v. */ - m_values[0][0] = !u.isNull() ? u->approximateToNextRank(m_rank, sqctx) : NAN; - m_values[1][0] = !v.isNull() ? v->approximateToNextRank(m_rank, sqctx) : NAN; - m_values[0][0] = !u.isNull() ? u->approximateToNextRank(m_rank, sqctx) : NAN; + m_values[0][0] = u ? u->approximateToNextRank(m_rank, sqctx) : NAN; + m_values[1][0] = v ? v->approximateToNextRank(m_rank, sqctx) : NAN; + m_values[0][0] = u ? u->approximateToNextRank(m_rank, sqctx) : NAN; } template class TemplatedSequenceContext; diff --git a/apps/sequence/sequence_store.h b/apps/sequence/sequence_store.h index 4082de393..2b5143cd6 100644 --- a/apps/sequence/sequence_store.h +++ b/apps/sequence/sequence_store.h @@ -13,7 +13,10 @@ class SequenceStore : public Shared::StorageFunctionStore { public: using Shared::StorageFunctionStore::StorageFunctionStore; char symbol() const override { return Sequence::Symbol(); } - Shared::ExpiringPointer modelForRecord(Ion::Storage::Record record) const { return Shared::ExpiringPointer(static_cast(privateModelForRecord(record))); } + /* Sequence Store hold all its Sequences in an array. The Sequence pointers + * return by modelForRecord are therefore non-expirable. We choose to return + * Sequence * instead of ExpiringPointer. */ + Sequence * modelForRecord(Ion::Storage::Record record) const { return static_cast(privateModelForRecord(record)); } Ion::Storage::Record::ErrorStatus addEmptyModel() override; /* WARNING: after calling removeModel or removeAll, the sequence context * need to invalidate its cache as the sequences evaluations might have diff --git a/apps/sequence/values/values_controller.cpp b/apps/sequence/values/values_controller.cpp index 653444660..8b46ec61d 100644 --- a/apps/sequence/values/values_controller.cpp +++ b/apps/sequence/values/values_controller.cpp @@ -31,7 +31,7 @@ void ValuesController::willDisplayCellAtLocation(HighlightCell * cell, int i, in // The cell is a function title cell: if (j == 0 && i > 0) { SequenceTitleCell * myCell = (SequenceTitleCell *)cell; - Shared::ExpiringPointer sequence = functionStore()->modelForRecord(recordAtColumn(i)); + Sequence * sequence = functionStore()->modelForRecord(recordAtColumn(i)); myCell->setLayout(sequence->nameLayout()); myCell->setColor(sequence->color()); } diff --git a/apps/shared/expiring_pointer.h b/apps/shared/expiring_pointer.h index 699e47939..871a61b97 100644 --- a/apps/shared/expiring_pointer.h +++ b/apps/shared/expiring_pointer.h @@ -15,13 +15,7 @@ public: s_global = rawPointer; #endif } - template - ExpiringPointer(ExpiringPointer p) : m_rawPointer(static_cast(p.m_rawPointer)) { -#if DEBUG - s_global = m_rawPointer; -#endif - } - bool isNull() { return m_rawPointer == nullptr; } + T * pointer() { return m_rawPointer; } T *operator->() { #if DEBUG assert(m_rawPointer != nullptr && m_rawPointer == s_global);