xref: /aoo41x/main/cppu/inc/com/sun/star/uno/Reference.hxx (revision c6ed87c9)
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 _COM_SUN_STAR_UNO_REFERENCE_HXX_
24 #define _COM_SUN_STAR_UNO_REFERENCE_HXX_
25 
26 #include <com/sun/star/uno/Reference.h>
27 #include <com/sun/star/uno/RuntimeException.hpp>
28 #ifndef _COM_SUN_STAR_UNO_XINTERFACE_HDL_
29 #include <com/sun/star/uno/XInterface.hdl>
30 #endif
31 #include <com/sun/star/uno/genfunc.hxx>
32 
33 namespace com
34 {
35 namespace sun
36 {
37 namespace star
38 {
39 namespace uno
40 {
41 
42 //__________________________________________________________________________________________________
43 inline XInterface * BaseReference::iquery(
44 	XInterface * pInterface, const Type & rType )
45     SAL_THROW( (RuntimeException) )
46 {
47 	if (pInterface)
48 	{
49 		Any aRet( pInterface->queryInterface( rType ) );
50 		if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
51 		{
52 			XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
53 			aRet.pReserved = 0;
54 			return pRet;
55 		}
56 	}
57 	return 0;
58 }
59 //__________________________________________________________________________________________________
60 template< class interface_type >
61 inline XInterface * Reference< interface_type >::iquery(
62 	XInterface * pInterface ) SAL_THROW( (RuntimeException) )
63 {
64     return BaseReference::iquery(pInterface, interface_type::static_type());
65 }
66 #ifndef EXCEPTIONS_OFF
67 extern "C" rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
68     typelib_TypeDescriptionReference * pType )
69     SAL_THROW_EXTERN_C();
70 extern "C" rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
71     typelib_TypeDescriptionReference * pType )
72     SAL_THROW_EXTERN_C();
73 //__________________________________________________________________________________________________
74 inline XInterface * BaseReference::iquery_throw(
75 	XInterface * pInterface, const Type & rType )
76     SAL_THROW( (RuntimeException) )
77 {
78     XInterface * pQueried = iquery( pInterface, rType );
79 	if (pQueried)
80         return pQueried;
81     throw RuntimeException(
82         ::rtl::OUString( cppu_unsatisfied_iquery_msg( rType.getTypeLibType() ), SAL_NO_ACQUIRE ),
83         Reference< XInterface >( pInterface ) );
84 }
85 //__________________________________________________________________________________________________
86 template< class interface_type >
87 inline XInterface * Reference< interface_type >::iquery_throw(
88 	XInterface * pInterface ) SAL_THROW( (RuntimeException) )
89 {
90     return BaseReference::iquery_throw(
91         pInterface, interface_type::static_type());
92 }
93 //__________________________________________________________________________________________________
94 template< class interface_type >
95 inline interface_type * Reference< interface_type >::iset_throw(
96 	interface_type * pInterface ) SAL_THROW( (RuntimeException) )
97 {
98     if (pInterface)
99     {
100         pInterface->acquire();
101         return pInterface;
102     }
103     throw RuntimeException(
104         ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
105         NULL );
106 }
107 #endif
108 
109 //__________________________________________________________________________________________________
110 template< class interface_type >
111 inline Reference< interface_type >::~Reference() SAL_THROW( () )
112 {
113 	if (_pInterface)
114 		_pInterface->release();
115 }
116 //__________________________________________________________________________________________________
117 template< class interface_type >
118 inline Reference< interface_type >::Reference() SAL_THROW( () )
119 {
120 	_pInterface = 0;
121 }
122 //__________________________________________________________________________________________________
123 template< class interface_type >
124 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef ) SAL_THROW( () )
125 {
126 	_pInterface = rRef._pInterface;
127 	if (_pInterface)
128 		_pInterface->acquire();
129 }
130 //__________________________________________________________________________________________________
131 template< class interface_type >
132 inline Reference< interface_type >::Reference( interface_type * pInterface ) SAL_THROW( () )
133 {
134 	_pInterface = castToXInterface(pInterface);
135 	if (_pInterface)
136 		_pInterface->acquire();
137 }
138 //__________________________________________________________________________________________________
139 template< class interface_type >
140 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire ) SAL_THROW( () )
141 {
142 	_pInterface = castToXInterface(pInterface);
143 }
144 //__________________________________________________________________________________________________
145 template< class interface_type >
146 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_NoAcquire ) SAL_THROW( () )
147 {
148 	_pInterface = castToXInterface(pInterface);
149 }
150 //__________________________________________________________________________________________________
151 template< class interface_type >
152 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_Query ) SAL_THROW( (RuntimeException) )
153 {
154 	_pInterface = iquery( rRef.get() );
155 }
156 //__________________________________________________________________________________________________
157 template< class interface_type >
158 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_Query ) SAL_THROW( (RuntimeException) )
159 {
160 	_pInterface = iquery( pInterface );
161 }
162 //__________________________________________________________________________________________________
163 template< class interface_type >
164 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_Query ) SAL_THROW( (RuntimeException) )
165 {
166     _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
167                    ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : 0);
168 }
169 #ifndef EXCEPTIONS_OFF
170 //__________________________________________________________________________________________________
171 template< class interface_type >
172 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
173 {
174 	_pInterface = iquery_throw( rRef.get() );
175 }
176 //__________________________________________________________________________________________________
177 template< class interface_type >
178 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
179 {
180 	_pInterface = iquery_throw( pInterface );
181 }
182 //__________________________________________________________________________________________________
183 template< class interface_type >
184 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
185 {
186     _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
187                                 ? static_cast< XInterface * >( rAny.pReserved ) : 0 );
188 }
189 //__________________________________________________________________________________________________
190 template< class interface_type >
191 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
192 {
193     _pInterface = iset_throw( rRef.get() );
194 }
195 //__________________________________________________________________________________________________
196 template< class interface_type >
197 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
198 {
199     _pInterface = iset_throw( pInterface );
200 }
201 #endif
202 
203 //__________________________________________________________________________________________________
204 template< class interface_type >
205 inline void Reference< interface_type >::clear() SAL_THROW( () )
206 {
207 	if (_pInterface)
208 	{
209         XInterface * const pOld = _pInterface;
210 		_pInterface = 0;
211 		pOld->release();
212 	}
213 }
214 //__________________________________________________________________________________________________
215 template< class interface_type >
216 inline sal_Bool Reference< interface_type >::set(
217 	interface_type * pInterface ) SAL_THROW( () )
218 {
219 	if (pInterface)
220 		castToXInterface(pInterface)->acquire();
221     XInterface * const pOld = _pInterface;
222 	_pInterface = castToXInterface(pInterface);
223 	if (pOld)
224 		pOld->release();
225 	return (0 != pInterface);
226 }
227 //__________________________________________________________________________________________________
228 template< class interface_type >
229 inline sal_Bool Reference< interface_type >::set(
230 	interface_type * pInterface, __sal_NoAcquire ) SAL_THROW( () )
231 {
232     XInterface * const pOld = _pInterface;
233 	_pInterface = castToXInterface(pInterface);
234 	if (pOld)
235 		pOld->release();
236 	return (0 != pInterface);
237 }
238 //__________________________________________________________________________________________________
239 template< class interface_type >
240 inline sal_Bool Reference< interface_type >::set(
241 	interface_type * pInterface, UnoReference_NoAcquire ) SAL_THROW( () )
242 {
243 	return set( pInterface, SAL_NO_ACQUIRE );
244 }
245 
246 //__________________________________________________________________________________________________
247 template< class interface_type >
248 inline sal_Bool Reference< interface_type >::set(
249 	const Reference< interface_type > & rRef ) SAL_THROW( () )
250 {
251 	return set( castFromXInterface( rRef._pInterface ) );
252 }
253 //__________________________________________________________________________________________________
254 template< class interface_type >
255 inline sal_Bool Reference< interface_type >::set(
256 	XInterface * pInterface, UnoReference_Query ) SAL_THROW( (RuntimeException) )
257 {
258 	return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
259 }
260 //__________________________________________________________________________________________________
261 template< class interface_type >
262 inline sal_Bool Reference< interface_type >::set(
263 	const BaseReference & rRef, UnoReference_Query ) SAL_THROW( (RuntimeException) )
264 {
265 	return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
266 }
267 
268 //______________________________________________________________________________
269 template< class interface_type >
270 inline bool Reference< interface_type >::set(
271     Any const & rAny, UnoReference_Query )
272 {
273 	return set(
274         castFromXInterface(
275             iquery(
276                 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
277                 ? static_cast< XInterface * >( rAny.pReserved ) : 0 )),
278         SAL_NO_ACQUIRE );
279 }
280 
281 #ifndef EXCEPTIONS_OFF
282 //__________________________________________________________________________________________________
283 template< class interface_type >
284 inline void Reference< interface_type >::set(
285 	XInterface * pInterface, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
286 {
287 	set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
288 }
289 //__________________________________________________________________________________________________
290 template< class interface_type >
291 inline void Reference< interface_type >::set(
292 	const BaseReference & rRef, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
293 {
294 	set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
295 }
296 
297 //______________________________________________________________________________
298 template< class interface_type >
299 inline void Reference< interface_type >::set(
300     Any const & rAny, UnoReference_QueryThrow )
301 {
302 	set( castFromXInterface(
303              iquery_throw(
304                  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
305                  ? static_cast< XInterface * >( rAny.pReserved ) : 0 )),
306          SAL_NO_ACQUIRE );
307 }
308 //__________________________________________________________________________________________________
309 template< class interface_type >
310 inline void Reference< interface_type >::set(
311 	interface_type * pInterface, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
312 {
313     set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
314 }
315 //__________________________________________________________________________________________________
316 template< class interface_type >
317 inline void Reference< interface_type >::set(
318 	const Reference< interface_type > & rRef, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
319 {
320     set( rRef.get(), UNO_SET_THROW );
321 }
322 
323 #endif
324 
325 //__________________________________________________________________________________________________
326 template< class interface_type >
327 inline Reference< interface_type > & Reference< interface_type >::operator = (
328 	interface_type * pInterface ) SAL_THROW( () )
329 {
330 	set( pInterface );
331 	return *this;
332 }
333 //__________________________________________________________________________________________________
334 template< class interface_type >
335 inline Reference< interface_type > & Reference< interface_type >::operator = (
336 	const Reference< interface_type > & rRef ) SAL_THROW( () )
337 {
338 	set( castFromXInterface( rRef._pInterface ) );
339 	return *this;
340 }
341 
342 //__________________________________________________________________________________________________
343 template< class interface_type >
344 inline Reference< interface_type > Reference< interface_type >::query(
345 	const BaseReference & rRef ) SAL_THROW( (RuntimeException) )
346 {
347 	return Reference< interface_type >(
348         castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
349 }
350 //__________________________________________________________________________________________________
351 template< class interface_type >
352 inline Reference< interface_type > Reference< interface_type >::query(
353 	XInterface * pInterface ) SAL_THROW( (RuntimeException) )
354 {
355 	return Reference< interface_type >(
356         castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
357 }
358 
359 //##################################################################################################
360 
361 //__________________________________________________________________________________________________
362 inline sal_Bool BaseReference::operator == ( XInterface * pInterface ) const SAL_THROW( () )
363 {
364 	if (_pInterface == pInterface)
365 		return sal_True;
366 #ifndef EXCEPTIONS_OFF
367     try
368     {
369 #endif
370         // only the query to XInterface must return the same pointer if they belong to same objects
371         Reference< XInterface > x1( _pInterface, UNO_QUERY );
372         Reference< XInterface > x2( pInterface, UNO_QUERY );
373         return (x1._pInterface == x2._pInterface);
374 #ifndef EXCEPTIONS_OFF
375     }
376     catch (RuntimeException &)
377     {
378         return sal_False;
379     }
380 #endif
381 }
382 
383 //______________________________________________________________________________
384 inline sal_Bool BaseReference::operator < (
385     const BaseReference & rRef ) const SAL_THROW( () )
386 {
387     if (_pInterface == rRef._pInterface)
388         return sal_False;
389 #if ! defined EXCEPTIONS_OFF
390     try
391     {
392 #endif
393         // only the query to XInterface must return the same pointer:
394         Reference< XInterface > x1( _pInterface, UNO_QUERY );
395         Reference< XInterface > x2( rRef, UNO_QUERY );
396         return (x1._pInterface < x2._pInterface);
397 #if ! defined EXCEPTIONS_OFF
398     }
399     catch (RuntimeException &)
400     {
401         return sal_False;
402     }
403 #endif
404 }
405 
406 //__________________________________________________________________________________________________
407 inline sal_Bool BaseReference::operator != ( XInterface * pInterface ) const SAL_THROW( () )
408 {
409 	return (! operator == ( pInterface ));
410 }
411 //__________________________________________________________________________________________________
412 inline sal_Bool BaseReference::operator == ( const BaseReference & rRef ) const SAL_THROW( () )
413 {
414 	return operator == ( rRef._pInterface );
415 }
416 //__________________________________________________________________________________________________
417 inline sal_Bool BaseReference::operator != ( const BaseReference & rRef ) const SAL_THROW( () )
418 {
419 	return (! operator == ( rRef._pInterface ));
420 }
421 
422 }
423 }
424 }
425 }
426 
427 #endif
428