From f8edf8abe4d7f18528cec6460831d34ee30881e3 Mon Sep 17 00:00:00 2001 From: Tobias Predel Date: Thu, 16 Jan 2025 20:55:25 +0100 Subject: [PATCH] Revert dynamic datastructures (#804) * Revert "Avoid double ownership (#800)" This reverts commit 89b40de858f598975098a0436637bca8357a4a86. * Revert "Fix bad unique_ptr usage" This reverts commit 2086ce8e571ad816dcce8dabf402e2de99fa2134. * Revert "Use correct method to delete resource (#783)" This reverts commit 81e74a0f3f325ada0abe35211e90b74132f91470. * Revert "Use unique_ptr for Grid in Cell (#782)" This reverts commit 9fe088af93baae54a11d5055536196954428c70f. * Revert "Make rootgrid an unique_ptr (#776)" This reverts commit 15d5f583ec24634baa5074428cf825d3ce9c1ce4. * Revert "Use vector instead of raw pointer arrays (#785)" This reverts commit 9dfc4fb8e1cf4efe16228ebfb76785a574c017d2. * Revert "Use unique_ptr for Document in TSCanvas (#780)" This reverts commit e3d46028c2e9a731024ea61221f950349a6a226f. --- src/cell.h | 26 ++++----- src/document.h | 57 ++++++++++---------- src/evaluator.h | 10 ++-- src/grid.h | 123 +++++++++++++++++++++--------------------- src/mycanvas.h | 7 ++- src/myframe.h | 10 ++-- src/mywxtools.h | 2 +- src/selection.h | 2 +- src/system.h | 17 +++--- src/treesheets_impl.h | 9 ++-- 10 files changed, 134 insertions(+), 129 deletions(-) diff --git a/src/cell.h b/src/cell.h index 46bba510..00969928 100644 --- a/src/cell.h +++ b/src/cell.h @@ -33,7 +33,7 @@ struct Cell { int tys {0}; int celltype; Text text; - unique_ptr grid; + Grid *grid; uint cellcolor {0xFFFFFF}; uint textcolor {0x000000}; uint actualcellcolor {0xFFFFFF}; @@ -53,8 +53,9 @@ struct Cell { if (_clonefrom) CloneStyleFrom(_clonefrom); } + ~Cell() { DELETEP(grid); } void Clear() { - grid.reset(); + DELETEP(grid); text.t.Clear(); text.image = nullptr; Reset(); @@ -195,7 +196,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.get()); } + if (grid) { grid->Clone(c->grid); } return c; } @@ -354,18 +355,19 @@ struct Cell { Grid *AddGrid(int x = 1, int y = 1) { if (!grid) { - grid = make_unique(x, y, this); + grid = new Grid(x, y, this); grid->InitCells(this); - if (parent) grid->CloneStyleFrom(parent->grid.get()); + if (parent) grid->CloneStyleFrom(parent->grid); } - return grid.get(); + return grid; } Cell *LoadGrid(wxDataInputStream &dis, int &numcells, int &textbytes, Cell *&ics) { int xs = dis.Read32(); - grid = make_unique(xs, dis.Read32()); - grid->cell = this; - if (!grid->LoadContents(dis, numcells, textbytes, ics)) return nullptr; + Grid *g = new Grid(xs, dis.Read32()); + grid = g; + g->cell = this; + if (!g->LoadContents(dis, numcells, textbytes, ics)) return nullptr; return this; } @@ -424,9 +426,9 @@ struct Cell { c->grid->Clone(cg); // Note: deleting grid may invalidate c if its a child of grid, so clear it. c = nullptr; - grid.reset(cg); // FIXME: could merge instead? - if (!HasText()) - grid->MergeWithParent(parent->grid.get(), s, doc); // deletes grid/this. + DELETEP(grid); // FIXME: could merge instead? + grid = cg; + if (!HasText()) grid->MergeWithParent(parent->grid, s, doc); // deletes grid/this. } } diff --git a/src/document.h b/src/document.h index 713407a1..aea2328d 100644 --- a/src/document.h +++ b/src/document.h @@ -13,7 +13,7 @@ struct UndoItem { struct Document { TSCanvas *sw {nullptr}; - unique_ptr rootgrid; + Cell *rootgrid {nullptr}; Selection hover; Selection selected; Selection begindrag; @@ -91,7 +91,7 @@ struct Document { CollectCells(par); \ loopv(_i, itercells) for (Cell *c = itercells[_i]; c; c = nullptr) #define loopallcells(c) \ - CollectCells(rootgrid.get()); \ + CollectCells(rootgrid); \ for (Cell *c : itercells) #define loopallcellssel(c, rec) \ CollectCellsSel(rec); \ @@ -107,11 +107,13 @@ struct Document { dndobjc->Add(dndobjf); } + ~Document() { DELETEP(rootgrid); } + uint Background() { return rootgrid ? rootgrid->cellcolor : 0xFFFFFF; } void InitCellSelect(Cell *ics, int xs, int ys) { if (!ics) { - SetSelect(Selection(rootgrid->grid.get(), 0, 0, 1, 1)); + SetSelect(Selection(rootgrid->grid, 0, 0, 1, 1)); return; } SetSelect(ics->parent->grid->FindCell(ics)); @@ -120,7 +122,7 @@ struct Document { } void InitWith(Cell *r, const wxString &filename, Cell *ics, int xs, int ys) { - rootgrid.reset(r); + rootgrid = r; InitCellSelect(ics, xs, ys); ChangeFileName(filename, false); } @@ -510,8 +512,7 @@ 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.get(), 0, 0, drawroot->grid->xs, drawroot->grid->ys)); + SetSelect(Selection(drawroot->grid, 0, 0, drawroot->grid->xs, drawroot->grid->ys)); } drawroot->ResetLayout(); drawroot->ResetChildren(); @@ -565,7 +566,7 @@ struct Document { ResetFont(); dc.SetUserScale(1, 1); curdrawroot = WalkPath(drawpath); - int psb = curdrawroot == rootgrid.get() ? 0 : curdrawroot->MinRelsize(); + int psb = curdrawroot == rootgrid ? 0 : curdrawroot->MinRelsize(); if (psb < 0 || psb == INT_MAX) psb = 0; if (psb != pathscalebias) curdrawroot->ResetChildren(); pathscalebias = psb; @@ -808,7 +809,7 @@ struct Document { } const wxChar *ExportFile(const wxString &fn, int k, bool currentview) { - auto root = currentview ? curdrawroot : rootgrid.get(); + auto root = currentview ? curdrawroot : rootgrid; if (k == A_EXPCSV) { int maxdepth = 0, leaves = 0; root->MaxDepthLeaves(0, maxdepth, leaves); @@ -973,7 +974,7 @@ struct Document { switch (k) { case wxID_EXECUTE: - sys->ev.Eval(rootgrid.get()); + sys->ev.Eval(rootgrid); rootgrid->ResetChildren(); ClearSelectionRefresh(); return _(L"Evaluation finished."); @@ -1499,12 +1500,12 @@ struct Document { case A_NEWGRID: if (!(c = selected.ThinExpand(this))) return OneCell(); if (c->grid) { - SetSelect(Selection(c->grid.get(), 0, c->grid->ys, 1, 0)); + SetSelect(Selection(c->grid, 0, c->grid->ys, 1, 0)); ScrollOrZoom(dc, true); } else { c->AddUndo(this); c->AddGrid(); - SetSelect(Selection(c->grid.get(), 0, 0, 1, 1)); + SetSelect(Selection(c->grid, 0, 0, 1, 1)); DrawSelectMove(dc, selected, true); } return nullptr; @@ -1879,7 +1880,8 @@ struct Document { Grid *g = new Grid(maxdepth, leaves); g->InitCells(); ac->grid->Flatten(0, 0, g); - ac->grid.reset(g); + DELETEP(ac->grid); + ac->grid = g; g->ReParent(ac); ac->ResetChildren(); ClearSelectionRefresh(); @@ -1896,7 +1898,7 @@ struct Document { case A_ENTERGRID: if (!c->grid) Action(dc, A_NEWGRID); - SetSelect(Selection(c->grid.get(), 0, 0, 1, 1)); + SetSelect(Selection(c->grid, 0, 0, 1, 1)); ScrollOrZoom(dc, true); return nullptr; @@ -2081,10 +2083,10 @@ struct Document { } else { c->parent->AddUndo(this); c->ResetLayout(); - c->grid.release(); + DELETEP(c->grid); sys->FillRows(c->AddGrid(), as, sys->CountCol(as[0]), 0, 0); if (!c->HasText()) - c->grid->MergeWithParent(c->parent->grid.get(), selected, this); + c->grid->MergeWithParent(c->parent->grid, selected, this); } } } @@ -2141,10 +2143,10 @@ struct Document { } Cell *WalkPath(vector &path) { - Cell *c = rootgrid.get(); + Cell *c = rootgrid; loopvrev(i, path) { Selection &s = path[i]; - Grid *g = c->grid.get(); + Grid *g = c->grid; if (!g) return c; ASSERT(g && s.x < g->xs && s.y < g->ys); c = g->C(s.x, s.y); @@ -2205,18 +2207,17 @@ struct Document { if (beforesel.g) CreatePath(beforesel.g->cell, beforepath); unique_ptr ui = std::move(fromlist.back()); fromlist.pop_back(); - if (Cell *c = WalkPath(ui->path); c->parent && c->parent->grid) { - auto clone = ui->clone.release(); + Cell *c = WalkPath(ui->path); + auto clone = ui->clone.release(); + ui->clone.reset(c); + if (c->parent && c->parent->grid) { c->parent->grid->ReplaceCell(c, clone); clone->parent = c->parent; - clone->ResetLayout(); - ui->clone.reset(c); - } else { - rootgrid.swap(ui->clone); - rootgrid->ResetLayout(); - } + } else + rootgrid = clone; + clone->ResetLayout(); SetSelect(ui->sel); - if (selected.g) selected.g = WalkPath(ui->selpath)->grid.get(); + if (selected.g) selected.g = WalkPath(ui->selpath)->grid; begindrag = selected; ui->sel = beforesel; ui->selpath = std::move(beforepath); @@ -2300,7 +2301,7 @@ struct Document { searchfilter = false; scrolltoselection = true; editfilter = min(max(editfilter, 1), 99); - CollectCells(rootgrid.get()); + CollectCells(rootgrid); std::sort(itercells.begin(), itercells.end(), [](Cell *a, Cell *b) { // sort in descending order return a->text.lastedit > b->text.lastedit; @@ -2313,7 +2314,7 @@ struct Document { void ApplyEditRangeFilter(wxDateTime &rangebegin, wxDateTime &rangeend) { searchfilter = false; scrolltoselection = true; - CollectCells(rootgrid.get()); + CollectCells(rootgrid); for (auto *c : itercells) { c->text.filtered = !c->text.lastedit.IsBetween(rangebegin, rangeend); } diff --git a/src/evaluator.h b/src/evaluator.h index ad7f90f8..4cd84f9d 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.get(), val->Clone(nullptr)); + if (sym->grid && val->grid) this->DestructuringAssign(sym->grid, val->Clone(nullptr)); } void DestructuringAssign(Grid const *names, unique_ptr val) { Grid const *ng = names; - Grid const *vg = val->grid.get(); + Grid const *vg = val->grid; 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.get(); + Grid *g = left->grid; 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.get(); - Grid *g2 = right->grid.get(); + Grid *g1 = left->grid; + Grid *g2 = right->grid; switch (op->args[0]) { case 'n': if (t1.t.Len() && t2.t.Len()) { diff --git a/src/grid.h b/src/grid.h index 74300e86..69bddf1f 100644 --- a/src/grid.h +++ b/src/grid.h @@ -3,7 +3,7 @@ struct Grid { // owning cell. Cell *cell; // subcells - vector cells; + Cell **cells; // widths for each column vector colwidths; // xsize, ysize @@ -18,12 +18,7 @@ struct Grid { bool tinyborder; bool folded {false}; - Cell *C(int x, int y) const { - ASSERT(x >= 0 && y >= 0 && x < xs && y < ys); - return cells[x + y * xs]; - } - - Cell *&C(int x, int y) { + Cell *&C(int x, int y) const { ASSERT(x >= 0 && y >= 0 && x < xs && y < ys); return cells[x + y * xs]; } @@ -33,11 +28,6 @@ struct Grid { for (int x = 0; x < xs; x++) \ for (bool _f = true; _f;) \ for (Cell *&c = C(x, y); _f; _f = false) - #define foreachconstcell(c) \ - for (int y = 0; y < ys; y++) \ - for (int x = 0; x < xs; x++) \ - for (bool _f = true; _f;) \ - for (Cell *c = C(x, y); _f; _f = false) #define foreachcellrev(c) \ for (int y = ys - 1; y >= 0; y--) \ for (int x = xs - 1; x >= 0; x--) \ @@ -68,12 +58,17 @@ struct Grid { for (bool _f = true; _f;) \ for (Cell *&c = g->C(x, y); _f; _f = false) - Grid(int _xs, int _ys, Cell *_c = nullptr) : xs(_xs), ys(_ys), cell(_c), cells(_xs * _ys) { + Grid(int _xs, int _ys, Cell *_c = nullptr) + : xs(_xs), ys(_ys), cell(_c), cells(new Cell *[_xs * _ys]) { + foreachcell(c) c = nullptr; InitColWidths(); SetOrient(); } - ~Grid() { foreachconstcell(c) if (c) delete c; } + ~Grid() { + foreachcell(c) if (c) delete c; + delete[] cells; + } void InitCells(Cell *clonestylefrom = nullptr) { foreachcell(c) c = new Cell(cell, clonestylefrom); @@ -269,7 +264,7 @@ struct Grid { } void FindXY(Document *doc, int px, int py, wxDC &dc) { - foreachconstcell(c) { + foreachcell(c) { int bx = px - c->ox; int by = py - c->oy; if (bx >= 0 && by >= -g_line_width - g_selmargin && bx < c->sx && by < g_selmargin) { @@ -305,8 +300,7 @@ struct Grid { Cell *FindLink(const Selection &s, Cell *link, Cell *best, bool &lastthis, bool &stylematch, bool forward, bool image) { if (forward) { - foreachconstcell(c) best = - c->FindLink(s, link, best, lastthis, stylematch, forward, image); + foreachcell(c) best = c->FindLink(s, link, best, lastthis, stylematch, forward, image); } else { foreachcellrev(c) best = c->FindLink(s, link, best, lastthis, stylematch, forward, image); @@ -320,30 +314,30 @@ struct Grid { foreachcellrev(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected, reverse); } else { - foreachconstcell(c) best = + foreachcell(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected, reverse); } return best; } Cell *FindNextFilterMatch(Cell *best, Cell *selected, bool &lastwasselected) { - foreachconstcell(c) best = c->FindNextFilterMatch(best, selected, lastwasselected); + foreachcell(c) best = c->FindNextFilterMatch(best, selected, lastwasselected); return best; } void FindReplaceAll(const wxString &str, const wxString &lstr) { - foreachconstcell(c) c->FindReplaceAll(str, lstr); + foreachcell(c) c->FindReplaceAll(str, lstr); } void ReplaceCell(Cell *o, Cell *n) { foreachcell(c) if (c == o) c = n; } Selection FindCell(Cell *o) { - foreachconstcell(c) if (c == o) return Selection(this, x, y, 1, 1); + foreachcell(c) if (c == o) return Selection(this, x, y, 1, 1); return Selection(); } Selection SelectAll() { return Selection(this, 0, 0, xs, ys); } void ImageRefCount(bool includefolded) { - if (includefolded || !folded) foreachconstcell(c) c->ImageRefCount(includefolded); + if (includefolded || !folded) foreachcell(c) c->ImageRefCount(includefolded); } void DrawHover(Document *doc, wxDC &dc, Selection &s) { #ifndef SIMPLERENDER @@ -464,10 +458,11 @@ struct Grid { } void DeleteCells(int dx, int dy, int nxs, int nys) { - vector ncells((xs + nxs) * (ys + nys)); - auto nit = ncells.begin(); - foreachcell(c) if (x == dx || y == dy) DELETEP(c) else *nit++ = c; - cells = std::move(ncells); + Cell **ncells = new Cell *[(xs + nxs) * (ys + nys)]; + Cell **ncp = ncells; + foreachcell(c) if (x == dx || y == dy) DELETEP(c) else *ncp++ = c; + delete[] cells; + cells = ncells; xs += nxs; ys += nys; if (dx >= 0) colwidths.erase(colwidths.begin() + dx); @@ -483,7 +478,7 @@ struct Grid { void MultiCellDeleteSub(Document *doc, Selection &s) { foreachcellinsel(c, s) c->Clear(); bool delhoriz = true, delvert = true; - foreachconstcell(c) { + foreachcell(c) { if (c->HasContent()) { if (y >= s.y && y < s.y + s.ys) delhoriz = false; if (x >= s.x && x < s.x + s.xs) delvert = false; @@ -518,17 +513,18 @@ struct Grid { } if (!cell->parent) return; // FIXME: deletion of root cell, what would be better? s = cell->parent->grid->FindCell(cell); - cell->grid.reset(); + Grid *&pthis = cell->grid; + DELETEP(pthis); } void InsertCells(int dx, int dy, int nxs, int nys, Cell *nc = nullptr) { assert(((dx < 0) == (nxs == 0)) && ((dy < 0) == (nys == 0))); assert(nxs + nys == 1); - vector ocells = std::move(cells); - cells = vector((xs + nxs) * (ys + nys)); + Cell **ocells = cells; + cells = new Cell *[(xs + nxs) * (ys + nys)]; xs += nxs; ys += nys; - auto oit = ocells.begin(); + Cell **ncp = ocells; SetOrient(); foreachcell(c) if (x == dx || y == dy) { if (nc) @@ -540,7 +536,8 @@ struct Grid { c->text.relsize = colcell->text.relsize; } } - else c = *oit++; + else c = *ncp++; + delete[] ocells; if (dx >= 0) colwidths.insert(colwidths.begin() + dx, cell->ColWidth()); } @@ -552,7 +549,7 @@ struct Grid { dos.Write8(cell->verticaltextandgrid); dos.Write8(folded); loop(x, xs) dos.Write32(colwidths[x]); - foreachconstcell(c) c->Save(dos, ocs); + foreachcell(c) c->Save(dos, ocs); } bool LoadContents(wxDataInputStream &dis, int &numcells, int &textbytes, Cell *&ics) { @@ -584,7 +581,7 @@ struct Grid { } void GetStats(int &numcells, int &textbytes) { - foreachconstcell(c) c->GetStats(numcells, textbytes); + foreachcell(c) c->GetStats(numcells, textbytes); } void Formatter(wxString &r, int format, int indent, const wxChar *xml, const wxChar *html, @@ -611,7 +608,7 @@ struct Grid { const int root_grid_spacing = 2; // Can't be adjusted in editor, so use a default. const int font_size = 14 - indent / 2; const int grid_border_width = - cell == doc->rootgrid.get() ? root_grid_spacing : user_grid_outer_spacing - 1; + cell == doc->rootgrid ? root_grid_spacing : user_grid_outer_spacing - 1; wxString xmlstr(L"RelSize(dir, zoomdepth); } + void RelSize(int dir, int zoomdepth) { foreachcell(c) c->RelSize(dir, zoomdepth); } void RelSize(int dir, const Selection &s, int zoomdepth) { foreachcellinsel(c, s) c->RelSize(dir, zoomdepth); } void SetBorder(int width, const Selection &s) { foreachcellinsel(c, s) c->SetBorder(width); } int MinRelsize(int rs) { - foreachconstcell(c) { + foreachcell(c) { int crs = c->MinRelsize(); rs = min(rs, crs); } @@ -653,7 +650,7 @@ struct Grid { void ResetChildren() { cell->Reset(); - foreachconstcell(c) c->ResetChildren(); + foreachcell(c) c->ResetChildren(); } void Move(int dx, int dy, const Selection &s) { @@ -672,7 +669,7 @@ struct Grid { } void MergeWithParent(Grid *p, Selection &s, Document *doc) { - cell->grid.release(); + cell->grid = nullptr; foreachcell(c) { if (x + s.x >= p->xs) p->InsertCells(p->xs, -1, 1, 0); if (y + s.y >= p->ys) p->InsertCells(-1, p->ys, 0, 1); @@ -878,7 +875,7 @@ struct Grid { unique_ptr Sum() { double total = 0; - foreachconstcell(c) { + foreachcell(c) { if (c->HasText()) total += c->text.GetNum(); } auto c = make_unique(); @@ -887,9 +884,10 @@ struct Grid { } void Transpose() { - vector tr(xs * ys); - foreachconstcell(c) tr[y + x * ys] = c; - cells = std::move(tr); + Cell **tr = new Cell *[xs * ys]; + foreachcell(c) tr[y + x * ys] = c; + delete[] cells; + cells = tr; swap_(xs, ys); SetOrient(); InitColWidths(); @@ -908,12 +906,12 @@ struct Grid { sys->sortcolumn = s.x; sys->sortxs = xs; sys->sortdescending = descending; - qsort(cells.data() + s.y * xs, s.ys, sizeof(Cell *) * xs, + qsort(cells + s.y * xs, s.ys, sizeof(Cell *) * xs, (int(__cdecl *)(const void *, const void *))sortfunc); } Cell *FindExact(const wxString &s) { - foreachconstcell(c) { + foreachcell(c) { Cell *f = c->FindExact(s); if (f) return f; } @@ -924,7 +922,7 @@ struct Grid { Cell *selcell = nullptr; bool done = false; lookformore: - foreachconstcell(c) if (c->grid && !done) { + foreachcell(c) if (c->grid && !done) { Cell *f = c->grid->FindExact(tag); if (f) { // add all parent tags as extra hierarchy inside the cell @@ -935,18 +933,18 @@ struct Grid { Cell *t = new Cell(f, p); t->text = p->text; t->text.cell = t; - t->grid = std::move(f->grid); + t->grid = f->grid; if (t->grid) t->grid->ReParent(t); - f->grid = make_unique(1, 1); + f->grid = new Grid(1, 1); f->grid->cell = f; - f->grid->cells[0] = t; + *f->grid->cells = t; } // remove cell from parent, recursively if parent becomes empty for (Cell *r = f; r && r != cell; r = r->parent->grid->DeleteTagParent(r, cell, f)) ; // merge newly constructed hierarchy at this level - if (cells.empty()) { - cells.push_back(f); + if (!*cells) { + *cells = f; f->parent = cell; selcell = f; } else { @@ -961,7 +959,7 @@ struct Grid { void ReParent(Cell *p) { cell = p; - foreachconstcell(c) c->parent = p; + foreachcell(c) c->parent = p; } Cell *DeleteTagParent(Cell *tag, Cell *basecell, Cell *found) { @@ -975,7 +973,7 @@ struct Grid { if (tag != found) delete tag; return next; } else - foreachconstcell(c) if (c == nullptr) { + foreachcell(c) if (c == nullptr) { if (ys > 1) DeleteCells(-1, y, 0, -1); else @@ -987,15 +985,16 @@ struct Grid { } void MergeTagCell(Cell *f, Cell *&selcell) { - foreachconstcell(c) if (c->text.t == f->text.t) { + foreachcell(c) if (c->text.t == f->text.t) { if (!selcell) selcell = c; if (f->grid) { if (c->grid) { f->grid->MergeTagAll(c); } else { - c->grid = std::move(f->grid); + c->grid = f->grid; c->grid->ReParent(c); + f->grid = nullptr; } delete f; } @@ -1017,7 +1016,7 @@ struct Grid { } bool IsTable() { - foreachconstcell(c) if (c->grid) return false; + foreachcell(c) if (c->grid) return false; return true; } @@ -1034,7 +1033,7 @@ struct Grid { if (prev->text.t == c->text.t) { if (rest) { ASSERT(prev->grid); - prev->grid->MergeRow(rest->grid.get()); + prev->grid->MergeRow(rest->grid); rest.reset(); } @@ -1046,14 +1045,14 @@ struct Grid { } } if (rest) { - c->grid.swap(rest->grid); + swap_(c->grid, rest->grid); c->grid->ReParent(c); } done:; } Selection s(this, 1, 0, xs - 1, ys); MultiCellDeleteSub(doc, s); - foreachconstcell(c) if (c->grid && c->grid->xs > 1) c->grid->Hierarchify(doc); + foreachcell(c) if (c->grid && c->grid->xs > 1) c->grid->Hierarchify(doc); } void MergeRow(Grid *tm) { @@ -1066,11 +1065,11 @@ struct Grid { } void MaxDepthLeaves(int curdepth, int &maxdepth, int &leaves) { - foreachconstcell(c) c->MaxDepthLeaves(curdepth, maxdepth, leaves); + foreachcell(c) c->MaxDepthLeaves(curdepth, maxdepth, leaves); } int Flatten(int curdepth, int cury, Grid *g) { - foreachconstcell(c) if (c->grid) { cury = c->grid->Flatten(curdepth + 1, cury, g); } + foreachcell(c) if (c->grid) { cury = c->grid->Flatten(curdepth + 1, cury, g); } else { Cell *ic = c; for (int i = curdepth; i >= 0; i--) { @@ -1096,7 +1095,7 @@ struct Grid { } } - void CollectCells(vector &itercells) { foreachconstcell(c) c->CollectCells(itercells); } + void CollectCells(vector &itercells) { foreachcell(c) c->CollectCells(itercells); } void CollectCellsSel(vector &itercells, const Selection &s, bool recurse) { foreachcellinsel(c, s) c->CollectCells(itercells, recurse); } diff --git a/src/mycanvas.h b/src/mycanvas.h index ae0ad5dd..947257a7 100644 --- a/src/mycanvas.h +++ b/src/mycanvas.h @@ -1,6 +1,6 @@ struct TSCanvas : public wxScrolledCanvas { MyFrame *frame; - unique_ptr doc; + Document *doc {nullptr}; int mousewheelaccum {0}; bool lastrmbwaswithctrl {false}; wxPoint lastmousepos; @@ -17,7 +17,10 @@ struct TSCanvas : public wxScrolledCanvas { EnableScrolling(false, false); } - ~TSCanvas() { frame = nullptr; } + ~TSCanvas() { + DELETEP(doc); + frame = nullptr; + } void OnPaint(wxPaintEvent &event) { #if defined(__WXMAC__) || defined(__WXGTK__) diff --git a/src/myframe.h b/src/myframe.h index fe8abd0c..53577457 100644 --- a/src/myframe.h +++ b/src/myframe.h @@ -753,7 +753,7 @@ struct MyFrame : wxFrame { TSCanvas *NewTab(Document *doc, bool append = false) { TSCanvas *sw = new TSCanvas(this, nb); - sw->doc.reset(doc); + sw->doc = doc; doc->sw = sw; sw->SetScrollRate(1, 1); if (append) @@ -784,7 +784,7 @@ struct MyFrame : wxFrame { TSCanvas *sw = (TSCanvas *)nb->GetPage(nbe.GetSelection()); sw->Status(); SetSearchTextBoxBackgroundColour(false); - sys->TabChange(sw->doc.get()); + sys->TabChange(sw->doc); } void TabsReset() { @@ -1093,7 +1093,7 @@ struct MyFrame : wxFrame { sys->darkennonmatchingcells = searchstring.Len() != 0; sys->searchstring = (sys->casesensitivesearch) ? searchstring : searchstring.Lower(); SetSearchTextBoxBackgroundColour(false); - Document *doc = GetCurTab()->doc.get(); + Document *doc = GetCurTab()->doc; TSCanvas *sw = GetCurTab(); wxClientDC dc(sw); doc->SearchNext(dc, false, false, false); @@ -1286,7 +1286,7 @@ struct MyFrame : wxFrame { if ((event.GetChangeType() & 0xF) == 0 || watcherwaitingforuser || !nb) return; const wxString &modfile = event.GetPath().GetFullPath(); loop(i, nb->GetPageCount()) { - Document *doc = ((TSCanvas *)nb->GetPage(i))->doc.get(); + Document *doc = ((TSCanvas *)nb->GetPage(i))->doc; if (modfile == doc->filename) { wxDateTime modtime = wxFileName(modfile).GetModificationTime(); // Compare with last modified to trigger multiple times. @@ -1318,7 +1318,7 @@ struct MyFrame : wxFrame { if (*msg) { GetCurTab()->Status(msg); } else { - loop(j, nb->GetPageCount()) if (((TSCanvas *)nb->GetPage(j))->doc.get() == doc) + loop(j, nb->GetPageCount()) if (((TSCanvas *)nb->GetPage(j))->doc == doc) nb->DeletePage(j); ::wxRemoveFile(sys->TmpName(modfile)); GetCurTab()->Status( diff --git a/src/mywxtools.h b/src/mywxtools.h index 0cf1c0e4..762d6d95 100644 --- a/src/mywxtools.h +++ b/src/mywxtools.h @@ -23,7 +23,7 @@ struct DropTarget : wxDropTarget { GetData(); TSCanvas *sw = sys->frame->GetCurTab(); sw->SelectClick(x, y, false, 0); - Document *doc = sw->doc.get(); + Document *doc = sw->doc; switch (doc->dndobjc->GetReceivedFormat().GetType()) { case wxDF_BITMAP: doc->PasteOrDrop(*doc->dndobji); break; case wxDF_FILENAME: doc->PasteOrDrop(*doc->dndobjf); break; diff --git a/src/selection.h b/src/selection.h index b0b03e67..f98bd7ff 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.get(); + Grid *g = at->Parent(ad - i + 1)->grid; 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 4a0dc8c3..e8fa9f66 100644 --- a/src/system.h +++ b/src/system.h @@ -210,7 +210,7 @@ struct System { void LoadOpRef() { LoadDB(frame->GetDocPath(L"examples/operation-reference.cts")); } - unique_ptr &InitDB(int sizex, int sizey = 0) { + Cell *&InitDB(int sizex, int sizey = 0) { Cell *c = new Cell(nullptr, nullptr, CT_DATA, new Grid(sizex, sizey ? sizey : sizex)); c->cellcolor = 0xCCDCE2; c->grid->InitCells(); @@ -470,12 +470,13 @@ struct System { case A_IMPXMLA: { wxXmlDocument doc; if (!doc.Load(fn)) goto problem; - unique_ptr &r = InitDB(1); - Cell *c = r->grid->cells[0]; + Cell *&r = InitDB(1); + Cell *c = *r->grid->cells; FillXML(c, doc.GetRoot(), k == A_IMPXMLA); if (!c->HasText() && c->grid) { - r->grid->cells[0] = nullptr; - r.reset(c); + *r->grid->cells = nullptr; + delete r; + r = c; c->parent = nullptr; } break; @@ -492,8 +493,8 @@ struct System { if (as.size()) switch (k) { case A_IMPTXTI: { - Cell *r = InitDB(1).get(); - FillRows(r->grid.get(), as, CountCol(as[0]), 0, 0); + Cell *r = InitDB(1); + FillRows(r->grid, as, CountCol(as[0]), 0, 0); }; break; case A_IMPTXTC: InitDB(1, (int)as.size())->grid->CSVImport(as, L','); @@ -608,7 +609,7 @@ struct System { if (col < column && startrow != 0) return i; if (col > column) { Cell *c = g->C(0, y - 1); - Grid *sg = c->grid.get(); + Grid *sg = c->grid; 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 b5ec7020..12b14816 100644 --- a/src/treesheets_impl.h +++ b/src/treesheets_impl.h @@ -7,9 +7,8 @@ struct TreeSheetsScriptImpl : public ScriptInterface { enum { max_new_grid_cells = 256 * 256 }; // Don't allow crazy sizes. void SwitchToCurrentDoc() { - doc = sys->frame->GetCurTab()->doc.get(); - cur = doc->rootgrid.get(); - + doc = sys->frame->GetCurTab()->doc; + cur = doc->rootgrid; doc->AddUndo(cur); } @@ -42,7 +41,7 @@ struct TreeSheetsScriptImpl : public ScriptInterface { return true; } - void GoToRoot() { cur = doc->rootgrid.get(); } + void GoToRoot() { cur = doc->rootgrid; } void GoToView() { cur = doc->curdrawroot; } bool HasSelection() { return doc->selected.g; } void GoToSelection() { @@ -95,7 +94,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.get(), x, y, xs, ys); + Selection s(cur->grid, x, y, xs, ys); cur->grid->MultiCellDeleteSub(doc, s); doc->SetSelect(Selection()); doc->Zoom(-100, *dc);