Skip to content

Commit

Permalink
Use Smart pointers (#735)
Browse files Browse the repository at this point in the history
* Make UndoItem unique_ptr in undolist and redolist

* Correct unique_ptr to shared_ptr for Grid in evaluator
  • Loading branch information
tobiolo authored Oct 30, 2024
1 parent eb4da48 commit 0fee6f5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ struct Document {
int laststylebits;
int initialzoomlevel {0};
Cell *curdrawroot; // for use during Render() calls
std::vector<UndoItem *> undolist;
std::vector<UndoItem *> redolist;
std::vector<unique_ptr<UndoItem>> undolist;
std::vector<unique_ptr<UndoItem>> redolist;
std::vector<Selection> drawpath;
int pathscalebias {0};
wxString filename {L""};
Expand Down Expand Up @@ -2174,14 +2174,14 @@ struct Document {
UpdateFileName();
}
if (LastUndoSameCellTextEdit(c)) return;
UndoItem *ui = new UndoItem();
undolist.push_back(ui);
unique_ptr<UndoItem> ui = make_unique<UndoItem>();
ui->clone = c->Clone(nullptr);
ui->estimated_size = c->EstimatedMemoryUse();
ui->sel = selected;
ui->cloned_from = (uintptr_t)c;
CreatePath(c, ui->path);
if (selected.g) CreatePath(selected.g->cell, ui->selpath);
undolist.push_back(std::move(ui));
size_t total_usage = 0;
size_t old_list_size = undolist.size();
// Cull undolist. Always at least keeps last item.
Expand All @@ -2199,12 +2199,12 @@ struct Document {
undolistsizeatfullsave -= items_culled; // Allowed to go < 0
}

void Undo(wxDC &dc, std::vector<UndoItem *> &fromlist, std::vector<UndoItem *> &tolist,
bool redo = false) {
void Undo(wxDC &dc, std::vector<unique_ptr<UndoItem>> &fromlist,
std::vector<unique_ptr<UndoItem>> &tolist, bool redo = false) {
Selection beforesel = selected;
std::vector<Selection> beforepath;
if (beforesel.g) CreatePath(beforesel.g->cell, beforepath);
UndoItem *ui = fromlist.back();
unique_ptr<UndoItem> ui = std::move(fromlist.back());
fromlist.pop_back();
Cell *c = WalkPath(ui->path);
auto clone = ui->clone.release();
Expand All @@ -2220,7 +2220,7 @@ struct Document {
begindrag = selected;
ui->sel = beforesel;
ui->selpath = std::move(beforepath);
tolist.push_back(ui);
tolist.push_back(std::move(ui));
if (undolistsizeatfullsave > undolist.size())
undolistsizeatfullsave = -1; // gone beyond the save point, always modified
modified = undolistsizeatfullsave != undolist.size();
Expand Down
2 changes: 1 addition & 1 deletion src/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct Evaluator {
if (g->xs == 1 || g->ys == 1) {
return op->runl(g);
} else {
std::vector<unique_ptr<Grid>> gs;
std::vector<shared_ptr<Grid>> gs;
g->Split(gs, vert);
g = new Grid(vert ? gs.size() : 1, vert ? 1 : gs.size());
auto c = make_unique<Cell>(nullptr, left.get(), CT_DATA, g);
Expand Down
6 changes: 3 additions & 3 deletions src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,10 @@ struct Grid {
return acc;
}

void Split(std::vector<unique_ptr<Grid>> &gs, bool vert) {
loop(i, vert ? xs : ys) gs.push_back(make_unique<Grid>(vert ? 1 : xs, vert ? ys : 1));
void Split(std::vector<shared_ptr<Grid>> &gs, bool vert) {
loop(i, vert ? xs : ys) gs.push_back(make_shared<Grid>(vert ? 1 : xs, vert ? ys : 1));
foreachcell(c) {
Grid *g = gs[vert ? x : y].get();
shared_ptr<Grid> g = gs[vert ? x : y];
g->cells[vert ? y : x] = c->SetParent(g->cell);
c = nullptr;
}
Expand Down

0 comments on commit 0fee6f5

Please sign in to comment.