Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unique_ptr for Grid in Cell #782

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Cell {
int tys {0};
int celltype;
Text text;
Grid *grid;
unique_ptr<Grid> grid;
uint cellcolor {0xFFFFFF};
uint textcolor {0x000000};
uint actualcellcolor {0xFFFFFF};
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<Grid>(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<Grid>(xs, dis.Read32());
grid->cell = this;
if (!grid->LoadContents(dis, numcells, textbytes, ics)) return nullptr;
return this;
}

Expand Down Expand Up @@ -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.
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cell> 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);
Expand All @@ -118,7 +118,7 @@ struct Evaluator {

unique_ptr<Cell> Execute(const Operation *op, unique_ptr<Cell> left) {
Text &t = left->text;
Grid *g = left->grid;
Grid *g = left->grid.get();
switch (op->args[0]) {
case 'n':
if (t.t.Len()) {
Expand Down Expand Up @@ -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()) {
Expand Down
14 changes: 6 additions & 8 deletions src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Grid>(1, 1);
f->grid->cell = f;
*f->grid->cells = t;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}

Expand All @@ -1045,7 +1043,7 @@ struct Grid {
}
}
if (rest) {
swap_(c->grid, rest->grid);
c->grid.swap(rest->grid);
c->grid->ReParent(c);
}
done:;
Expand Down
2 changes: 1 addition & 1 deletion src/selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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',');
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/treesheets_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading