mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[poincare] Remove SerializationHelperInterface
All TreeNodes are now serializable. This removes a pointer to virtual table from the nodes (for instance, an ExpressionNode was 24o, now 16o). It also remove 4K of code in the binary.
This commit is contained in:
committed by
EmilieNumworks
parent
8604e73e97
commit
b8408d2575
@@ -39,7 +39,7 @@ public:
|
||||
}
|
||||
private:
|
||||
// Layout
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override;
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override;
|
||||
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
|
||||
return SerializationHelper::Infix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, name());
|
||||
|
||||
@@ -42,7 +42,7 @@ public:
|
||||
}
|
||||
|
||||
// Layout
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override;
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override;
|
||||
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
|
||||
return SerializationHelper::Infix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "/");
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <poincare/tree_node.h>
|
||||
#include <poincare/evaluation.h>
|
||||
#include <poincare/layout.h>
|
||||
#include <poincare/serialization_helper_interface.h>
|
||||
#include <poincare/context.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -13,7 +12,7 @@ namespace Poincare {
|
||||
/* Methods preceded by '*!*' interfere with the expression pool, which can make
|
||||
* 'this' outdated. They should only be called in a wrapper on Expression. */
|
||||
|
||||
class ExpressionNode : public TreeNode, public SerializationHelperInterface {
|
||||
class ExpressionNode : public TreeNode {
|
||||
friend class AdditionNode;
|
||||
friend class DivisionNode;
|
||||
friend class NAryExpressionNode;
|
||||
@@ -159,10 +158,6 @@ protected:
|
||||
/* Hierarchy */
|
||||
ExpressionNode * parent() const override { return static_cast<ExpressionNode *>(TreeNode::parent()); }
|
||||
Direct<ExpressionNode> children() const { return Direct<ExpressionNode>(this); }
|
||||
|
||||
/* SerializationHelperInterface */
|
||||
SerializationHelperInterface * serializableChildAtIndex(int i) const override { return childAtIndex(i); }
|
||||
int numberOfSerializableChildren() const override { return numberOfChildren(); }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
Type type() const override { return Type::Factorial; }
|
||||
private:
|
||||
// Layout
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override;
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override;
|
||||
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
// Simplication
|
||||
|
||||
@@ -43,7 +43,7 @@ public:
|
||||
|
||||
protected:
|
||||
// SerializationInterface
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override { return true; }
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override { return true; }
|
||||
// LayoutNode
|
||||
KDSize computeSize() override;
|
||||
KDCoordinate computeBaseline() override;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define POINCARE_LAYOUT_NODE_H
|
||||
|
||||
#include <poincare/tree_node.h>
|
||||
#include <poincare/serialization_helper_interface.h>
|
||||
#include <kandinsky.h>
|
||||
#include <ion/charset.h>
|
||||
|
||||
@@ -11,7 +10,7 @@ namespace Poincare {
|
||||
class LayoutCursor;
|
||||
class Layout;
|
||||
|
||||
class LayoutNode : public TreeNode, public SerializationHelperInterface {
|
||||
class LayoutNode : public TreeNode {
|
||||
friend class Layout;
|
||||
public:
|
||||
enum class VerticalDirection {
|
||||
@@ -39,10 +38,6 @@ public:
|
||||
//TODO: invalid cache when tempering with hierarchy
|
||||
virtual void invalidAllSizesPositionsAndBaselines();
|
||||
|
||||
// SerializationHelperInterface
|
||||
SerializationHelperInterface * serializableChildAtIndex(int i) const override { return childAtIndex(i); }
|
||||
int numberOfSerializableChildren() const override { return numberOfChildren(); }
|
||||
|
||||
// Tree
|
||||
LayoutNode * parent() const override { return static_cast<LayoutNode *>(TreeNode::parent()); }
|
||||
LayoutNode * childAtIndex(int i) const override { return static_cast<LayoutNode *>(TreeNode::childAtIndex(i)); }
|
||||
|
||||
@@ -37,7 +37,7 @@ private:
|
||||
Expression setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) override;
|
||||
|
||||
// Layout
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override;
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override;
|
||||
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
|
||||
// Serialize
|
||||
|
||||
@@ -36,7 +36,7 @@ public:
|
||||
}
|
||||
|
||||
// Layout
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override;
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override;
|
||||
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const override;
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ private:
|
||||
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
|
||||
// Serialize
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override;
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override;
|
||||
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
|
||||
return SerializationHelper::Infix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, name());
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#ifndef POINCARE_SERIALIZATION_HELPER_H
|
||||
#define POINCARE_SERIALIZATION_HELPER_H
|
||||
|
||||
#include <poincare/serialization_helper_interface.h>
|
||||
#include <poincare/tree_node.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
namespace SerializationHelper {
|
||||
/* SerializableReference to Text */
|
||||
int Infix(
|
||||
const SerializationHelperInterface * interface,
|
||||
const TreeNode * node,
|
||||
char * buffer,
|
||||
int bufferSize,
|
||||
Preferences::PrintFloatMode floatDisplayMode,
|
||||
@@ -18,7 +18,7 @@ namespace SerializationHelper {
|
||||
int lastChildIndex = -1);
|
||||
|
||||
int Prefix(
|
||||
const SerializationHelperInterface * interface,
|
||||
const TreeNode * node,
|
||||
char * buffer,
|
||||
int bufferSize,
|
||||
Preferences::PrintFloatMode floatDisplayMode,
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#ifndef POINCARE_SERIALIZATION_HELPER_INTERFACE_H
|
||||
#define POINCARE_SERIALIZATION_HELPER_INTERFACE_H
|
||||
|
||||
#include <poincare/preferences.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
class SerializationHelperInterface {
|
||||
public:
|
||||
virtual int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const = 0;
|
||||
virtual bool childNeedsParenthesis(const SerializationHelperInterface * child) const { return false; };
|
||||
|
||||
virtual SerializationHelperInterface * serializableChildAtIndex(int i) const = 0;
|
||||
virtual int numberOfSerializableChildren() const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
}
|
||||
|
||||
/* Layout */
|
||||
bool childNeedsParenthesis(const SerializationHelperInterface * child) const override;
|
||||
bool childNeedsParenthesis(const TreeNode * child) const override;
|
||||
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#if POINCARE_TREE_LOG
|
||||
#include <ostream>
|
||||
#endif
|
||||
#include <poincare/preferences.h>
|
||||
|
||||
/* What's in a TreeNode, really?
|
||||
* - a vtable pointer
|
||||
@@ -62,6 +63,10 @@ public:
|
||||
// AddChild collateral effect
|
||||
virtual void didAddChildAtIndex(int newNumberOfChildren) {}
|
||||
|
||||
// Serialization
|
||||
virtual int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const { assert(false); return 0; }
|
||||
virtual bool childNeedsParenthesis(const TreeNode * child) const { return false; };
|
||||
|
||||
template <typename T>
|
||||
class Iterator {
|
||||
public:
|
||||
|
||||
@@ -28,7 +28,7 @@ int AdditionNode::getPolynomialCoefficients(char symbolName, Expression coeffici
|
||||
// Private
|
||||
|
||||
// Layout
|
||||
bool AdditionNode::childNeedsParenthesis(const SerializationHelperInterface * child) const {
|
||||
bool AdditionNode::childNeedsParenthesis(const TreeNode * child) const {
|
||||
if ((static_cast<const ExpressionNode *>(child)->isNumber() && static_cast<const ExpressionNode *>(child)->sign() == Sign::Negative) || static_cast<const ExpressionNode *>(child)->type() == Type::Opposite) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ int DivisionNode::polynomialDegree(char symbolName) const {
|
||||
return childAtIndex(0)->polynomialDegree(symbolName);
|
||||
}
|
||||
|
||||
bool DivisionNode::childNeedsParenthesis(const SerializationHelperInterface * child) const {
|
||||
bool DivisionNode::childNeedsParenthesis(const TreeNode * child) const {
|
||||
if (static_cast<const ExpressionNode *>(child)->isNumber() && static_cast<const ExpressionNode *>(child)->sign() == Sign::Negative) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Poincare {
|
||||
|
||||
// Layout
|
||||
|
||||
bool FactorialNode::childNeedsParenthesis(const SerializationHelperInterface * child) const {
|
||||
bool FactorialNode::childNeedsParenthesis(const TreeNode * child) const {
|
||||
if (static_cast<const ExpressionNode *>(child)->isNumber() && static_cast<const ExpressionNode *>(child)->sign() == Sign::Negative) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ Expression MultiplicationNode::setSign(Sign s, Context & context, Preferences::A
|
||||
return Multiplication(this).setSign(s, context, angleUnit);
|
||||
}
|
||||
|
||||
bool MultiplicationNode::childNeedsParenthesis(const SerializationHelperInterface * child) const {
|
||||
bool MultiplicationNode::childNeedsParenthesis(const TreeNode * child) const {
|
||||
if (static_cast<const ExpressionNode *>(child)->isNumber() && static_cast<const ExpressionNode *>(child)->sign() == Sign::Negative) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ ExpressionNode::Sign OppositeNode::sign() const {
|
||||
|
||||
/* Layout */
|
||||
|
||||
bool OppositeNode::childNeedsParenthesis(const SerializationHelperInterface * child) const {
|
||||
bool OppositeNode::childNeedsParenthesis(const TreeNode * child) const {
|
||||
if (static_cast<const ExpressionNode *>(child)->isNumber() && static_cast<const ExpressionNode *>(child)->sign() == Sign::Negative) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ Layout PowerNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int
|
||||
|
||||
// Serialize
|
||||
|
||||
bool PowerNode::childNeedsParenthesis(const SerializationHelperInterface * child) const {
|
||||
bool PowerNode::childNeedsParenthesis(const TreeNode * child) const {
|
||||
if (static_cast<const ExpressionNode *>(child)->isNumber() && static_cast<const ExpressionNode *>(child)->sign() == Sign::Negative) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
static void serializeChild(const SerializationHelperInterface * childInterface, const SerializationHelperInterface * interface, char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfDigits, int * numberOfChar) {
|
||||
static void serializeChild(const TreeNode * childNode, const TreeNode * parentNode, char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfDigits, int * numberOfChar) {
|
||||
// Write the child with parentheses if needed
|
||||
bool addParentheses = interface->childNeedsParenthesis(childInterface);
|
||||
bool addParentheses = parentNode->childNeedsParenthesis(childNode);
|
||||
if (addParentheses) {
|
||||
buffer[*numberOfChar] = '(';
|
||||
*numberOfChar = *numberOfChar + 1;
|
||||
@@ -14,7 +14,7 @@ static void serializeChild(const SerializationHelperInterface * childInterface,
|
||||
return;
|
||||
}
|
||||
}
|
||||
*numberOfChar += childInterface->serialize(buffer + *numberOfChar, bufferSize - *numberOfChar, floatDisplayMode, numberOfDigits);
|
||||
*numberOfChar += childNode->serialize(buffer + *numberOfChar, bufferSize - *numberOfChar, floatDisplayMode, numberOfDigits);
|
||||
if (*numberOfChar >= bufferSize-1) {
|
||||
return;
|
||||
}
|
||||
@@ -25,7 +25,7 @@ static void serializeChild(const SerializationHelperInterface * childInterface,
|
||||
}
|
||||
|
||||
int SerializationHelper::Infix(
|
||||
const SerializationHelperInterface * interface,
|
||||
const TreeNode * node,
|
||||
char * buffer,
|
||||
int bufferSize,
|
||||
Preferences::PrintFloatMode floatDisplayMode,
|
||||
@@ -44,13 +44,13 @@ int SerializationHelper::Infix(
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get some information on the interface
|
||||
// Get some information on the node
|
||||
int numberOfChar = 0;
|
||||
int numberOfChildren = interface->numberOfSerializableChildren();
|
||||
int numberOfChildren = node->numberOfChildren();
|
||||
assert(numberOfChildren > 0);
|
||||
|
||||
// Write the first child, with parentheses if needed
|
||||
serializeChild(interface->serializableChildAtIndex(firstChildIndex), interface, buffer, bufferSize, floatDisplayMode, numberOfDigits, &numberOfChar);
|
||||
serializeChild(node->childAtIndex(firstChildIndex), node, buffer, bufferSize, floatDisplayMode, numberOfDigits, &numberOfChar);
|
||||
if (numberOfChar >= bufferSize-1) {
|
||||
return bufferSize-1;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ int SerializationHelper::Infix(
|
||||
return bufferSize-1;
|
||||
}
|
||||
// Write the child, with parentheses if needed
|
||||
serializeChild(interface->serializableChildAtIndex(i), interface, buffer, bufferSize, floatDisplayMode, numberOfDigits, &numberOfChar);
|
||||
serializeChild(node->childAtIndex(i), node, buffer, bufferSize, floatDisplayMode, numberOfDigits, &numberOfChar);
|
||||
if (numberOfChar >= bufferSize-1) {
|
||||
return bufferSize-1;
|
||||
}
|
||||
@@ -75,7 +75,7 @@ int SerializationHelper::Infix(
|
||||
}
|
||||
|
||||
int SerializationHelper::Prefix(
|
||||
const SerializationHelperInterface * interface,
|
||||
const TreeNode * node,
|
||||
char * buffer,
|
||||
int bufferSize,
|
||||
Preferences::PrintFloatMode floatDisplayMode,
|
||||
@@ -104,7 +104,7 @@ int SerializationHelper::Prefix(
|
||||
return bufferSize-1;
|
||||
}
|
||||
|
||||
int childrenCount = interface->numberOfSerializableChildren();
|
||||
int childrenCount = node->numberOfChildren();
|
||||
if (childrenCount > 0) {
|
||||
if (!writeFirstChild) {
|
||||
assert(childrenCount > 1);
|
||||
@@ -112,7 +112,7 @@ int SerializationHelper::Prefix(
|
||||
int firstChildIndex = writeFirstChild ? 0 : 1;
|
||||
|
||||
// Write the first child
|
||||
numberOfChar += interface->serializableChildAtIndex(firstChildIndex)->serialize(buffer+numberOfChar, bufferSize-numberOfChar, floatDisplayMode, numberOfDigits);
|
||||
numberOfChar += node->childAtIndex(firstChildIndex)->serialize(buffer+numberOfChar, bufferSize-numberOfChar, floatDisplayMode, numberOfDigits);
|
||||
if (numberOfChar >= bufferSize-1) {
|
||||
return bufferSize-1;
|
||||
}
|
||||
@@ -123,7 +123,7 @@ int SerializationHelper::Prefix(
|
||||
if (numberOfChar >= bufferSize-1) {
|
||||
return bufferSize-1;
|
||||
}
|
||||
numberOfChar += interface->serializableChildAtIndex(i)->serialize(buffer+numberOfChar, bufferSize-numberOfChar, floatDisplayMode, numberOfDigits);
|
||||
numberOfChar += node->childAtIndex(i)->serialize(buffer+numberOfChar, bufferSize-numberOfChar, floatDisplayMode, numberOfDigits);
|
||||
if (numberOfChar >= bufferSize-1) {
|
||||
return bufferSize-1;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ int SubtractionNode::polynomialDegree(char symbolName) const {
|
||||
|
||||
// Private
|
||||
|
||||
bool SubtractionNode::childNeedsParenthesis(const SerializationHelperInterface * child) const {
|
||||
bool SubtractionNode::childNeedsParenthesis(const TreeNode * child) const {
|
||||
if (static_cast<const ExpressionNode *>(child)->isNumber() && static_cast<const ExpressionNode *>(child)->sign() == Sign::Negative) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user