Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #ifndef _CPPUHELPER_WEAKREF_HXX_
29 : : #define _CPPUHELPER_WEAKREF_HXX_
30 : :
31 : : #include <com/sun/star/uno/XInterface.hpp>
32 : : #include "cppuhelperdllapi.h"
33 : :
34 : :
35 : : namespace com
36 : : {
37 : : namespace sun
38 : : {
39 : : namespace star
40 : : {
41 : : namespace uno
42 : : {
43 : :
44 : : class OWeakRefListener;
45 : :
46 : : /** The WeakReferenceHelper holds a weak reference to an object. This object must implement
47 : : the ::com::sun::star::uno::XWeak interface. The implementation is thread safe.
48 : : */
49 : : class CPPUHELPER_DLLPUBLIC WeakReferenceHelper
50 : : {
51 : : public:
52 : : /** Default ctor. Creates an empty weak reference.
53 : : */
54 : 1170739 : inline WeakReferenceHelper() SAL_THROW(())
55 : 1170739 : : m_pImpl( 0 )
56 : 1170739 : {}
57 : :
58 : : /** Copy ctor. Initialize this reference with the same interface as in rWeakRef.
59 : :
60 : : @param rWeakRef another weak ref
61 : : */
62 : : WeakReferenceHelper( const WeakReferenceHelper & rWeakRef ) SAL_THROW(());
63 : : /** Initialize this reference with the hard interface reference xInt. If the implementation
64 : : behind xInt does not support XWeak or XInt is null then this reference will be null.
65 : :
66 : : @param xInt another hard interface reference
67 : : */
68 : : WeakReferenceHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & xInt )
69 : : SAL_THROW(());
70 : : /** Releases this reference.
71 : : */
72 : : ~WeakReferenceHelper() SAL_THROW(());
73 : :
74 : : /** Releases this reference and takes over rWeakRef.
75 : :
76 : : @param rWeakRef another weak ref
77 : : */
78 : : WeakReferenceHelper & SAL_CALL operator = ( const WeakReferenceHelper & rWeakRef ) SAL_THROW(());
79 : :
80 : : /** Releases this reference and takes over hard reference xInt.
81 : : If the implementation behind xInt does not support XWeak
82 : : or XInt is null, then this reference is null.
83 : :
84 : : @param xInt another hard reference
85 : : */
86 : : WeakReferenceHelper & SAL_CALL operator = (
87 : : const ::com::sun::star::uno::Reference<
88 : : ::com::sun::star::uno::XInterface > & xInt ) SAL_THROW(());
89 : :
90 : : /** Returns true if both weak refs reference to the same object.
91 : :
92 : : @param rObj another weak ref
93 : : @return true, if both weak refs reference to the same object.
94 : : */
95 : 55661 : inline sal_Bool SAL_CALL operator == ( const WeakReferenceHelper & rObj ) const SAL_THROW(())
96 [ + - ][ + - ]: 55661 : { return (get() == rObj.get()); }
97 : :
98 : : /** Gets a hard reference to the object.
99 : :
100 : : @return hard reference or null, if the weakly referenced interface has gone
101 : : */
102 : : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL get() const SAL_THROW(());
103 : : /** Gets a hard reference to the object.
104 : :
105 : : @return hard reference or null, if the weakly referenced interface has gone
106 : : */
107 : 23893799 : inline SAL_CALL operator Reference< XInterface > () const SAL_THROW(())
108 : 23893799 : { return get(); }
109 : :
110 : : /** Releases this reference.
111 : :
112 : : @since UDK 3.2.12
113 : : */
114 : : void SAL_CALL clear() SAL_THROW(());
115 : :
116 : : protected:
117 : : /// @cond INTERNAL
118 : : OWeakRefListener * m_pImpl;
119 : : /// @endcond
120 : : };
121 : :
122 : : /** The WeakReference<> holds a weak reference to an object. This object must implement
123 : : the ::com::sun::star::uno::XWeak interface. The implementation is thread safe.
124 : :
125 : : @tparam interface_type type of interface
126 : : */
127 : : template< class interface_type >
128 : 2354583 : class WeakReference : public WeakReferenceHelper
129 : : {
130 : : public:
131 : : /** Default ctor. Creates an empty weak reference.
132 : : */
133 : 692223 : inline WeakReference() SAL_THROW(())
134 : 692223 : : WeakReferenceHelper()
135 : 692223 : {}
136 : :
137 : : /** Copy ctor. Initialize this reference with a hard reference.
138 : :
139 : : @param rRef another hard ref
140 : : */
141 : 243046 : inline WeakReference( const Reference< interface_type > & rRef ) SAL_THROW(())
142 : 243046 : : WeakReferenceHelper( rRef )
143 : 243046 : {}
144 : :
145 : : /** Releases this reference and takes over hard reference xInt.
146 : : If the implementation behind xInt does not support XWeak
147 : : or XInt is null, then this reference is null.
148 : :
149 : : @param xInt another hard reference
150 : :
151 : : @since UDK 3.2.12
152 : : */
153 : 590016 : WeakReference & SAL_CALL operator = (
154 : : const ::com::sun::star::uno::Reference< interface_type > & xInt )
155 : : SAL_THROW(())
156 : 590016 : { WeakReferenceHelper::operator=(xInt); return *this; }
157 : :
158 : : /** Gets a hard reference to the object.
159 : :
160 : : @return hard reference or null, if the weakly referenced interface has gone
161 : : */
162 : 1031734 : inline SAL_CALL operator Reference< interface_type > () const SAL_THROW(())
163 [ + - ][ + - ]: 1031734 : { return Reference< interface_type >::query( get() ); }
[ + - ][ + - ]
[ + - ]
164 : : };
165 : :
166 : : }
167 : : }
168 : : }
169 : : }
170 : :
171 : : #endif
172 : :
173 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|