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/tools/weakbase.hxx | |
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/tools/weakbase.hxx')
-rw-r--r-- | include/tools/weakbase.hxx | 47 |
1 files changed, 13 insertions, 34 deletions
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(); } |