From bd5d2bf11417777868f3daf2097744b327ae4d58 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 22 Jun 2017 20:29:32 -0400 Subject: [PATCH] Accessor for shared pointer to track, given only a bare pointer --- src/Track.cpp | 3 +++ src/Track.h | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Track.cpp b/src/Track.cpp index de11921fd..68a247f16 100644 --- a/src/Track.cpp +++ b/src/Track.cpp @@ -942,6 +942,8 @@ auto TrackList::Replace(Track * t, value_type &&with) -> value_type value_type holder; if (t && with) { auto node = t->GetNode(); + t->SetOwner(nullptr, {}); + holder = std::move(*node); Track *pTrack = with.get(); @@ -962,6 +964,7 @@ TrackNodePointer TrackList::Remove(Track *t) TrackNodePointer result(end()); if (t) { auto node = t->GetNode(); + t->SetOwner(nullptr, {}); if (!isNull(node)) { value_type holder = std::move( *node ); diff --git a/src/Track.h b/src/Track.h index 351a9fea5..a5b1d81f5 100644 --- a/src/Track.h +++ b/src/Track.h @@ -109,6 +109,27 @@ class AUDACITY_DLL_API Track /* not final */ public: mutable wxSize vrulerSize; + // Given a bare pointer, find a shared_ptr. But this is not possible for + // a track not owned by any list, so the result can be null. + template + inline static std::shared_ptr Pointer( Track *t ) + { + if (t && t->mList) + return std::static_pointer_cast(*t->mNode); + return {}; + } + + template + inline static std::shared_ptr Pointer( const Track *t ) + { + if (t && t->mList) { + std::shared_ptr p{ *t->mNode }; + // Let you change the type, but not cast away the const + return std::static_pointer_cast(p); + } + return {}; + } + // An implementation is defined for call-through from subclasses, but // the inherited method is still marked pure virtual HitTestResult HitTest