From 3667e0ff6cfc83398b2f758d2d4560b4e6b8359b Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Tue, 7 Aug 2018 17:47:26 +0200 Subject: [PATCH] [poincare] Move from InfinityReference to Infinity --- .../allocation_failed_expression_node.h | 15 ++++---- .../poincare/allocation_failed_layout_node.h | 7 ++-- poincare/include/poincare/infinity.h | 34 ++++++++++++++----- poincare/src/infinity.cpp | 9 +++-- 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/poincare/include/poincare/allocation_failed_expression_node.h b/poincare/include/poincare/allocation_failed_expression_node.h index 0c0e11369..5ead95ed6 100644 --- a/poincare/include/poincare/allocation_failed_expression_node.h +++ b/poincare/include/poincare/allocation_failed_expression_node.h @@ -2,7 +2,7 @@ #define POINCARE_ALLOCATION_FAILED_EXPRESSION_NODE_H #include -#include +#include #include #include #include @@ -11,10 +11,14 @@ namespace Poincare { class AllocationFailedExpressionNode : public ExpressionNode { public: + static AllocationFailedExpressionNode * FailedAllocationStaticNode() { + return static_cast(ExpressionNode::FailedAllocationStaticNode()); + } + // ExpressionNode Type type() const override { return Type::AllocationFailure; } - EvaluationReference approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return ComplexReference::Undefined(); } - EvaluationReference approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return ComplexReference::Undefined(); } + Evaluation approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return Complex::Undefined(); } + Evaluation approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return Complex::Undefined(); } int writeTextInBuffer(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const override { int descriptionLength = strlen(description()) + 1; return strlcpy(buffer, description(), bufferSize < descriptionLength ? bufferSize : descriptionLength); @@ -30,10 +34,9 @@ public: bool isAllocationFailure() const override { return true; } }; -class AllocationFailedExpressionRef : public ExpressionReference { +class AllocationFailedExpressionRef : public Expression { public: - AllocationFailedExpressionRef() : ExpressionReference(TreePool::sharedPool()->createTreeNode(), true) {} - AllocationFailedExpressionRef(TreeNode * n) : ExpressionReference(n) {} + AllocationFailedExpressionRef() : Expression(TreePool::sharedPool()->createTreeNode()) {} }; } diff --git a/poincare/include/poincare/allocation_failed_layout_node.h b/poincare/include/poincare/allocation_failed_layout_node.h index 1fe844754..6c36dd6d0 100644 --- a/poincare/include/poincare/allocation_failed_layout_node.h +++ b/poincare/include/poincare/allocation_failed_layout_node.h @@ -9,7 +9,10 @@ namespace Poincare { class AllocationFailedLayoutNode : public LayoutNode { public: - using GhostLayoutNode::GhostLayoutNode; + static AllocationFailedLayoutNode * FailedAllocationStaticNode() { + return static_cast(LayoutNode::FailedAllocationStaticNode()); + } + // TreeNode bool isAllocationFailure() const override { return true; } size_t size() const override { return sizeof(AllocationFailedLayoutNode); } @@ -47,7 +50,7 @@ private: class AllocationFailedLayoutRef : public LayoutReference { public: - AllocationFailedLayoutRef() : LayoutReference(TreePool::sharedPool()->createTreeNode(), true) {} + AllocationFailedLayoutRef() : LayoutReference(TreePool::sharedPool()->createTreeNode()) {} }; } diff --git a/poincare/include/poincare/infinity.h b/poincare/include/poincare/infinity.h index 8bc5758aa..d09975b2b 100644 --- a/poincare/include/poincare/infinity.h +++ b/poincare/include/poincare/infinity.h @@ -2,11 +2,16 @@ #define POINCARE_INFINITY_H #include +#include namespace Poincare { +class AllocationFailureInfinityNode; + class InfinityNode : public NumberNode { public: + static InfinityNode * FailedAllocationStaticNode(); + void setNegative(bool negative) { m_negative = negative; } // TreeNode @@ -20,10 +25,10 @@ public: Sign sign() const override { return m_negative ? Sign::Negative : Sign::Positive; } // Approximation - EvaluationReference approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { + Evaluation approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return templatedApproximate(); } - EvaluationReference approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { + Evaluation approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return templatedApproximate(); } @@ -31,17 +36,30 @@ public: LayoutRef createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override; int writeTextInBuffer(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const override; private: - template EvaluationReference templatedApproximate() const; + template Evaluation templatedApproximate() const; bool m_negative; }; -class InfinityReference : public NumberReference { +class AllocationFailureInfinityNode : public InfinityNode, public AllocationFailedExpressionNode { + using AllocationFailedExpressionNode::type; + using AllocationFailedExpressionNode::approximate; + using AllocationFailedExpressionNode::writeTextInBuffer; + using AllocationFailedExpressionNode::createLayout; + using AllocationFailedExpressionNode::numberOfChildren; + using AllocationFailedExpressionNode::isAllocationFailure; + size_t size() const override { return sizeof(AllocationFailureInfinityNode); } +#if TREE_LOG + const char * description() const override { return "AllocationFailureInfinityNode"; } +#endif +}; + +class Infinity : public Number { public: - InfinityReference(bool negative) : NumberReference(TreePool::sharedPool()->createTreeNode(), true) { - if (!node->isAllocationFailure()) { - static_cast(node)->setNegative(negative); - } + Infinity(bool negative) : Number(TreePool::sharedPool()->createTreeNode()) { + node()->setNegative(negative); } +private: + InfinityNode * node() { return static_cast(Number::node()); } }; } diff --git a/poincare/src/infinity.cpp b/poincare/src/infinity.cpp index c4929eec2..854500b76 100644 --- a/poincare/src/infinity.cpp +++ b/poincare/src/infinity.cpp @@ -9,6 +9,11 @@ extern "C" { namespace Poincare { +InfinityNode * InfinityNode::FailedAllocationStaticNode() { + static AllocationFailureInfinityNode failure; + return &failure; +} + LayoutRef InfinityNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { char buffer[5]; int numberOfChars = writeTextInBuffer(buffer, 5, floatDisplayMode, numberOfSignificantDigits); @@ -22,8 +27,8 @@ int InfinityNode::writeTextInBuffer(char * buffer, int bufferSize, Preferences:: return PrintFloat::convertFloatToText(m_negative ? -INFINITY : INFINITY, buffer, bufferSize, numberOfSignificantDigits, floatDisplayMode); } -template EvaluationReference InfinityNode::templatedApproximate() const { - return ComplexReference(INFINITY); +template Evaluation InfinityNode::templatedApproximate() const { + return Complex(INFINITY); } }