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

Refactor(parented_ptr): make trivially destructible in release mode, delete move operations #11981

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/util/parented_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ class parented_ptr final {
explicit parented_ptr(T* t) noexcept
: m_ptr{t} {
}

// Only generate destructor if not empty, otherwise its empty but will
// cause the parented_ptr to be not trivially destructible even though it could be.
#ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED
~parented_ptr() noexcept {
DEBUG_ASSERT(!m_ptr || static_cast<const QObject*>(m_ptr)->parent());
}

// Delete copy constructor and copy assignment operator
#else
// explicitly generate trivial destructor (since decltype(m_ptr) is not a class type)
~parented_ptr() noexcept = default;
#endif
Copy link
Member

Choose a reason for hiding this comment

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

for my understanding '~parented_ptr() = default' and ~parented_ptr() {} is the same. So I can imagine that this is not needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

No its not equivalent. One if an empty definition which means that its still user provided even if it does nothing, the other one is the default declared one which does not count as user-provided.

See
https://en.cppreference.com/w/cpp/language/destructor#Trivial_destructor
https://compiler-explorer.com/z/cbG99jbzr

// Rule of 5
parented_ptr(const parented_ptr<T>&) = delete;
parented_ptr& operator=(const parented_ptr<T>&) = delete;
parented_ptr(const parented_ptr<T>&& other) = delete;
parented_ptr& operator=(const parented_ptr<T>&& other) = delete;
Comment on lines 40 to +43
Copy link
Member

Choose a reason for hiding this comment

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

I have never understood why it should be not lowed to copy or move a parented_ptr()
A copy of the parented pointer can be safely moved around since it is not owning the pointer. It is just telling the programmer: "This is a borrowed pointer owned by the parent object".

Copy link
Member

Choose a reason for hiding this comment

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

Ok, It makes sense to have only one instance checking the ownership. The deleted copy constructor enforces that.
Unfortunately it prevents also to copy the object that contains it.
A move will move the responsibility for checking the parent along with the pointer. There is nothing wrong with it. Is it?
What is your opinion here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't really check to be honest. I just saw that we didn't follow the rule of five and thus I chose the safe default. We can lift the restrictions if we can be sure that its safe.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have reservations about moving. If we move an object that is owned by its parent, its only safe if the parent knows that it has been moved, do QObjects take care of telling their parent?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it is probably OK, we can change it whenever we find a valid use case for it.


// If U* is convertible to T* then parented_ptr<U> is convertible to parented_ptr<T>
template<
Expand Down
11 changes: 3 additions & 8 deletions src/widget/findonwebmenufactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ namespace mixxx {
namespace library {

void createFindOnWebSubmenus(QMenu* pFindOnWebMenu, const Track& track) {
auto pFindOnWebMenuSoundcloud = make_parented<QMenu>(
new FindOnWebMenuSoundcloud(pFindOnWebMenu, track));

auto pFindOnWebMenuDiscogs = make_parented<QMenu>(
new FindOnWebMenuDiscogs(pFindOnWebMenu, track));

auto pFindOnWebMenuLastfm = make_parented<QMenu>(
new FindOnWebMenuLastfm(pFindOnWebMenu, track));
make_parented<QMenu>(new FindOnWebMenuSoundcloud(pFindOnWebMenu, track));
make_parented<QMenu>(new FindOnWebMenuDiscogs(pFindOnWebMenu, track));
make_parented<QMenu>(new FindOnWebMenuLastfm(pFindOnWebMenu, track));
}

} // namespace library
Expand Down
Loading