[poincare] Factorize Builders of SymbolAbstract classes

This commit is contained in:
Émilie Feral
2019-02-19 14:51:33 +01:00
committed by LeaNumworks
parent 3adbca88d8
commit d29885028b
7 changed files with 27 additions and 29 deletions

View File

@@ -55,7 +55,7 @@ private:
class Constant final : public SymbolAbstract {
public:
Constant(const ConstantNode * node) : SymbolAbstract(node) {}
static Constant Builder(char name);
static Constant Builder(char name) { return SymbolAbstract::Builder<Constant, ConstantNode>(&name, 1); }
// Constant properties
bool isPi() const { return node()->isPi(); }

View File

@@ -64,8 +64,8 @@ public:
UnknownX = 1,
};
Symbol(const SymbolNode * node) : SymbolAbstract(node) {}
static Symbol Builder(const char * name, int length);
static Symbol Builder(char name);
static Symbol Builder(const char * name, int length) { return SymbolAbstract::Builder<Symbol, SymbolNode>(name, length); }
static Symbol Builder(char name) { return Symbol::Builder(&name, 1); }
static Symbol Ans() { return Symbol::Builder(k_ans, k_ansLength); }
static Expression UntypedBuilder(const char * name, size_t length, Context * context) {
// create an expression only if it is not in the context or defined as a symbol

View File

@@ -75,6 +75,9 @@ public:
protected:
SymbolAbstract(const SymbolAbstractNode * node) : Expression(node) {}
template <typename T, typename U>
static T Builder(const char * name, int length);
SymbolAbstractNode * node() const { return static_cast<SymbolAbstractNode *>(Expression::node()); }
private:
static Expression Expand(const SymbolAbstract & symbol, Context & context, bool clone);

View File

@@ -76,14 +76,6 @@ Expression ConstantNode::shallowReduce(Context & context, Preferences::ComplexFo
return Constant(this).shallowReduce(context, complexFormat, angleUnit, target);
}
Constant Constant::Builder(char name) {
size_t size = sizeof(ConstantNode) + 1 + 1;
void * bufferNode = TreePool::sharedPool()->alloc(size);
ConstantNode * node = new (bufferNode) ConstantNode(&name, 1);
TreeHandle h = TreeHandle::BuildWithBasicChildren(node);
return static_cast<Constant &>(h);
}
Expression Constant::shallowReduce(Context & context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, ExpressionNode::ReductionTarget target) {
Expression result;
if (complexFormat == Preferences::ComplexFormat::Real && isIComplex()) {

View File

@@ -92,14 +92,12 @@ Evaluation<T> FunctionNode::templatedApproximate(Context& context, Preferences::
return e.node()->approximate(T(), context, complexFormat, angleUnit);
}
Function Function::Builder(const char * name, size_t length, Expression child) {
size_t size = sizeof(FunctionNode) + length + 1;
void * bufferNode = TreePool::sharedPool()->alloc(size);
FunctionNode * node = new (bufferNode) FunctionNode(name, length);
Expression * childPointer = child.isUninitialized() ? nullptr : &child;
int numberOfChild = child.isUninitialized() ? 0 : 1;
TreeHandle h = TreeHandle::BuildWithBasicChildren(node, childPointer, numberOfChild);
return static_cast<Function &>(h);
Function Function::Builder(const char * name, size_t length, Expression child) {
Function f = SymbolAbstract::Builder<Function, FunctionNode>(name, length);
if (!child.isUninitialized()) {
f.replaceChildAtIndexInPlace(0, child);
}
return f;
}
Expression Function::replaceSymbolWithExpression(const SymbolAbstract & symbol, const Expression & expression) {

View File

@@ -139,16 +139,6 @@ Evaluation<T> SymbolNode::templatedApproximate(Context& context, Preferences::Co
return e.node()->approximate(T(), context, complexFormat, angleUnit);
}
Symbol Symbol::Builder(const char * name, int length) {
size_t size = sizeof(SymbolNode) + length + 1;
void * bufferNode = TreePool::sharedPool()->alloc(size);
SymbolNode * node = new (bufferNode) SymbolNode(name, length);
TreeHandle h = TreeHandle::BuildWithBasicChildren(node);
return static_cast<Symbol &>(h);
}
Symbol Symbol::Builder(char name) { return Symbol::Builder(&name, 1); }
bool Symbol::isSeriesSymbol(const char * c) {
// [NV][1-3]
if (c[2] == 0 && (c[0] == 'N' || c[0] == 'V') && c[1] >= '1' && c[1] <= '3') {

View File

@@ -1,5 +1,7 @@
#include <poincare/symbol_abstract.h>
#include <poincare/complex_cartesian.h>
#include <poincare/constant.h>
#include <poincare/function.h>
#include <poincare/rational.h>
#include <poincare/symbol.h>
#include <poincare/expression.h>
@@ -34,6 +36,15 @@ int SymbolAbstractNode::simplificationOrderSameType(const ExpressionNode * e, bo
return strcmp(name(), static_cast<const SymbolAbstractNode *>(e)->name());
}
template <typename T, typename U>
T SymbolAbstract::Builder(const char * name, int length) {
size_t size = sizeof(U) + length + 1;
void * bufferNode = TreePool::sharedPool()->alloc(size);
U * node = new (bufferNode) U(name, length);
TreeHandle h = TreeHandle::BuildWithBasicChildren(node);
return static_cast<T &>(h);
}
size_t SymbolAbstract::TruncateExtension(char * dst, const char * src, size_t len) {
const char * cur = src;
const char * end = src+len-1;
@@ -71,4 +82,8 @@ bool SymbolAbstract::isReal(const SymbolAbstract & symbol, Context & context) {
return e.isReal(context);
}
template Constant SymbolAbstract::Builder<Constant, ConstantNode>(char const*, int);
template Function SymbolAbstract::Builder<Function, FunctionNode>(char const*, int);
template Symbol SymbolAbstract::Builder<Symbol, SymbolNode>(char const*, int);
}