From 2295697df9c66f4b19d6874106729fede51ec93d Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 10 Jul 2014 17:05:19 +0200 Subject: convert the weak reference macro in tools into a template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit which required making SfxFrame subclass SvCompatWeakBase, but that makes the relationships clearer anyhow. Change-Id: I209d05359d50111eacac72c971bb46ccf569ba49 Reviewed-on: https://gerrit.libreoffice.org/10285 Tested-by: LibreOffice gerrit bot Reviewed-by: Caolán McNamara Tested-by: Caolán McNamara --- include/sfx2/frame.hxx | 6 ++-- include/tools/ref.hxx | 69 ++++++++++++++++++++++++------------------- sfx2/source/view/frame.cxx | 6 ---- sfx2/source/view/frame2.cxx | 3 +- sfx2/source/view/impframe.hxx | 5 ++-- 5 files changed, 44 insertions(+), 45 deletions(-) diff --git a/include/sfx2/frame.hxx b/include/sfx2/frame.hxx index 18bf6cd08099..7a64d9b2f52d 100644 --- a/include/sfx2/frame.hxx +++ b/include/sfx2/frame.hxx @@ -75,7 +75,6 @@ class SfxDispatcher; class Rectangle; class SfxRequest; class SfxUnoControllerItem; -class SvCompatWeakHdl; class SystemWindow; class SfxFrame; @@ -97,7 +96,7 @@ typedef ::std::vector TargetList; // from their parent frames. -class SFX2_DLLPUBLIC SfxFrame +class SFX2_DLLPUBLIC SfxFrame : public SvCompatWeakBase { friend class SfxFrameIterator; friend class SfxFrameWindow_Impl; @@ -125,7 +124,6 @@ public: CreateBlankFrame(); static SfxFrame* Create( SfxObjectShell& rDoc, Window& rWindow, sal_uInt16 nViewId, bool bHidden ); - SvCompatWeakHdl* GetHdl(); Window& GetWindow() const { return *pWindow;} void CancelTransfers( bool bCancelLoadEnv = true ); bool DoClose(); @@ -209,7 +207,7 @@ private: SAL_DLLPRIVATE void Construct_Impl(); }; -SV_DECL_COMPAT_WEAK_REF( SfxFrame ) +typedef SvCompatWeakRef SfxFrameWeakRef; class SfxFrameIterator { diff --git a/include/tools/ref.hxx b/include/tools/ref.hxx index 98740f0747cf..ed34f4137666 100644 --- a/include/tools/ref.hxx +++ b/include/tools/ref.hxx @@ -189,56 +189,63 @@ public: { return nRefCount; } }; -/** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak. - This acts as a intermediary between SfxFrameWeak and SfxFrame_Impl. +template +class SvCompatWeakBase; + +/** SvCompatWeakHdl acts as a intermediary between SvCompatWeakRef and T. */ +template class SvCompatWeakHdl : public SvRefBase { - friend class SvCompatWeakBase; - void* _pObj; + friend class SvCompatWeakBase; + T* _pObj; - SvCompatWeakHdl( void* pObj ) : _pObj( pObj ) {} + SvCompatWeakHdl( T* pObj ) : _pObj( pObj ) {} public: void ResetWeakBase( ) { _pObj = 0; } - void* GetObj() { return _pObj; } + T* GetObj() { return _pObj; } }; -/** We only have one place that extends this, in sfx2/source/view/impframe.hxx, class SfxFrame_Impl, - its function is to notify the SvCompatWeakHdl when an SfxFrame_Impl object is deleted. +/** We only have one place that extends this, in include/sfx2/frame.hxx, class SfxFrame. + Its function is to notify the SvCompatWeakHdl when an SfxFrame object is deleted. */ +template class SvCompatWeakBase { - tools::SvRef _xHdl; + tools::SvRef< SvCompatWeakHdl > _xHdl; public: - // Does not use initializer due to compiler warnings, - // because the lifetime of the _xHdl object can exceed the lifetime of this class. - SvCompatWeakBase( void* pObj ) { _xHdl = new SvCompatWeakHdl( pObj ); } + /** Does not use initializer due to compiler warnings, + because the lifetime of the _xHdl object can exceed the lifetime of this class. + */ + SvCompatWeakBase( T* pObj ) { _xHdl = new SvCompatWeakHdl( pObj ); } ~SvCompatWeakBase() { _xHdl->ResetWeakBase(); } - SvCompatWeakHdl* GetHdl() { return _xHdl; } + SvCompatWeakHdl* GetHdl() { return _xHdl; } }; -#define SV_DECL_COMPAT_WEAK_REF( ClassName ) \ -class ClassName##WeakRef \ -{ \ - tools::SvRef _xHdl; \ -public: \ - inline ClassName##WeakRef( ) {} \ - inline ClassName##WeakRef( ClassName* pObj ) { \ - if( pObj ) _xHdl = pObj->GetHdl(); } \ - inline ClassName##WeakRef& operator = ( ClassName * pObj ) { \ - _xHdl = pObj ? pObj->GetHdl() : 0; return *this; } \ - inline bool Is() const { \ - return _xHdl.Is() && _xHdl->GetObj(); } \ - inline ClassName * operator -> () const { \ - return (ClassName*) ( _xHdl.Is() ? _xHdl->GetObj() : 0 ); } \ - inline ClassName * operator & () const { \ - return (ClassName*) ( _xHdl.Is() ? _xHdl->GetObj() : 0 ); } \ - inline operator ClassName * () const { \ - return (ClassName*) (_xHdl.Is() ? _xHdl->GetObj() : 0 ); } \ +/** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak. +*/ +template +class SvCompatWeakRef +{ + tools::SvRef< SvCompatWeakHdl > _xHdl; +public: + inline SvCompatWeakRef( ) {} + inline SvCompatWeakRef( T* pObj ) + { if( pObj ) _xHdl = pObj->GetHdl(); } + inline SvCompatWeakRef& operator = ( T * pObj ) + { _xHdl = pObj ? pObj->GetHdl() : 0; return *this; } + inline bool Is() const + { return _xHdl.Is() && _xHdl->GetObj(); } + inline T* operator -> () const + { return _xHdl.Is() ? _xHdl->GetObj() : 0; } + inline T* operator & () const + { return _xHdl.Is() ? _xHdl->GetObj() : 0; } + inline operator T* () const + { return _xHdl.Is() ? _xHdl->GetObj() : 0; } }; #endif diff --git a/sfx2/source/view/frame.cxx b/sfx2/source/view/frame.cxx index 63a82c8ad1a4..f28748f44386 100644 --- a/sfx2/source/view/frame.cxx +++ b/sfx2/source/view/frame.cxx @@ -79,12 +79,6 @@ TYPEINIT1_AUTOFACTORY(SfxFrameItem, SfxPoolItem); TYPEINIT1(SfxUsrAnyItem, SfxPoolItem); TYPEINIT1_AUTOFACTORY(SfxUnoFrameItem, SfxPoolItem); -SvCompatWeakHdl* SfxFrame::GetHdl() -{ - return pImp->GetHdl(); -} - - void SfxFrame::Construct_Impl() { pImp = new SfxFrame_Impl( this ); diff --git a/sfx2/source/view/frame2.cxx b/sfx2/source/view/frame2.cxx index 79cc49193902..ad99ca316cf3 100644 --- a/sfx2/source/view/frame2.cxx +++ b/sfx2/source/view/frame2.cxx @@ -296,7 +296,8 @@ SfxFrame* SfxFrame::Create( const Reference < XFrame >& i_rFrame ) } SfxFrame::SfxFrame( Window& i_rContainerWindow, bool i_bHidden ) - :pParentFrame( NULL ) + :SvCompatWeakBase( this ) + ,pParentFrame( NULL ) ,pChildArr( NULL ) ,pImp( NULL ) ,pWindow( NULL ) diff --git a/sfx2/source/view/impframe.hxx b/sfx2/source/view/impframe.hxx index 7b23889efb79..e66e59fcf5d3 100644 --- a/sfx2/source/view/impframe.hxx +++ b/sfx2/source/view/impframe.hxx @@ -33,7 +33,7 @@ class SfxViewFrame; #include #include -class SfxFrame_Impl : public SfxBroadcaster, public SvCompatWeakBase +class SfxFrame_Impl : public SfxBroadcaster { public: ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame; @@ -57,8 +57,7 @@ public: bool bMenuBarOn; SfxFrame_Impl( SfxFrame* pAntiImplP ) - :SvCompatWeakBase( pAntiImplP ) - ,nType( 0L ) + :nType( 0L ) ,pCurrentViewFrame( NULL ) ,pDescr( NULL ) ,nLocks( 0 ) -- cgit v1.2.3