[poincare] Move from InfinityReference to Infinity

This commit is contained in:
Romain Goyet
2018-08-07 17:47:26 +02:00
parent c29afba2bd
commit 3667e0ff6c
4 changed files with 47 additions and 18 deletions

View File

@@ -2,7 +2,7 @@
#define POINCARE_ALLOCATION_FAILED_EXPRESSION_NODE_H
#include <poincare/expression_node.h>
#include <poincare/expression_reference.h>
#include <poincare/expression.h>
#include <poincare/complex.h>
#include <poincare/allocation_failed_layout_node.h>
#include <stdio.h>
@@ -11,10 +11,14 @@ namespace Poincare {
class AllocationFailedExpressionNode : public ExpressionNode {
public:
static AllocationFailedExpressionNode * FailedAllocationStaticNode() {
return static_cast<AllocationFailedExpressionNode *>(ExpressionNode::FailedAllocationStaticNode());
}
// ExpressionNode
Type type() const override { return Type::AllocationFailure; }
EvaluationReference<float> approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return ComplexReference<float>::Undefined(); }
EvaluationReference<double> approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return ComplexReference<double>::Undefined(); }
Evaluation<float> approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return Complex<float>::Undefined(); }
Evaluation<double> approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return Complex<double>::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<AllocationFailedExpressionNode>(), true) {}
AllocationFailedExpressionRef(TreeNode * n) : ExpressionReference(n) {}
AllocationFailedExpressionRef() : Expression(TreePool::sharedPool()->createTreeNode<AllocationFailedExpressionNode>()) {}
};
}

View File

@@ -9,7 +9,10 @@ namespace Poincare {
class AllocationFailedLayoutNode : public LayoutNode {
public:
using GhostLayoutNode::GhostLayoutNode;
static AllocationFailedLayoutNode * FailedAllocationStaticNode() {
return static_cast<AllocationFailedLayoutNode *>(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<AllocationFailedLayoutNode>(), true) {}
AllocationFailedLayoutRef() : LayoutReference(TreePool::sharedPool()->createTreeNode<AllocationFailedLayoutNode>()) {}
};
}

View File

@@ -2,11 +2,16 @@
#define POINCARE_INFINITY_H
#include <poincare/number.h>
#include <poincare/allocation_failed_expression_node.h>
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<float> approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override {
Evaluation<float> approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override {
return templatedApproximate<float>();
}
EvaluationReference<double> approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override {
Evaluation<double> approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override {
return templatedApproximate<double>();
}
@@ -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<typename T> EvaluationReference<T> templatedApproximate() const;
template<typename T> Evaluation<T> 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<InfinityNode>(), true) {
if (!node->isAllocationFailure()) {
static_cast<InfinityNode *>(node)->setNegative(negative);
}
Infinity(bool negative) : Number(TreePool::sharedPool()->createTreeNode<InfinityNode>()) {
node()->setNegative(negative);
}
private:
InfinityNode * node() { return static_cast<InfinityNode *>(Number::node()); }
};
}

View File

@@ -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<float>(m_negative ? -INFINITY : INFINITY, buffer, bufferSize, numberOfSignificantDigits, floatDisplayMode);
}
template<typename T> EvaluationReference<T> InfinityNode::templatedApproximate() const {
return ComplexReference<T>(INFINITY);
template<typename T> Evaluation<T> InfinityNode::templatedApproximate() const {
return Complex<T>(INFINITY);
}
}