From 9fe088af93baae54a11d5055536196954428c70f Mon Sep 17 00:00:00 2001 From: Tobias Predel Date: Tue, 10 Dec 2024 22:09:24 +0100 Subject: [PATCH] Use unique_ptr for Grid in Cell (#782) Make it clear that Cell owns Grid `grid`. --- src/cell.h | 26 ++++++++++++-------------- src/document.h | 24 ++++++++++++------------ src/evaluator.h | 10 +++++----- src/grid.h | 14 ++++++-------- src/selection.h | 2 +- src/system.h | 4 ++-- src/treesheets_impl.h | 2 +- 7 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/cell.h b/src/cell.h index 00969928..0d8b7546 100644 --- a/src/cell.h +++ b/src/cell.h @@ -33,7 +33,7 @@ struct Cell { int tys {0}; int celltype; Text text; - Grid *grid; + unique_ptr grid; uint cellcolor {0xFFFFFF}; uint textcolor {0x000000}; uint actualcellcolor {0xFFFFFF}; @@ -53,9 +53,8 @@ struct Cell { if (_clonefrom) CloneStyleFrom(_clonefrom); } - ~Cell() { DELETEP(grid); } void Clear() { - DELETEP(grid); + grid.release(); text.t.Clear(); text.image = nullptr; Reset(); @@ -196,7 +195,7 @@ struct Cell { grid ? new Grid(grid->xs, grid->ys) : nullptr); c->text = text; c->text.cell = c.get(); - if (grid) { grid->Clone(c->grid); } + if (grid) { grid->Clone(c->grid.get()); } return c; } @@ -355,19 +354,18 @@ struct Cell { Grid *AddGrid(int x = 1, int y = 1) { if (!grid) { - grid = new Grid(x, y, this); + grid = make_unique(x, y, this); grid->InitCells(this); - if (parent) grid->CloneStyleFrom(parent->grid); + if (parent) grid->CloneStyleFrom(parent->grid.get()); } - return grid; + return grid.get(); } Cell *LoadGrid(wxDataInputStream &dis, int &numcells, int &textbytes, Cell *&ics) { int xs = dis.Read32(); - Grid *g = new Grid(xs, dis.Read32()); - grid = g; - g->cell = this; - if (!g->LoadContents(dis, numcells, textbytes, ics)) return nullptr; + grid = make_unique(xs, dis.Read32()); + grid->cell = this; + if (!grid->LoadContents(dis, numcells, textbytes, ics)) return nullptr; return this; } @@ -426,9 +424,9 @@ struct Cell { c->grid->Clone(cg); // Note: deleting grid may invalidate c if its a child of grid, so clear it. c = nullptr; - DELETEP(grid); // FIXME: could merge instead? - grid = cg; - if (!HasText()) grid->MergeWithParent(parent->grid, s, doc); // deletes grid/this. + grid.reset(cg); // FIXME: could merge instead? + if (!HasText()) + grid->MergeWithParent(parent->grid.get(), s, doc); // deletes grid/this. } } diff --git a/src/document.h b/src/document.h index 966775d6..0cc2a94f 100644 --- a/src/document.h +++ b/src/document.h @@ -111,10 +111,10 @@ struct Document { void InitCellSelect(Cell *ics, int xs, int ys) { if (!ics) { - SetSelect(Selection(rootgrid->grid, 0, 0, 1, 1)); + SetSelect(Selection(rootgrid->grid.get(), 0, 0, 1, 1)); return; } - Grid *ipg = ics->parent->grid; + Grid *ipg = ics->parent->grid.get(); foreachcellingrid(c, ipg) { if (c == ics) { SetSelect(Selection(ipg, x, y, xs, ys)); @@ -515,7 +515,8 @@ struct Document { Cell *drawroot = WalkPath(drawpath); if (selected.GetCell() == drawroot && drawroot->grid) { // We can't have the drawroot selected, so we must move the selection to the children. - SetSelect(Selection(drawroot->grid, 0, 0, drawroot->grid->xs, drawroot->grid->ys)); + SetSelect( + Selection(drawroot->grid.get(), 0, 0, drawroot->grid->xs, drawroot->grid->ys)); } drawroot->ResetLayout(); drawroot->ResetChildren(); @@ -1503,12 +1504,12 @@ struct Document { case A_NEWGRID: if (!(c = selected.ThinExpand(this))) return OneCell(); if (c->grid) { - SetSelect(Selection(c->grid, 0, c->grid->ys, 1, 0)); + SetSelect(Selection(c->grid.get(), 0, c->grid->ys, 1, 0)); ScrollOrZoom(dc, true); } else { c->AddUndo(this); c->AddGrid(); - SetSelect(Selection(c->grid, 0, 0, 1, 1)); + SetSelect(Selection(c->grid.get(), 0, 0, 1, 1)); DrawSelectMove(dc, selected, true); } return nullptr; @@ -1883,8 +1884,7 @@ struct Document { Grid *g = new Grid(maxdepth, leaves); g->InitCells(); ac->grid->Flatten(0, 0, g); - DELETEP(ac->grid); - ac->grid = g; + ac->grid.reset(g); g->ReParent(ac); ac->ResetChildren(); ClearSelectionRefresh(); @@ -1901,7 +1901,7 @@ struct Document { case A_ENTERGRID: if (!c->grid) Action(dc, A_NEWGRID); - SetSelect(Selection(c->grid, 0, 0, 1, 1)); + SetSelect(Selection(c->grid.get(), 0, 0, 1, 1)); ScrollOrZoom(dc, true); return nullptr; @@ -2086,10 +2086,10 @@ struct Document { } else { c->parent->AddUndo(this); c->ResetLayout(); - DELETEP(c->grid); + c->grid.release(); sys->FillRows(c->AddGrid(), as, sys->CountCol(as[0]), 0, 0); if (!c->HasText()) - c->grid->MergeWithParent(c->parent->grid, selected, this); + c->grid->MergeWithParent(c->parent->grid.get(), selected, this); } } } @@ -2149,7 +2149,7 @@ struct Document { Cell *c = rootgrid.get(); loopvrev(i, path) { Selection &s = path[i]; - Grid *g = c->grid; + Grid *g = c->grid.get(); if (!g) return c; ASSERT(g && s.x < g->xs && s.y < g->ys); c = g->C(s.x, s.y); @@ -2220,7 +2220,7 @@ struct Document { rootgrid.reset(clone); clone->ResetLayout(); SetSelect(ui->sel); - if (selected.g) selected.g = WalkPath(ui->selpath)->grid; + if (selected.g) selected.g = WalkPath(ui->selpath)->grid.get(); begindrag = selected; ui->sel = beforesel; ui->selpath = std::move(beforepath); diff --git a/src/evaluator.h b/src/evaluator.h index 4cd84f9d..ad7f90f8 100644 --- a/src/evaluator.h +++ b/src/evaluator.h @@ -97,12 +97,12 @@ struct Evaluator { void Assign(const Cell *sym, const Cell *val) { this->SetSymbol(sym->text.t, val->Clone(nullptr)); - if (sym->grid && val->grid) this->DestructuringAssign(sym->grid, val->Clone(nullptr)); + if (sym->grid && val->grid) this->DestructuringAssign(sym->grid.get(), val->Clone(nullptr)); } void DestructuringAssign(Grid const *names, unique_ptr val) { Grid const *ng = names; - Grid const *vg = val->grid; + Grid const *vg = val->grid.get(); if (ng->xs == vg->xs && ng->ys == vg->ys) { loop(x, ng->xs) loop(y, ng->ys) { Cell *nc = ng->C(x, y); @@ -118,7 +118,7 @@ struct Evaluator { unique_ptr Execute(const Operation *op, unique_ptr left) { Text &t = left->text; - Grid *g = left->grid; + Grid *g = left->grid.get(); switch (op->args[0]) { case 'n': if (t.t.Len()) { @@ -167,8 +167,8 @@ struct Evaluator { if (!right) return left; Text &t1 = left->text; Text &t2 = right->text; - Grid *g1 = left->grid; - Grid *g2 = right->grid; + Grid *g1 = left->grid.get(); + Grid *g2 = right->grid.get(); switch (op->args[0]) { case 'n': if (t1.t.Len() && t2.t.Len()) { diff --git a/src/grid.h b/src/grid.h index ca35a316..b7cfd882 100644 --- a/src/grid.h +++ b/src/grid.h @@ -513,8 +513,7 @@ struct Grid { } if (!cell->parent) return; // FIXME: deletion of root cell, what would be better? s = cell->parent->grid->FindCell(cell); - Grid *&pthis = cell->grid; - DELETEP(pthis); + cell->grid.release(); } void InsertCells(int dx, int dy, int nxs, int nys, Cell *nc = nullptr) { @@ -933,9 +932,9 @@ struct Grid { Cell *t = new Cell(f, p); t->text = p->text; t->text.cell = t; - t->grid = f->grid; + t->grid = std::move(f->grid); if (t->grid) t->grid->ReParent(t); - f->grid = new Grid(1, 1); + f->grid = make_unique(1, 1); f->grid->cell = f; *f->grid->cells = t; } @@ -992,9 +991,8 @@ struct Grid { if (c->grid) { f->grid->MergeTagAll(c); } else { - c->grid = f->grid; + c->grid = std::move(f->grid); c->grid->ReParent(c); - f->grid = nullptr; } delete f; } @@ -1033,7 +1031,7 @@ struct Grid { if (prev->text.t == c->text.t) { if (rest) { ASSERT(prev->grid); - prev->grid->MergeRow(rest->grid); + prev->grid->MergeRow(rest->grid.get()); rest.reset(); } @@ -1045,7 +1043,7 @@ struct Grid { } } if (rest) { - swap_(c->grid, rest->grid); + c->grid.swap(rest->grid); c->grid->ReParent(c); } done:; diff --git a/src/selection.h b/src/selection.h index f98bd7ff..b0b03e67 100644 --- a/src/selection.h +++ b/src/selection.h @@ -96,7 +96,7 @@ class Selection { int bd = bt->Depth(); int i = 0; while (i < ad && i < bd && at->Parent(ad - i) == bt->Parent(bd - i)) i++; - Grid *g = at->Parent(ad - i + 1)->grid; + Grid *g = at->Parent(ad - i + 1)->grid.get(); Merge(g->FindCell(at->Parent(ad - i)), g->FindCell(bt->Parent(bd - i))); return; } diff --git a/src/system.h b/src/system.h index 5c361d18..472f851d 100644 --- a/src/system.h +++ b/src/system.h @@ -493,7 +493,7 @@ struct System { if (as.size()) switch (k) { case A_IMPTXTI: { Cell *r = InitDB(1).get(); - FillRows(r->grid, as, CountCol(as[0]), 0, 0); + FillRows(r->grid.get(), as, CountCol(as[0]), 0, 0); }; break; case A_IMPTXTC: InitDB(1, (int)as.size())->grid->CSVImport(as, L','); @@ -608,7 +608,7 @@ struct System { if (col < column && startrow != 0) return i; if (col > column) { Cell *c = g->C(0, y - 1); - Grid *sg = c->grid; + Grid *sg = c->grid.get(); i = FillRows(sg ? sg : c->AddGrid(), as, col, i, sg ? sg->ys : 0) - 1; } else { if (g->ys <= y) g->InsertCells(-1, y, 0, 1); diff --git a/src/treesheets_impl.h b/src/treesheets_impl.h index 6595cf43..b5ec7020 100644 --- a/src/treesheets_impl.h +++ b/src/treesheets_impl.h @@ -95,7 +95,7 @@ struct TreeSheetsScriptImpl : public ScriptInterface { void Delete(int x, int y, int xs, int ys) { if (cur->grid && x >= 0 && x + xs <= cur->grid->xs && y >= 0 && y + ys <= cur->grid->ys) { - Selection s(cur->grid, x, y, xs, ys); + Selection s(cur->grid.get(), x, y, xs, ys); cur->grid->MultiCellDeleteSub(doc, s); doc->SetSelect(Selection()); doc->Zoom(-100, *dc);