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

Make rootgrid an unique_ptr #776

Merged
merged 1 commit into from
Dec 2, 2024
Merged

Conversation

tobiolo
Copy link
Collaborator

@tobiolo tobiolo commented Dec 2, 2024

This is the second approach. I used the wrong comment type (review comment), so maybe this more cleaner now.

@@ -570,7 +568,7 @@ struct Document {
ResetFont();
dc.SetUserScale(1, 1);
curdrawroot = WalkPath(drawpath);
int psb = curdrawroot == rootgrid ? 0 : curdrawroot->MinRelsize();
int psb = curdrawroot == rootgrid.get() ? 0 : curdrawroot->MinRelsize();
Copy link
Collaborator Author

@tobiolo tobiolo Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to call .get() because curdrawroot is Cell * and rootgrid is unique_ptr<Cell>.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the precedence order here is:
int psb = ((curdrawroot == rootgrid.get()) ? 0 : curdrawroot->MinRelsize());
Is this correct? If yes, I cannot compare curdrawroot and rootgrid directly?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, did not see. You're right!

Cell *c = *r->grid->cells;
FillXML(c, doc.GetRoot(), k == A_IMPXMLA);
if (!c->HasText() && c->grid) {
*r->grid->cells = nullptr;
delete r;
r = c;
r.reset(c);
Copy link
Collaborator Author

@tobiolo tobiolo Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I manipulate the pointer r directly, so I need a reference to the pointer. r is (by reference) the same as rootgrid of the newly created Document created by line 467. I cannot see how I can borrow the pointer here. But maybe you can help me out here? With .reset(T*) I instruct the owner directly to delete the previous managed resource.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems fine

@@ -487,7 +486,7 @@ struct System {

if (as.size()) switch (k) {
case A_IMPTXTI: {
Cell *r = InitDB(1);
Cell *r = InitDB(1).get();
Copy link
Collaborator Author

@tobiolo tobiolo Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I can borrow the pointer r because only the substructure r->grid is manipulated.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this IS borrowing a pointer

@tobiolo tobiolo requested a review from aardappel December 2, 2024 07:40
@@ -608,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 ? root_grid_spacing : user_grid_outer_spacing - 1;
cell == doc->rootgrid.get() ? root_grid_spacing : user_grid_outer_spacing - 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my other comment about using it as boolean

Copy link
Collaborator Author

@tobiolo tobiolo Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here: I think the precedence order is
const int grid_border_width = (((cell == doc->rootgrid.get()) ? root_grid_spacing : user_grid_outer_spacing) - 1)

Copy link
Collaborator Author

@tobiolo tobiolo Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a unary operation or am I missing something?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, weird, they have overloads to directly compare against null, but not raw pointers https://en.cppreference.com/w/cpp/memory/unique_ptr/operator_cmp

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately they have not, would make things easier...

@tobiolo tobiolo requested a review from aardappel December 2, 2024 18:02
@aardappel aardappel merged commit 15d5f58 into aardappel:master Dec 2, 2024
8 checks passed
@tobiolo tobiolo deleted the uniqueptrs-2 branch December 2, 2024 18:27
@tobiolo
Copy link
Collaborator Author

tobiolo commented Dec 2, 2024

Thanks for the merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants