[poincare] Add Hierarchy::detachOperand to detach a single operand

This commit is contained in:
Romain Goyet
2017-09-29 21:13:10 +02:00
parent 9587447b19
commit dbdd2744ce
2 changed files with 21 additions and 6 deletions

View File

@@ -11,8 +11,11 @@ public:
const Expression * operand(int i) const override;
void swapOperands(int i, int j) override;
void replaceOperand(const Expression * oldOperand, Expression * newOperand, bool deleteOldOperand = true) override;
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;
private:
void detachOperandAtIndex(int i);
};
}

View File

@@ -21,14 +21,17 @@ void Hierarchy::swapOperands(int i, int j) {
op[j] = temp;
}
void Hierarchy::detachOperands() {
Expression ** op = const_cast<Expression **>(operands());
void Hierarchy::detachOperand(const Expression * e) {
for (int i=0; i<numberOfOperands(); i++) {
// When detachOperands is called, it's very likely that said operands have been stolen
if (op[i]->parent() == this) {
const_cast<Expression *>(op[i])->setParent(nullptr);
if (operand(i) == e) {
detachOperandAtIndex(i);
}
op[i] = nullptr;
}
}
void Hierarchy::detachOperands() {
for (int i=0; i<numberOfOperands(); i++) {
detachOperandAtIndex(i);
}
}
@@ -51,4 +54,13 @@ void Hierarchy::replaceOperand(const Expression * oldOperand, Expression * newOp
}
}
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
if (op[i]->parent() == this) {
const_cast<Expression *>(op[i])->setParent(nullptr);
}
op[i] = nullptr;
}
}