summaryrefslogtreecommitdiff
path: root/include/tools/weakbase.h
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2018-01-25 12:59:53 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2018-01-27 17:19:26 +0100
commitc95d91ee35ca09379a8a1d415ae77716ddeadaac (patch)
tree370b0ad6832f3aca2ee0e2ef375bb6f953e5d360 /include/tools/weakbase.h
parent2d8f17565ebe867210f5769851d91b2e7b612a8f (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.h')
-rw-r--r--include/tools/weakbase.h24
1 files changed, 12 insertions, 12 deletions
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;
};
}