1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _CPPUHELPER_WEAKREF_HXX_
24 #define _CPPUHELPER_WEAKREF_HXX_
25 
26 #include <com/sun/star/uno/XInterface.hpp>
27 
28 
29 namespace com
30 {
31 namespace sun
32 {
33 namespace star
34 {
35 namespace uno
36 {
37 
38 /** @internal */
39 class OWeakRefListener;
40 
41 /** The WeakReferenceHelper holds a weak reference to an object. This object must implement
42     the ::com::sun::star::uno::XWeak interface.  The implementation is thread safe.
43 */
44 class WeakReferenceHelper
45 {
46 public:
47 	/** Default ctor.  Creates an empty weak reference.
48     */
49 	inline WeakReferenceHelper() SAL_THROW( () )
50 		: m_pImpl( 0 )
51 		{}
52 
53 	/** Copy ctor.  Initialize this reference with the same interface as in rWeakRef.
54 
55         @param rWeakRef another weak ref
56     */
57 	WeakReferenceHelper( const WeakReferenceHelper & rWeakRef ) SAL_THROW( () );
58 	/** Initialize this reference with the hard interface reference xInt. If the implementation
59         behind xInt does not support XWeak or XInt is null then this reference will be null.
60 
61         @param xInt another hard interface reference
62     */
63 	WeakReferenceHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & xInt )
64 		SAL_THROW( () );
65 	/** Releases this reference.
66 	*/
67 	~WeakReferenceHelper() SAL_THROW( () );
68 
69 	/** Releases this reference and takes over rWeakRef.
70 
71         @param rWeakRef another weak ref
72     */
73 	WeakReferenceHelper & SAL_CALL operator = ( const WeakReferenceHelper & rWeakRef ) SAL_THROW( () );
74 
75     /** Releases this reference and takes over hard reference xInt.
76         If the implementation behind xInt does not support XWeak
77         or XInt is null, then this reference is null.
78 
79         @param xInt another hard reference
80     */
81     WeakReferenceHelper & SAL_CALL operator = (
82             const ::com::sun::star::uno::Reference<
83                 ::com::sun::star::uno::XInterface > & xInt ) SAL_THROW( () );
84 
85 	/** Returns true if both weak refs reference to the same object.
86 
87         @param rObj another weak ref
88         @return true, if both weak refs reference to the same object.
89     */
operator ==(const WeakReferenceHelper & rObj) const90     inline sal_Bool SAL_CALL operator == ( const WeakReferenceHelper & rObj ) const SAL_THROW( () )
91 		{ return (get() == rObj.get()); }
92 
93 	/**  Gets a hard reference to the object.
94 
95          @return hard reference or null, if the weakly referenced interface has gone
96     */
97 	::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL get() const SAL_THROW( () );
98 	/**  Gets a hard reference to the object.
99 
100          @return hard reference or null, if the weakly referenced interface has gone
101     */
operator Reference<XInterface>() const102     inline SAL_CALL operator Reference< XInterface > () const SAL_THROW( () )
103 		{ return get(); }
104 
105     /** Releases this reference.
106 
107         @since UDK 3.2.12
108     */
109     void SAL_CALL clear() SAL_THROW( () );
110 
111 protected:
112 	/** @internal */
113 	OWeakRefListener * m_pImpl;
114 };
115 
116 /** The WeakReference<> holds a weak reference to an object. This object must implement
117     the ::com::sun::star::uno::XWeak interface.  The implementation is thread safe.
118 
119     @tplparam interface_type type of interface
120 */
121 template< class interface_type >
122 class WeakReference : public WeakReferenceHelper
123 {
124 public:
125 	/** Default ctor.  Creates an empty weak reference.
126     */
127 	inline WeakReference() SAL_THROW( () )
128 		: WeakReferenceHelper()
129 		{}
130 
131 	/** Copy ctor.  Initialize this reference with a hard reference.
132 
133         @param rRef another hard ref
134     */
135 	inline WeakReference( const Reference< interface_type > & rRef ) SAL_THROW( () )
136 		: WeakReferenceHelper( rRef )
137 		{}
138 
139     /** Releases this reference and takes over hard reference xInt.
140         If the implementation behind xInt does not support XWeak
141         or XInt is null, then this reference is null.
142 
143         @param xInt another hard reference
144 
145         @since UDK 3.2.12
146     */
operator =(const::com::sun::star::uno::Reference<interface_type> & xInt)147     WeakReference & SAL_CALL operator = (
148             const ::com::sun::star::uno::Reference< interface_type > & xInt )
149         SAL_THROW( () )
150         { WeakReferenceHelper::operator=(xInt); return *this; }
151 
152 	/**  Gets a hard reference to the object.
153 
154          @return hard reference or null, if the weakly referenced interface has gone
155     */
operator Reference<interface_type>() const156 	inline SAL_CALL operator Reference< interface_type > () const SAL_THROW( () )
157 		{ return Reference< interface_type >::query( get() ); }
158 };
159 
160 }
161 }
162 }
163 }
164 
165 #endif
166