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