diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-01-25 12:59:53 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-01-27 17:19:26 +0100 |
commit | c95d91ee35ca09379a8a1d415ae77716ddeadaac (patch) | |
tree | 370b0ad6832f3aca2ee0e2ef375bb6f953e5d360 /include | |
parent | 2d8f17565ebe867210f5769851d91b2e7b612a8f (diff) |
improve subtyping when dealing with tools::WeakReference
tweak the templating to make it easier to declare a WeakReference that
points to a subclass for a weak-capable class.
Which lets us declare some fields with more specific types, and dump a
lot of unnecessary casting.
And make WeakBase be inherited from virtually, so we don't end
up with weird states where two weak refernces could point to two
different parts of the same object.
Change-Id: I3213ea27e087038457b0761b5171c7bce96e71f3
Reviewed-on: https://gerrit.libreoffice.org/48650
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include')
-rw-r--r-- | include/svx/sdr/table/tablecontroller.hxx | 4 | ||||
-rw-r--r-- | include/svx/svdedxv.hxx | 4 | ||||
-rw-r--r-- | include/svx/svdmodel.hxx | 2 | ||||
-rw-r--r-- | include/svx/svdobj.hxx | 2 | ||||
-rw-r--r-- | include/svx/svdoutl.hxx | 2 | ||||
-rw-r--r-- | include/svx/svdpage.hxx | 2 | ||||
-rw-r--r-- | include/svx/svdtext.hxx | 4 | ||||
-rw-r--r-- | include/svx/svdview.hxx | 2 | ||||
-rw-r--r-- | include/tools/weakbase.h | 24 | ||||
-rw-r--r-- | include/tools/weakbase.hxx | 47 |
10 files changed, 36 insertions, 57 deletions
diff --git a/include/svx/sdr/table/tablecontroller.hxx b/include/svx/sdr/table/tablecontroller.hxx index d621d87d2fd0..b2eeadc48007 100644 --- a/include/svx/sdr/table/tablecontroller.hxx +++ b/include/svx/sdr/table/tablecontroller.hxx @@ -116,7 +116,7 @@ public: bool isColumnSelected( sal_Int32 nColumn ); bool isRowHeader(); bool isColumnHeader(); - sdr::table::SdrTableObj* GetTableObj() { return dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() ); } + sdr::table::SdrTableObj* GetTableObj() { return mxTableObj.get(); } private: SvxTableController(SvxTableController &) = delete; void operator =(SvxTableController &) = delete; @@ -176,7 +176,7 @@ private: sdr::overlay::OverlayObjectList* mpSelectionOverlay; SdrView* mpView; - tools::WeakReference<SdrObject> mxTableObj; + tools::WeakReference<SdrTableObj> mxTableObj; SdrModel* mpModel; css::uno::Reference< css::util::XModifyListener > mxModifyListener; diff --git a/include/svx/svdedxv.hxx b/include/svx/svdedxv.hxx index 20f61dff15af..46dd040e0d5a 100644 --- a/include/svx/svdedxv.hxx +++ b/include/svx/svdedxv.hxx @@ -74,7 +74,7 @@ class SVX_DLLPUBLIC SdrObjEditView: public SdrGlueEditView, public EditViewCallb protected: // TextEdit - tools::WeakReference<SdrObject> + tools::WeakReference<SdrTextObj> mxTextEditObj; // current object in TextEdit SdrPageView* pTextEditPV; SdrOutliner* pTextEditOutliner; // outliner for the TextEdit @@ -216,7 +216,7 @@ public: bool IsTextEditInSelectionMode() const; // If sb needs the object out of the TextEdit: - SdrObject* GetTextEditObject() const { return mxTextEditObj.get(); } + SdrTextObj* GetTextEditObject() const { return mxTextEditObj.get(); } // info about TextEditPageView. Default is 0L. virtual SdrPageView* GetTextEditPageView() const override; diff --git a/include/svx/svdmodel.hxx b/include/svx/svdmodel.hxx index 5d72c59477b7..b30493328d24 100644 --- a/include/svx/svdmodel.hxx +++ b/include/svx/svdmodel.hxx @@ -141,7 +141,7 @@ public: struct SdrModelImpl; -class SVX_DLLPUBLIC SdrModel : public SfxBroadcaster, public tools::WeakBase< SdrModel > +class SVX_DLLPUBLIC SdrModel : public SfxBroadcaster, public virtual tools::WeakBase { protected: std::vector<SdrPage*> maMaPag; // master pages diff --git a/include/svx/svdobj.hxx b/include/svx/svdobj.hxx index 25ab2c824e14..007283844a7a 100644 --- a/include/svx/svdobj.hxx +++ b/include/svx/svdobj.hxx @@ -268,7 +268,7 @@ public: /// Abstract DrawObject class SvxShape; -class SVX_DLLPUBLIC SdrObject: public SfxListener, public tools::WeakBase< SdrObject > +class SVX_DLLPUBLIC SdrObject: public SfxListener, public virtual tools::WeakBase { friend class SdrObjListIter; friend class SdrVirtObj; diff --git a/include/svx/svdoutl.hxx b/include/svx/svdoutl.hxx index b13cd4b0ab6f..c1ec4636dff6 100644 --- a/include/svx/svdoutl.hxx +++ b/include/svx/svdoutl.hxx @@ -30,7 +30,7 @@ class SdrPage; class SVX_DLLPUBLIC SdrOutliner : public Outliner { protected: - tools::WeakReference<SdrObject> mpTextObj; + tools::WeakReference<SdrTextObj> mpTextObj; const SdrPage* mpVisualizedPage; public: diff --git a/include/svx/svdpage.hxx b/include/svx/svdpage.hxx index 7f9917bacb29..8cd45fbe93f5 100644 --- a/include/svx/svdpage.hxx +++ b/include/svx/svdpage.hxx @@ -350,7 +350,7 @@ public: Also it's possible to request and directly set the order number (ZOrder) of SdrObjects. */ -class SVX_DLLPUBLIC SdrPage : public SdrObjList, public tools::WeakBase< SdrPage > +class SVX_DLLPUBLIC SdrPage : public SdrObjList, public virtual tools::WeakBase { // #i9076# friend class SdrModel; diff --git a/include/svx/svdtext.hxx b/include/svx/svdtext.hxx index 4733ad4f6230..f16e16edd4e1 100644 --- a/include/svx/svdtext.hxx +++ b/include/svx/svdtext.hxx @@ -40,11 +40,11 @@ namespace sdr { namespace properties { */ class SfxStyleSheet; -class SVX_DLLPUBLIC SdrText : public tools::WeakBase< SdrText > +class SVX_DLLPUBLIC SdrText : public virtual tools::WeakBase { public: SdrText( SdrTextObj& rObject, OutlinerParaObject* pOutlinerParaObject = nullptr ); - virtual ~SdrText(); + virtual ~SdrText() override; virtual void SetModel(SdrModel* pNewModel); void ForceOutlinerParaObject( OutlinerMode nOutlMode ); diff --git a/include/svx/svdview.hxx b/include/svx/svdview.hxx index 0fc3672b562c..b289cb5b66b4 100644 --- a/include/svx/svdview.hxx +++ b/include/svx/svdview.hxx @@ -144,7 +144,7 @@ public: }; -class SVX_DLLPUBLIC SdrView: public SdrCreateView, public tools::WeakBase< SdrView > +class SVX_DLLPUBLIC SdrView: public SdrCreateView, public virtual tools::WeakBase { friend class SdrPageView; diff --git a/include/tools/weakbase.h b/include/tools/weakbase.h index bacde04eaf4b..82c16773e33c 100644 --- a/include/tools/weakbase.h +++ b/include/tools/weakbase.h @@ -22,6 +22,7 @@ #include <sal/types.h> #include <osl/diagnose.h> #include <rtl/ref.hxx> +#include <tools/toolsdllapi.h> /** the template classes in this header are helper to implement weak references to implementation objects that are not refcounted. @@ -54,15 +55,16 @@ */ namespace tools { +class WeakBase; /** private connection helper, do not use directly */ -template <class reference_type> struct WeakConnection { sal_Int32 mnRefCount; - reference_type* mpReference; + WeakBase* mpReference; - WeakConnection( reference_type* pReference ) : mnRefCount( 0 ), mpReference( pReference ) {}; + WeakConnection() : mnRefCount( 0 ), mpReference( nullptr ) {}; + WeakConnection( WeakBase* pReference ) : mnRefCount( 0 ), mpReference( pReference ) {}; void acquire() { mnRefCount++; } void release() { mnRefCount--; if( mnRefCount == 0 ) delete this; } }; @@ -118,19 +120,17 @@ public: inline WeakReference<reference_type>& operator= (WeakReference<reference_type> && handle); private: - rtl::Reference<WeakConnection< reference_type >> mpWeakConnection; + rtl::Reference<WeakConnection> mpWeakConnection; }; /** derive your implementation classes from this class if you want them to support weak references */ -template <class reference_type> -class WeakBase +class TOOLS_DLLPUBLIC WeakBase { - friend class WeakReference<reference_type>; + template<typename T> friend class WeakReference; public: - inline WeakBase(); - - inline ~WeakBase(); + WeakBase() {} + virtual ~WeakBase(); /** clears the reference pointer in all living weak references for this instance. Further created weak references will also be invalid. You should call this method in the d'tor of your derived classes for an early @@ -140,8 +140,8 @@ public: inline void clearWeak(); private: - inline WeakConnection< reference_type >* getWeakConnection(); - rtl::Reference<WeakConnection< reference_type >> mpWeakConnection; + inline WeakConnection* getWeakConnection(); + rtl::Reference<WeakConnection> mpWeakConnection; }; } diff --git a/include/tools/weakbase.hxx b/include/tools/weakbase.hxx index bee866c190bf..9fe3f32775c3 100644 --- a/include/tools/weakbase.hxx +++ b/include/tools/weakbase.hxx @@ -30,16 +30,13 @@ namespace tools template< class reference_type > inline WeakReference< reference_type >::WeakReference() { - mpWeakConnection = new WeakConnection<reference_type>( 0 ); + mpWeakConnection = new WeakConnection; } template< class reference_type > inline WeakReference< reference_type >::WeakReference( reference_type* pReference ) { - if( pReference ) - mpWeakConnection = pReference->getWeakConnection(); - else - mpWeakConnection = new WeakConnection<reference_type>( 0 ); + reset( pReference ); } template< class reference_type > @@ -57,13 +54,16 @@ inline WeakReference< reference_type >::WeakReference( WeakReference< reference_ template< class reference_type > inline bool WeakReference< reference_type >::is() const { - return mpWeakConnection->mpReference != 0; + return mpWeakConnection->mpReference != nullptr; } template< class reference_type > inline reference_type * WeakReference< reference_type >::get() const { - return mpWeakConnection->mpReference; + auto pWeakBase = mpWeakConnection->mpReference; + auto pRet = dynamic_cast<reference_type *>(pWeakBase); + assert((pWeakBase && pRet) || (!pWeakBase && !pRet)); + return pRet; } template< class reference_type > @@ -72,14 +72,13 @@ inline void WeakReference< reference_type >::reset( reference_type* pReference ) if( pReference ) mpWeakConnection = pReference->getWeakConnection(); else - mpWeakConnection = new WeakConnection<reference_type>( 0 ); + mpWeakConnection = new WeakConnection; } template< class reference_type > inline reference_type * WeakReference< reference_type >::operator->() const { - OSL_PRECOND(mpWeakConnection.is(), "tools::WeakReference::operator->() : null body"); - return mpWeakConnection->mpReference; + return get(); } template< class reference_type > @@ -117,9 +116,7 @@ inline WeakReference<reference_type>& WeakReference<reference_type>::operator= ( const WeakReference<reference_type>& rReference) { if (&rReference != this) - { mpWeakConnection = rReference.mpWeakConnection; - } return *this; } @@ -131,34 +128,16 @@ inline WeakReference<reference_type>& WeakReference<reference_type>::operator= ( return *this; } -template< class reference_type > -inline WeakBase< reference_type >::WeakBase() -{ -} - -template< class reference_type > -inline WeakBase< reference_type >::~WeakBase() +inline void WeakBase::clearWeak() { if( mpWeakConnection.is() ) - { - mpWeakConnection->mpReference = 0; - } + mpWeakConnection->mpReference = nullptr; } -template< class reference_type > -inline void WeakBase< reference_type >::clearWeak() -{ - if( mpWeakConnection.is() ) - mpWeakConnection->mpReference = 0; -} - -template< class reference_type > -inline WeakConnection< reference_type >* WeakBase< reference_type >::getWeakConnection() +inline WeakConnection* WeakBase::getWeakConnection() { if( !mpWeakConnection.is() ) - { - mpWeakConnection = new WeakConnection< reference_type >( static_cast< reference_type* >( this ) ); - } + mpWeakConnection = new WeakConnection( this ); return mpWeakConnection.get(); } |