From df2b9c4d81649b052bec17d87df0c360550194e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 21 Dec 2018 13:29:32 +0100 Subject: [PATCH] [escher/apps] Get children count via template in ToolboxMessageTree::Node --- apps/code/python_toolbox.cpp | 73 ++++++++------------ apps/math_toolbox.cpp | 57 ++++++--------- escher/include/escher/toolbox_message_tree.h | 5 +- 3 files changed, 53 insertions(+), 82 deletions(-) diff --git a/apps/code/python_toolbox.cpp b/apps/code/python_toolbox.cpp index aad702aed..b38ff341d 100644 --- a/apps/code/python_toolbox.cpp +++ b/apps/code/python_toolbox.cpp @@ -8,31 +8,14 @@ extern "C" { namespace Code { -static constexpr int catalogChildrenCount = 125; -static constexpr int MathModuleChildrenCount = 43; -static constexpr int KandinskyModuleChildrenCount = 7; -static constexpr int CMathModuleChildrenCount = 13; -static constexpr int RandomModuleChildrenCount = 10; -static constexpr int TurtleModuleChildrenCount = 30; - -static constexpr int conditionsChildrenCount = 9; -static constexpr int forLoopChildrenCount = 4; -static constexpr int functionsChildrenCount = 2; -static constexpr int ifStatementChildrenCount = 5; -static constexpr int loopsAndTestsChildrenCount = 4; -static constexpr int menuChildrenCount = 4; -static constexpr int modulesChildrenCount = 5; -static constexpr int whileLoopChildrenCount = 1; - - -const ToolboxMessageTree forLoopChildren[forLoopChildrenCount] = { +const ToolboxMessageTree forLoopChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::ForInRange1ArgLoopWithArg, I18n::Message::Default, false, I18n::Message::ForInRange1ArgLoop), ToolboxMessageTree::Leaf(I18n::Message::ForInRange2ArgsLoopWithArg, I18n::Message::Default, false, I18n::Message::ForInRange2ArgsLoop), ToolboxMessageTree::Leaf(I18n::Message::ForInRange3ArgsLoopWithArg, I18n::Message::Default, false, I18n::Message::ForInRange3ArgsLoop), ToolboxMessageTree::Leaf(I18n::Message::ForInListLoopWithArg, I18n::Message::Default, false, I18n::Message::ForInListLoop) }; -const ToolboxMessageTree ifStatementChildren[ifStatementChildrenCount] = { +const ToolboxMessageTree ifStatementChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::IfElseStatementWithArg, I18n::Message::Default, false, I18n::Message::IfElseStatement), ToolboxMessageTree::Leaf(I18n::Message::IfThenStatementWithArg, I18n::Message::Default, false, I18n::Message::IfThenStatement), ToolboxMessageTree::Leaf(I18n::Message::IfElifElseStatementWithArg, I18n::Message::Default, false, I18n::Message::IfElifElseStatement), @@ -40,11 +23,11 @@ const ToolboxMessageTree ifStatementChildren[ifStatementChildrenCount] = { ToolboxMessageTree::Leaf(I18n::Message::IfOrIfElseStatementWithArg, I18n::Message::Default, false, I18n::Message::IfOrIfElseStatement) }; -const ToolboxMessageTree whileLoopChildren[whileLoopChildrenCount] = { +const ToolboxMessageTree whileLoopChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::WhileLoopWithArg, I18n::Message::Default, false, I18n::Message::WhileLoop) }; -const ToolboxMessageTree conditionsChildren[conditionsChildrenCount] = { +const ToolboxMessageTree conditionsChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::EqualityConditionWithArg, I18n::Message::Default, false, I18n::Message::EqualityCondition), ToolboxMessageTree::Leaf(I18n::Message::NonEqualityConditionWithArg, I18n::Message::Default, false, I18n::Message::NonEqualityCondition), ToolboxMessageTree::Leaf(I18n::Message::SuperiorStrictConditionWithArg, I18n::Message::Default, false, I18n::Message::SuperiorStrictCondition), @@ -56,14 +39,14 @@ const ToolboxMessageTree conditionsChildren[conditionsChildrenCount] = { ToolboxMessageTree::Leaf(I18n::Message::ConditionNot, I18n::Message::Default, false) }; -const ToolboxMessageTree loopsAndTestsChildren[loopsAndTestsChildrenCount] = { - ToolboxMessageTree::Node(I18n::Message::ForLoopMenu, forLoopChildren, forLoopChildrenCount), - ToolboxMessageTree::Node(I18n::Message::IfStatementMenu, ifStatementChildren, ifStatementChildrenCount), - ToolboxMessageTree::Node(I18n::Message::WhileLoopMenu, whileLoopChildren, whileLoopChildrenCount), - ToolboxMessageTree::Node(I18n::Message::ConditionsMenu, conditionsChildren, conditionsChildrenCount) +const ToolboxMessageTree loopsAndTestsChildren[] = { + ToolboxMessageTree::Node(I18n::Message::ForLoopMenu, forLoopChildren), + ToolboxMessageTree::Node(I18n::Message::IfStatementMenu, ifStatementChildren), + ToolboxMessageTree::Node(I18n::Message::WhileLoopMenu, whileLoopChildren), + ToolboxMessageTree::Node(I18n::Message::ConditionsMenu, conditionsChildren) }; -const ToolboxMessageTree MathModuleChildren[MathModuleChildrenCount] = { +const ToolboxMessageTree MathModuleChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportMath, I18n::Message::PythonImportMath, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportFromMath, I18n::Message::PythonImportFromMath, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandMathFunction, I18n::Message::PythonMathFunction, false, I18n::Message::PythonCommandMathFunctionWithoutArg), @@ -109,7 +92,7 @@ const ToolboxMessageTree MathModuleChildren[MathModuleChildrenCount] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandLgamma, I18n::Message::PythonLgamma) }; -const ToolboxMessageTree KandinskyModuleChildren[KandinskyModuleChildrenCount] = { +const ToolboxMessageTree KandinskyModuleChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportKandinsky, I18n::Message::PythonImportKandinsky, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportFromKandinsky, I18n::Message::PythonImportFromKandinsky, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandKandinskyFunction, I18n::Message::PythonKandinskyFunction, false), @@ -119,7 +102,7 @@ const ToolboxMessageTree KandinskyModuleChildren[KandinskyModuleChildrenCount] = ToolboxMessageTree::Leaf(I18n::Message::PythonCommandDrawString, I18n::Message::PythonDrawString) }; -const ToolboxMessageTree RandomModuleChildren[RandomModuleChildrenCount] = { +const ToolboxMessageTree RandomModuleChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportRandom, I18n::Message::PythonImportRandom, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportFromRandom, I18n::Message::PythonImportFromRandom, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandRandomFunction, I18n::Message::PythonRandomFunction, false, I18n::Message::PythonCommandRandomFunctionWithoutArg), @@ -132,7 +115,7 @@ const ToolboxMessageTree RandomModuleChildren[RandomModuleChildrenCount] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandUniform, I18n::Message::PythonUniform) }; -const ToolboxMessageTree CMathModuleChildren[CMathModuleChildrenCount] = { +const ToolboxMessageTree CMathModuleChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportCmath, I18n::Message::PythonImportCmath, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportFromCmath, I18n::Message::PythonImportFromCmath, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandCmathFunction, I18n::Message::PythonCmathFunction, false, I18n::Message::PythonCommandCmathFunctionWithoutArg), @@ -148,7 +131,7 @@ const ToolboxMessageTree CMathModuleChildren[CMathModuleChildrenCount] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandSinComplex, I18n::Message::PythonSin) }; -const ToolboxMessageTree TurtleModuleChildren[TurtleModuleChildrenCount] = { +const ToolboxMessageTree TurtleModuleChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportTurtle, I18n::Message::PythonImportTurtle, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImportFromTurtle, I18n::Message::PythonImportFromTurtle, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandTurtleFunction, I18n::Message::PythonTurtleFunction, false, I18n::Message::PythonCommandTurtleFunctionWithoutArg), @@ -181,15 +164,15 @@ const ToolboxMessageTree TurtleModuleChildren[TurtleModuleChildrenCount] = { ToolboxMessageTree::Leaf(I18n::Message::PythonTurtleCommandGrey, I18n::Message::PythonTurtleGrey, false) }; -const ToolboxMessageTree modulesChildren[modulesChildrenCount] = { - ToolboxMessageTree::Node(I18n::Message::MathModule, MathModuleChildren, MathModuleChildrenCount), - ToolboxMessageTree::Node(I18n::Message::CmathModule, CMathModuleChildren, CMathModuleChildrenCount), - ToolboxMessageTree::Node(I18n::Message::RandomModule, RandomModuleChildren, RandomModuleChildrenCount), - ToolboxMessageTree::Node(I18n::Message::TurtleModule, TurtleModuleChildren, TurtleModuleChildrenCount), - ToolboxMessageTree::Node(I18n::Message::KandinskyModule, KandinskyModuleChildren, KandinskyModuleChildrenCount) +const ToolboxMessageTree modulesChildren[] = { + ToolboxMessageTree::Node(I18n::Message::MathModule, MathModuleChildren), + ToolboxMessageTree::Node(I18n::Message::CmathModule, CMathModuleChildren), + ToolboxMessageTree::Node(I18n::Message::RandomModule, RandomModuleChildren), + ToolboxMessageTree::Node(I18n::Message::TurtleModule, TurtleModuleChildren), + ToolboxMessageTree::Node(I18n::Message::KandinskyModule, KandinskyModuleChildren) }; -const ToolboxMessageTree catalogChildren[catalogChildrenCount] = { +const ToolboxMessageTree catalogChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandPound, I18n::Message::PythonPound, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandPercent, I18n::Message::PythonPercent, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommand1J, I18n::Message::Python1J, false), @@ -317,19 +300,19 @@ const ToolboxMessageTree catalogChildren[catalogChildrenCount] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandReal, I18n::Message::PythonReal, false, I18n::Message::PythonCommandRealWithoutArg) }; -const ToolboxMessageTree functionsChildren[functionsChildrenCount] = { +const ToolboxMessageTree functionsChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandDefWithArg, I18n::Message::Default, false, I18n::Message::PythonCommandDef), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandReturn, I18n::Message::Default) }; -const ToolboxMessageTree menu[menuChildrenCount] = { - ToolboxMessageTree::Node(I18n::Message::LoopsAndTests, loopsAndTestsChildren, loopsAndTestsChildrenCount), - ToolboxMessageTree::Node(I18n::Message::Modules, modulesChildren, modulesChildrenCount), - ToolboxMessageTree::Node(I18n::Message::Catalog, catalogChildren, catalogChildrenCount), - ToolboxMessageTree::Node(I18n::Message::Functions, functionsChildren, functionsChildrenCount) +const ToolboxMessageTree menu[] = { + ToolboxMessageTree::Node(I18n::Message::LoopsAndTests, loopsAndTestsChildren), + ToolboxMessageTree::Node(I18n::Message::Modules, modulesChildren), + ToolboxMessageTree::Node(I18n::Message::Catalog, catalogChildren), + ToolboxMessageTree::Node(I18n::Message::Functions, functionsChildren) }; -const ToolboxMessageTree toolboxModel = ToolboxMessageTree::Node(I18n::Message::Toolbox, menu, menuChildrenCount); +const ToolboxMessageTree toolboxModel = ToolboxMessageTree::Node(I18n::Message::Toolbox, menu); PythonToolbox::PythonToolbox() : diff --git a/apps/math_toolbox.cpp b/apps/math_toolbox.cpp index 8e2fa2e89..7145a6ea0 100644 --- a/apps/math_toolbox.cpp +++ b/apps/math_toolbox.cpp @@ -5,20 +5,18 @@ using namespace Poincare; -/* TODO: find a shorter way to initialize tree models - * We create one model tree: each node keeps the label of the row it refers to +/* We create one model tree: each node keeps the label of the row it refers to * and the text which would be edited by clicking on the row. When the node is a * subtree, the edited text is set at I18n::Message::Default. */ - -const ToolboxMessageTree calculChildren[4] = { +const ToolboxMessageTree calculChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::DiffCommandWithArg, I18n::Message::DerivateNumber, false, I18n::Message::DiffCommand), ToolboxMessageTree::Leaf(I18n::Message::IntCommandWithArg, I18n::Message::Integral, false, I18n::Message::IntCommand), ToolboxMessageTree::Leaf(I18n::Message::SumCommandWithArg, I18n::Message::Sum, false, I18n::Message::SumCommand), ToolboxMessageTree::Leaf(I18n::Message::ProductCommandWithArg, I18n::Message::Product, false, I18n::Message::ProductCommand) }; -const ToolboxMessageTree complexChildren[5] = { +const ToolboxMessageTree complexChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::AbsCommandWithArg,I18n::Message::ComplexAbsoluteValue), ToolboxMessageTree::Leaf(I18n::Message::ArgCommandWithArg, I18n::Message::Agument), ToolboxMessageTree::Leaf(I18n::Message::ReCommandWithArg, I18n::Message::RealPart), @@ -26,12 +24,12 @@ const ToolboxMessageTree complexChildren[5] = { ToolboxMessageTree::Leaf(I18n::Message::ConjCommandWithArg, I18n::Message::Conjugate) }; -const ToolboxMessageTree probabilityChildren[2] = { +const ToolboxMessageTree probabilityChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::BinomialCommandWithArg, I18n::Message::Combination), ToolboxMessageTree::Leaf(I18n::Message::PermuteCommandWithArg, I18n::Message::Permutation) }; -const ToolboxMessageTree arithmeticChildren[5] = { +const ToolboxMessageTree arithmeticChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::GcdCommandWithArg, I18n::Message::GreatCommonDivisor), ToolboxMessageTree::Leaf(I18n::Message::LcmCommandWithArg, I18n::Message::LeastCommonMultiple), ToolboxMessageTree::Leaf(I18n::Message::FactorCommandWithArg, I18n::Message::PrimeFactorDecomposition), @@ -40,7 +38,7 @@ const ToolboxMessageTree arithmeticChildren[5] = { }; #if MATRICES_ARE_DEFINED -const ToolboxMessageTree matricesChildren[6] = { +const ToolboxMessageTree matricesChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::MatrixCommandWithArg, I18n::Message::NewMatrix, false, I18n::Message::MatrixCommand), ToolboxMessageTree::Leaf(I18n::Message::InverseCommandWithArg, I18n::Message::Inverse), ToolboxMessageTree::Leaf(I18n::Message::DeterminantCommandWithArg, I18n::Message::Determinant), @@ -51,7 +49,7 @@ const ToolboxMessageTree matricesChildren[6] = { #endif #if LIST_ARE_DEFINED -const ToolboxMessageTree listesChildren[5] = { +const ToolboxMessageTree listsChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::SortCommandWithArg, I18n::Message::Sort), ToolboxMessageTree::Leaf(I18n::Message::InvSortCommandWithArg, I18n::Message::InvSort), ToolboxMessageTree::Leaf(I18n::Message::MaxCommandWithArg, I18n::Message::Maximum), @@ -60,7 +58,7 @@ const ToolboxMessageTree listesChildren[5] = { }; #endif -const ToolboxMessageTree randomAndApproximationChildren[6] = { +const ToolboxMessageTree randomAndApproximationChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::RandomCommandWithArg, I18n::Message::RandomFloat), ToolboxMessageTree::Leaf(I18n::Message::RandintCommandWithArg, I18n::Message::RandomInteger), ToolboxMessageTree::Leaf(I18n::Message::FloorCommandWithArg, I18n::Message::Floor), @@ -68,7 +66,7 @@ const ToolboxMessageTree randomAndApproximationChildren[6] = { ToolboxMessageTree::Leaf(I18n::Message::CeilCommandWithArg, I18n::Message::Ceiling), ToolboxMessageTree::Leaf(I18n::Message::RoundCommandWithArg, I18n::Message::Rounding)}; -const ToolboxMessageTree trigonometryChildren[6] = { +const ToolboxMessageTree trigonometryChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::CoshCommandWithArg, I18n::Message::HyperbolicCosine), ToolboxMessageTree::Leaf(I18n::Message::SinhCommandWithArg, I18n::Message::HyperbolicSine), ToolboxMessageTree::Leaf(I18n::Message::TanhCommandWithArg, I18n::Message::HyperbolicTangent), @@ -76,41 +74,30 @@ const ToolboxMessageTree trigonometryChildren[6] = { ToolboxMessageTree::Leaf(I18n::Message::AsinhCommandWithArg, I18n::Message::InverseHyperbolicSine), ToolboxMessageTree::Leaf(I18n::Message::AtanhCommandWithArg, I18n::Message::InverseHyperbolicTangent)}; -const ToolboxMessageTree predictionChildren[3] = { +const ToolboxMessageTree predictionChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::Prediction95CommandWithArg, I18n::Message::Prediction95), ToolboxMessageTree::Leaf(I18n::Message::PredictionCommandWithArg, I18n::Message::Prediction), ToolboxMessageTree::Leaf(I18n::Message::ConfidenceCommandWithArg, I18n::Message::Confidence)}; -#if LIST_ARE_DEFINED -const ToolboxMessageTree menu[12] = { -#elif MATRICES_ARE_DEFINED -const ToolboxMessageTree menu[11] = { -#else -const ToolboxMessageTree menu[10] = { -#endif +const ToolboxMessageTree menu[] = { ToolboxMessageTree::Leaf(I18n::Message::AbsCommandWithArg, I18n::Message::AbsoluteValue), ToolboxMessageTree::Leaf(I18n::Message::RootCommandWithArg, I18n::Message::NthRoot), ToolboxMessageTree::Leaf(I18n::Message::LogCommandWithArg, I18n::Message::BasedLogarithm), - ToolboxMessageTree::Node(I18n::Message::Calculation, calculChildren, 4), - ToolboxMessageTree::Node(I18n::Message::ComplexNumber, complexChildren, 5), - ToolboxMessageTree::Node(I18n::Message::Probability, probabilityChildren, 2), - ToolboxMessageTree::Node(I18n::Message::Arithmetic, arithmeticChildren, 5), + ToolboxMessageTree::Node(I18n::Message::Calculation, calculChildren), + ToolboxMessageTree::Node(I18n::Message::ComplexNumber, complexChildren), + ToolboxMessageTree::Node(I18n::Message::Probability, probabilityChildren), + ToolboxMessageTree::Node(I18n::Message::Arithmetic, arithmeticChildren), #if MATRICES_ARE_DEFINED - ToolboxMessageTree::Node(I18n::Message::Matrices, matricesChildren, 6), + ToolboxMessageTree::Node(I18n::Message::Matrices, matricesChildren), #endif #if LIST_ARE_DEFINED - ToolboxMessageTree::Node(I18n::Message::Lists,listesChildren, 5), -#endif - ToolboxMessageTree::Node(I18n::Message::RandomAndApproximation, randomAndApproximationChildren, 6), - ToolboxMessageTree::Node(I18n::Message::HyperbolicTrigonometry, trigonometryChildren, 6), - ToolboxMessageTree::Node(I18n::Message::Fluctuation, predictionChildren, 3)}; -#if LIST_ARE_DEFINED -const ToolboxMessageTree toolboxModel = ToolboxMessageTree::Node(I18n::Message::Toolbox, menu, 12); -#elif MATRICES_ARE_DEFINED -const ToolboxMessageTree toolboxModel = ToolboxMessageTree::Node(I18n::Message::Toolbox, menu, 11); -#else -const ToolboxMessageTree toolboxModel = ToolboxMessageTree::Node(I18n::Message::Toolbox, menu, 10); + ToolboxMessageTree::Node(I18n::Message::Lists,listsChildren), #endif + ToolboxMessageTree::Node(I18n::Message::RandomAndApproximation, randomAndApproximationChildren), + ToolboxMessageTree::Node(I18n::Message::HyperbolicTrigonometry, trigonometryChildren), + ToolboxMessageTree::Node(I18n::Message::Fluctuation, predictionChildren)}; + +const ToolboxMessageTree toolboxModel = ToolboxMessageTree::Node(I18n::Message::Toolbox, menu); MathToolbox::MathToolbox() : Toolbox(nullptr, rootModel()->label()) diff --git a/escher/include/escher/toolbox_message_tree.h b/escher/include/escher/toolbox_message_tree.h index 2f1aa402f..c4a70d77c 100644 --- a/escher/include/escher/toolbox_message_tree.h +++ b/escher/include/escher/toolbox_message_tree.h @@ -14,13 +14,14 @@ public: 0, stripInsertedText); }; - constexpr static ToolboxMessageTree Node(I18n::Message label, const ToolboxMessageTree * children, int numberOfChildren) { + template + constexpr static ToolboxMessageTree Node(I18n::Message label, const ToolboxMessageTree (&children)[N]) { return ToolboxMessageTree( label, (I18n::Message)0, (I18n::Message)0, children, - numberOfChildren, + N, true); } const MessageTree * children(int index) const override { return &m_children[index]; }