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

SidebarModel: Clean up variable naming and use C++ casts #3922

Merged
merged 1 commit into from
May 30, 2021

Conversation

Holzhaus
Copy link
Member

This adds the p prefix to pointer variables, uses camelCase naming
consistently and replaces C-style typecasts with C++ casts.

We'll need some refactoring of the library handling to support QML, so I thought it would be best to take care of this separately.
I also found two issues where we cast a const pointer to a mutable one. That was hidden by C-style typecast. I didn't fix it here, just make the issue obvious by using const_cast.

This adds the `p` prefix to pointer variables, uses camelCase naming
consistently and replaces C-style typecasts with C++ casts.

// `this` is const, but the function expects a non-const pointer.
// TODO: Check if we can get rid of this const cast somehow.
return createIndex(row, column, const_cast<SidebarModel*>(this));
Copy link
Member

Choose a reason for hiding this comment

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

This is IMHO one of the rare cases where a const cast is correct.
The function itself is const, and it returns an object with a pointer to itself.
Nothing changes in the object before the function is done. So the const nature is maintained.

Interestingly this function takes a const pointer in Qt 6 here:
https://github.com/qt/qtbase/blob/9db7cc79a26ced4997277b5c206ca15949133240/src/corelib/itemmodels/qabstractitemmodel.h#L404

I would prefer to cast to the parameter type directly:```suggestion
return createIndex(row, column, const_cast<void*>(this));

Copy link
Member

Choose a reason for hiding this comment

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

Taking the this pointer is redundant by the way:
https://github.com/qt/qtbase/blob/9db7cc79a26ced4997277b5c206ca15949133240/src/corelib/itemmodels/qabstractitemmodel.h#L457

We can access it by QModelIndex::model()

However there is a warning

A const pointer to the model is returned because calls to non-const functions of the model might invalidate the model index and possibly crash your application.

That is what we do now ... :-/

https://doc.qt.io/qt-6/qmodelindex.html#model

Copy link
Member

Choose a reason for hiding this comment

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

Do you have interest to check, if we can just replace internalPointer() by model() and keep the constnes of the pointer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you have interest to check, if we can just replace internalPointer() by model() and keep the constnes of the pointer?

I don't really get what you mean. Can you give me a code example?

Copy link
Contributor

@uklotzde uklotzde left a comment

Choose a reason for hiding this comment

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

The requested changes should be applied in a subsequent PR. This PR serves just as a cleanup and does not change any behavior, which I consider the right approach. LGTM

@daschuer Merge?

Copy link
Member

@daschuer daschuer left a comment

Choose a reason for hiding this comment

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

Yes sure. LGTM.

@daschuer daschuer merged commit 15a97b5 into mixxxdj:main May 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants