1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_TOOLS_WEAKBASE_H
20 #define INCLUDED_TOOLS_WEAKBASE_H
22 #include <sal/types.h>
23 #include <osl/diagnose.h>
24 #include <rtl/ref.hxx>
26 /** the template classes in this header are helper to implement weak references
27 to implementation objects that are not refcounted.
29 THIS IS NOT THREADSAFE
31 Use this only to have 'safe' pointers to implementation objects that you
32 don't own but that you reference with a pointer.
36 class ImplClass : public tools::WeakBase< ImplClass >
38 ~ImplClass() { clearWeek(); } // not needed but safer, see method description
44 tools::WeakReference< ImplClass > mxWeakRef;
46 UserClass( ImplClass* pOjbect ) : mxWeakRef( pObject ) {}
51 mxWeakRef->DoSomethingMore();
58 /** private connection helper, do not use directly */
59 template <class reference_type
>
63 reference_type
* mpReference
;
65 WeakConnection( reference_type
* pReference
) : mnRefCount( 0 ), mpReference( pReference
) {};
66 void acquire() { mnRefCount
++; }
67 void release() { mnRefCount
--; if( mnRefCount
== 0 ) delete this; }
70 /** template implementation to hold a weak reference to an instance of type reference_type */
71 template <class reference_type
>
72 class SAL_WARN_UNUSED WeakReference
75 /** constructs an empty reference */
76 inline WeakReference();
78 /** constructs a reference with a pointer to a class derived from WeakBase */
79 inline WeakReference( reference_type
* pReference
);
81 /** constructs a reference from another reference */
82 inline WeakReference( const WeakReference
< reference_type
>& rWeakRef
);
84 /** constructs a reference from another reference */
85 inline WeakReference( WeakReference
< reference_type
>&& rWeakRef
);
87 /** returns true if the reference object is not null and still alive */
88 inline bool is() const;
90 /** returns the pointer to the reference object or null */
91 inline reference_type
* get() const;
93 /** sets this reference to the given object or null */
94 inline void reset( reference_type
* pReference
);
96 /** returns the pointer to the reference object or null */
97 inline reference_type
* operator->() const;
99 /** returns true if this instance references pReferenceObject */
100 inline bool operator== (const reference_type
* pReferenceObject
) const;
102 /** returns true if this instance and the given weakref reference the same object */
103 inline bool operator== (const WeakReference
<reference_type
> & handle
) const;
105 /** only needed for using this class with stl containers */
106 inline bool operator!= (const WeakReference
<reference_type
> & handle
) const;
108 /** only needed for using this class with stl containers */
109 inline bool operator< (const WeakReference
<reference_type
> & handle
) const;
111 /** only needed for using this class with stl containers */
112 inline bool operator> (const WeakReference
<reference_type
> & handle
) const;
114 /** the assignment operator */
115 inline WeakReference
<reference_type
>& operator= (const WeakReference
<reference_type
> & handle
);
117 /** the move assignment operator */
118 inline WeakReference
<reference_type
>& operator= (WeakReference
<reference_type
> && handle
);
121 rtl::Reference
<WeakConnection
< reference_type
>> mpWeakConnection
;
124 /** derive your implementation classes from this class if you want them to support weak references */
125 template <class reference_type
>
128 friend class WeakReference
<reference_type
>;
134 /** clears the reference pointer in all living weak references for this instance.
135 Further created weak references will also be invalid.
136 You should call this method in the d'tor of your derived classes for an early
137 invalidate of all living weak references while your object is already inside
140 inline void clearWeak();
143 inline WeakConnection
< reference_type
>* getWeakConnection();
144 rtl::Reference
<WeakConnection
< reference_type
>> mpWeakConnection
;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */