From dbdd2744cef8559aaa1522c78ccb395c31780ecc Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Fri, 29 Sep 2017 21:13:10 +0200 Subject: [PATCH] [poincare] Add Hierarchy::detachOperand to detach a single operand --- poincare/include/poincare/hierarchy.h | 3 +++ poincare/src/hierarchy.cpp | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/poincare/include/poincare/hierarchy.h b/poincare/include/poincare/hierarchy.h index 9d7b0338b..e3a05e370 100644 --- a/poincare/include/poincare/hierarchy.h +++ b/poincare/include/poincare/hierarchy.h @@ -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); }; } diff --git a/poincare/src/hierarchy.cpp b/poincare/src/hierarchy.cpp index 40f728a13..3dbab0b31 100644 --- a/poincare/src/hierarchy.cpp +++ b/poincare/src/hierarchy.cpp @@ -21,14 +21,17 @@ void Hierarchy::swapOperands(int i, int j) { op[j] = temp; } -void Hierarchy::detachOperands() { - Expression ** op = const_cast(operands()); +void Hierarchy::detachOperand(const Expression * e) { for (int i=0; iparent() == this) { - const_cast(op[i])->setParent(nullptr); + if (operand(i) == e) { + detachOperandAtIndex(i); } - op[i] = nullptr; + } +} + +void Hierarchy::detachOperands() { + for (int i=0; i(operands()); + // When detachOperands is called, it's very likely that said operands have been stolen + if (op[i]->parent() == this) { + const_cast(op[i])->setParent(nullptr); + } + op[i] = nullptr; +} + }