Skip to content

Commit e3d4602

Browse files
authored
Use unique_ptr for Document in TSCanvas (#780)
Make it clear that TSCanvas owns the Document.
1 parent c7fade2 commit e3d4602

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

src/mycanvas.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct TSCanvas : public wxScrolledCanvas {
22
MyFrame *frame;
3-
Document *doc {nullptr};
3+
unique_ptr<Document> doc;
44
int mousewheelaccum {0};
55
bool lastrmbwaswithctrl {false};
66
wxPoint lastmousepos;
@@ -17,10 +17,7 @@ struct TSCanvas : public wxScrolledCanvas {
1717
EnableScrolling(false, false);
1818
}
1919

20-
~TSCanvas() {
21-
DELETEP(doc);
22-
frame = nullptr;
23-
}
20+
~TSCanvas() { frame = nullptr; }
2421

2522
void OnPaint(wxPaintEvent &event) {
2623
#if defined(__WXMAC__) || defined(__WXGTK__)

src/myframe.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ struct MyFrame : wxFrame {
742742

743743
TSCanvas *NewTab(Document *doc, bool append = false) {
744744
TSCanvas *sw = new TSCanvas(this, nb);
745-
sw->doc = doc;
745+
sw->doc.reset(doc);
746746
doc->sw = sw;
747747
sw->SetScrollRate(1, 1);
748748
if (append)
@@ -773,7 +773,7 @@ struct MyFrame : wxFrame {
773773
TSCanvas *sw = (TSCanvas *)nb->GetPage(nbe.GetSelection());
774774
sw->Status();
775775
SetSearchTextBoxBackgroundColour(false);
776-
sys->TabChange(sw->doc);
776+
sys->TabChange(sw->doc.get());
777777
}
778778

779779
void TabsReset() {
@@ -1082,7 +1082,7 @@ struct MyFrame : wxFrame {
10821082
sys->darkennonmatchingcells = searchstring.Len() != 0;
10831083
sys->searchstring = (sys->casesensitivesearch) ? searchstring : searchstring.Lower();
10841084
SetSearchTextBoxBackgroundColour(false);
1085-
Document *doc = GetCurTab()->doc;
1085+
Document *doc = GetCurTab()->doc.get();
10861086
TSCanvas *sw = GetCurTab();
10871087
wxClientDC dc(sw);
10881088
doc->SearchNext(dc, false, false, false);
@@ -1275,7 +1275,7 @@ struct MyFrame : wxFrame {
12751275
if ((event.GetChangeType() & 0xF) == 0 || watcherwaitingforuser || !nb) return;
12761276
const wxString &modfile = event.GetPath().GetFullPath();
12771277
loop(i, nb->GetPageCount()) {
1278-
Document *doc = ((TSCanvas *)nb->GetPage(i))->doc;
1278+
Document *doc = ((TSCanvas *)nb->GetPage(i))->doc.get();
12791279
if (modfile == doc->filename) {
12801280
wxDateTime modtime = wxFileName(modfile).GetModificationTime();
12811281
// Compare with last modified to trigger multiple times.
@@ -1307,7 +1307,7 @@ struct MyFrame : wxFrame {
13071307
if (*msg) {
13081308
GetCurTab()->Status(msg);
13091309
} else {
1310-
loop(j, nb->GetPageCount()) if (((TSCanvas *)nb->GetPage(j))->doc == doc)
1310+
loop(j, nb->GetPageCount()) if (((TSCanvas *)nb->GetPage(j))->doc.get() == doc)
13111311
nb->DeletePage(j);
13121312
::wxRemoveFile(sys->TmpName(modfile));
13131313
GetCurTab()->Status(

src/mywxtools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct DropTarget : wxDropTarget {
2323
GetData();
2424
TSCanvas *sw = sys->frame->GetCurTab();
2525
sw->SelectClick(x, y, false, 0);
26-
Document *doc = sw->doc;
26+
Document *doc = sw->doc.get();
2727
switch (doc->dndobjc->GetReceivedFormat().GetType()) {
2828
case wxDF_BITMAP: doc->PasteOrDrop(*doc->dndobji); break;
2929
case wxDF_FILENAME: doc->PasteOrDrop(*doc->dndobjf); break;

src/treesheets_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct TreeSheetsScriptImpl : public ScriptInterface {
77
enum { max_new_grid_cells = 256 * 256 }; // Don't allow crazy sizes.
88

99
void SwitchToCurrentDoc() {
10-
doc = sys->frame->GetCurTab()->doc;
10+
doc = sys->frame->GetCurTab()->doc.get();
1111
cur = doc->rootgrid.get();
1212

1313
doc->AddUndo(cur);

0 commit comments

Comments
 (0)