diff --git a/src/Track.cpp b/src/Track.cpp index e3a78a8d4..9a4d50cd3 100644 --- a/src/Track.cpp +++ b/src/Track.cpp @@ -138,6 +138,26 @@ void Track::SetOwner mNode = node; } +const std::shared_ptr &Track::GetTrackView() +{ + return mpView; +} + +void Track::SetTrackView( const std::shared_ptr &pView ) +{ + mpView = pView; +} + +const std::shared_ptr &Track::GetTrackControls() +{ + return mpControls; +} + +void Track::SetTrackControls( const std::shared_ptr &pControls ) +{ + mpControls = pControls; +} + int Track::GetIndex() const { return mIndex; @@ -1270,13 +1290,3 @@ void TrackFactory::Destroy( AudacityProject &project ) { project.AttachedObjects::Assign( key2, nullptr ); } - -template<> auto DoGetControls::Implementation() -> Function { - return nullptr; -} -static DoGetControls registerDoGetControls; - -template<> auto DoGetView::Implementation() -> Function { - return nullptr; -} -static DoGetView registerDoGetView; diff --git a/src/Track.h b/src/Track.h index edc82f986..d850a983a 100644 --- a/src/Track.h +++ b/src/Track.h @@ -261,15 +261,15 @@ class AUDACITY_DLL_API Track /* not final */ public: mutable wxSize vrulerSize; - // Return another, associated TrackPanelCell object that implements - // click and drag and keystrokes in the track contents. - std::shared_ptr GetTrackView(); - std::shared_ptr GetTrackView() const; + // These are exposed only for the purposes of the TrackView class, to + // initialize the pointer on demand + const std::shared_ptr &GetTrackView(); + void SetTrackView( const std::shared_ptr &pView ); - // Return another, associated TrackPanelCell object that implements the - // drop-down, close and minimize buttons, etc. - std::shared_ptr GetTrackControls(); - std::shared_ptr GetTrackControls() const; + // These are exposed only for the purposes of the TrackControls class, to + // initialize the pointer on demand + const std::shared_ptr &GetTrackControls(); + void SetTrackControls( const std::shared_ptr &pControls ); // Return another, associated TrackPanelCell object that implements the @@ -1588,24 +1588,4 @@ class AUDACITY_DLL_API TrackFactory final #endif }; -#include "AttachedVirtualFunction.h" - -struct DoGetControlsTag; - -using DoGetControls = -AttachedVirtualFunction< - DoGetControlsTag, - std::shared_ptr< TrackControls >, - Track ->; - -struct DoGetViewTag; - -using DoGetView = -AttachedVirtualFunction< - DoGetViewTag, - std::shared_ptr< TrackView >, - Track ->; - #endif diff --git a/src/tracks/ui/TrackControls.cpp b/src/tracks/ui/TrackControls.cpp index 0c850ec59..5e4dee257 100644 --- a/src/tracks/ui/TrackControls.cpp +++ b/src/tracks/ui/TrackControls.cpp @@ -24,11 +24,20 @@ TrackControls::~TrackControls() TrackControls &TrackControls::Get( Track &track ) { - return *static_cast( track.GetTrackControls().get() ); + auto pControls = + std::static_pointer_cast( track.GetTrackControls() ); + if (!pControls) + // create on demand + track.SetTrackControls( pControls = DoGetControls::Call( track ) ); + return *pControls; } const TrackControls &TrackControls::Get( const Track &track ) { - return *static_cast( track.GetTrackControls().get() ); + return Get( const_cast< Track& >( track ) ); } +template<> auto DoGetControls::Implementation() -> Function { + return nullptr; +} +static DoGetControls registerDoGetControls; diff --git a/src/tracks/ui/TrackControls.h b/src/tracks/ui/TrackControls.h index 317548343..11f451bc6 100644 --- a/src/tracks/ui/TrackControls.h +++ b/src/tracks/ui/TrackControls.h @@ -28,4 +28,15 @@ public: virtual ~TrackControls() = 0; }; +#include "AttachedVirtualFunction.h" + +struct DoGetControlsTag; + +using DoGetControls = +AttachedVirtualFunction< + DoGetControlsTag, + std::shared_ptr< TrackControls >, + Track +>; + #endif diff --git a/src/tracks/ui/TrackView.cpp b/src/tracks/ui/TrackView.cpp index 47524c25f..7d25afe14 100644 --- a/src/tracks/ui/TrackView.cpp +++ b/src/tracks/ui/TrackView.cpp @@ -59,12 +59,16 @@ void TrackView::CopyTo( Track &track ) const TrackView &TrackView::Get( Track &track ) { - return static_cast( *track.GetTrackView() ); + auto pView = std::static_pointer_cast( track.GetTrackView() ); + if (!pView) + // create on demand + track.SetTrackView( pView = DoGetView::Call( track ) ); + return *pView; } const TrackView &TrackView::Get( const Track &track ) { - return static_cast( *track.GetTrackView() ); + return Get( const_cast< Track& >( track ) ); } void TrackView::SetMinimized(bool isMinimized) @@ -107,32 +111,6 @@ void TrackView::DoSetMinimized(bool isMinimized) mMinimized = isMinimized; } -std::shared_ptr Track::GetTrackView() -{ - if (!mpView) - // create on demand - mpView = DoGetView::Call( *this ); - return mpView; -} - -std::shared_ptr Track::GetTrackView() const -{ - return const_cast(this)->GetTrackView(); -} - -std::shared_ptr Track::GetTrackControls() -{ - if (!mpControls) - // create on demand - mpControls = DoGetControls::Call( *this ); - return mpControls; -} - -std::shared_ptr Track::GetTrackControls() const -{ - return const_cast< Track* >( this )->GetTrackControls(); -} - std::shared_ptr TrackView::GetVRulerControls() { if (!mpVRulerControls) @@ -233,3 +211,8 @@ static const AudacityProject::AttachedObjects::RegisteredFactory key{ }; } + +template<> auto DoGetView::Implementation() -> Function { + return nullptr; +} +static DoGetView registerDoGetView; diff --git a/src/tracks/ui/TrackView.h b/src/tracks/ui/TrackView.h index 0feee6c35..fcf855b15 100644 --- a/src/tracks/ui/TrackView.h +++ b/src/tracks/ui/TrackView.h @@ -93,4 +93,15 @@ private: int mHeight{ DefaultHeight }; }; +#include "AttachedVirtualFunction.h" + +struct DoGetViewTag; + +using DoGetView = +AttachedVirtualFunction< + DoGetViewTag, + std::shared_ptr< TrackView >, + Track +>; + #endif