[poincare] Add a shortcut constructor in Hierarchy

Change-Id: I2b0de8ab43ae6a0503814a48a20991b4c92572ea
This commit is contained in:
Émilie Feral
2017-11-02 13:24:02 +01:00
parent 15273979aa
commit 86c5d29cb0
5 changed files with 18 additions and 0 deletions

View File

@@ -10,6 +10,8 @@ class DynamicHierarchy : public Hierarchy {
public:
DynamicHierarchy();
DynamicHierarchy(const Expression * const * operands, int numberOfOperands, bool cloneOperands = true);
DynamicHierarchy(const Expression * operand1, const Expression * operand2, bool cloneOperands = true) :
DynamicHierarchy(ExpressionArray(operand1, operand2), 2, cloneOperands) {}
~DynamicHierarchy();
DynamicHierarchy(const DynamicHierarchy & other) = delete;
DynamicHierarchy(DynamicHierarchy && other) = delete;

View File

@@ -14,6 +14,8 @@ public:
void detachOperand(const Expression * e); // Removes an operand WITHOUT deleting it
void detachOperands(); // Removes all operands WITHOUT deleting them
virtual const Expression * const * operands() const = 0;
protected:
static const Expression * const * ExpressionArray(const Expression * e1, const Expression * e2);
private:
void detachOperandAtIndex(int i);
};

View File

@@ -11,6 +11,7 @@ class StaticHierarchy : public Hierarchy {
public:
StaticHierarchy();
StaticHierarchy(const Expression * const * operands, bool cloneOperands = true);
StaticHierarchy(const Expression * expression1, const Expression * expression2, bool cloneOperands = true); // Specialized constructor for StaticHierarchy<2>
~StaticHierarchy();
StaticHierarchy(const StaticHierarchy & other) = delete;
StaticHierarchy(StaticHierarchy && other) = delete;

View File

@@ -59,6 +59,13 @@ void Hierarchy::replaceOperand(const Expression * oldOperand, Expression * newOp
}
}
const Expression * const * Hierarchy::ExpressionArray(const Expression * e1, const Expression * e2) {
static const Expression * result[2] = {nullptr, nullptr};
result[0] = e1;
result[1] = e2;
return result;
}
void Hierarchy::detachOperandAtIndex(int i) {
Expression ** op = const_cast<Expression **>(operands());
// When detachOperands is called, it's very likely that said operands have been stolen

View File

@@ -19,6 +19,12 @@ StaticHierarchy<T>::StaticHierarchy(const Expression * const * operands, bool cl
build(operands, T, cloneOperands);
}
template<>
StaticHierarchy<2>::StaticHierarchy(const Expression * e1, const Expression * e2, bool cloneOperands) :
StaticHierarchy(ExpressionArray(e1, e2), cloneOperands)
{
}
template<int T>
StaticHierarchy<T>::~StaticHierarchy() {
for (int i = 0; i < T; i++) {