xref: /aoo42x/main/cppu/inc/com/sun/star/uno/Reference.hxx (revision a8ee967b)
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 //__________________________________________________________________________________________________
iquery(XInterface * pInterface,const Type & rType)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 >
iquery(XInterface * pInterface)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 //__________________________________________________________________________________________________
iquery_throw(XInterface * pInterface,const Type & rType)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 >
iquery_throw(XInterface * pInterface)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 >
iset_throw(interface_type * pInterface)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 >
~Reference()111 inline Reference< interface_type >::~Reference() SAL_THROW( () )
112 {
113 	if (_pInterface)
114 		_pInterface->release();
115 }
116 //__________________________________________________________________________________________________
117 template< class interface_type >
Reference()118 inline Reference< interface_type >::Reference() SAL_THROW( () )
119 {
120 	_pInterface = 0;
121 }
122 //__________________________________________________________________________________________________
123 template< class interface_type >
Reference(const Reference<interface_type> & rRef)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 >
Reference(interface_type * pInterface)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 >
Reference(interface_type * pInterface,__sal_NoAcquire)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 >
Reference(interface_type * pInterface,UnoReference_NoAcquire)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 >
Reference(const BaseReference & rRef,UnoReference_Query)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 >
Reference(XInterface * pInterface,UnoReference_Query)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 >
Reference(const Any & rAny,UnoReference_Query)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 >
Reference(const BaseReference & rRef,UnoReference_QueryThrow)172 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
173 {
174 	_pInterface = NULL;
175 	_pInterface = iquery_throw( rRef.get() );
176 }
177 //__________________________________________________________________________________________________
178 template< class interface_type >
Reference(XInterface * pInterface,UnoReference_QueryThrow)179 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
180 {
181 	_pInterface = NULL;
182 	_pInterface = iquery_throw( pInterface );
183 }
184 //__________________________________________________________________________________________________
185 template< class interface_type >
Reference(const Any & rAny,UnoReference_QueryThrow)186 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
187 {
188     _pInterface = NULL;
189     _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
190                                 ? static_cast< XInterface * >( rAny.pReserved ) : 0 );
191 }
192 //__________________________________________________________________________________________________
193 template< class interface_type >
Reference(const Reference<interface_type> & rRef,UnoReference_SetThrow)194 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
195 {
196     _pInterface = NULL;
197     _pInterface = iset_throw( rRef.get() );
198 }
199 //__________________________________________________________________________________________________
200 template< class interface_type >
Reference(interface_type * pInterface,UnoReference_SetThrow)201 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
202 {
203     _pInterface = NULL;
204     _pInterface = iset_throw( pInterface );
205 }
206 #endif
207 
208 //__________________________________________________________________________________________________
209 template< class interface_type >
clear()210 inline void Reference< interface_type >::clear() SAL_THROW( () )
211 {
212 	if (_pInterface)
213 	{
214         XInterface * const pOld = _pInterface;
215 		_pInterface = 0;
216 		pOld->release();
217 	}
218 }
219 //__________________________________________________________________________________________________
220 template< class interface_type >
set(interface_type * pInterface)221 inline sal_Bool Reference< interface_type >::set(
222 	interface_type * pInterface ) SAL_THROW( () )
223 {
224 	if (pInterface)
225 		castToXInterface(pInterface)->acquire();
226     XInterface * const pOld = _pInterface;
227 	_pInterface = castToXInterface(pInterface);
228 	if (pOld)
229 		pOld->release();
230 	return (0 != pInterface);
231 }
232 //__________________________________________________________________________________________________
233 template< class interface_type >
set(interface_type * pInterface,__sal_NoAcquire)234 inline sal_Bool Reference< interface_type >::set(
235 	interface_type * pInterface, __sal_NoAcquire ) SAL_THROW( () )
236 {
237     XInterface * const pOld = _pInterface;
238 	_pInterface = castToXInterface(pInterface);
239 	if (pOld)
240 		pOld->release();
241 	return (0 != pInterface);
242 }
243 //__________________________________________________________________________________________________
244 template< class interface_type >
set(interface_type * pInterface,UnoReference_NoAcquire)245 inline sal_Bool Reference< interface_type >::set(
246 	interface_type * pInterface, UnoReference_NoAcquire ) SAL_THROW( () )
247 {
248 	return set( pInterface, SAL_NO_ACQUIRE );
249 }
250 
251 //__________________________________________________________________________________________________
252 template< class interface_type >
set(const Reference<interface_type> & rRef)253 inline sal_Bool Reference< interface_type >::set(
254 	const Reference< interface_type > & rRef ) SAL_THROW( () )
255 {
256 	return set( castFromXInterface( rRef._pInterface ) );
257 }
258 //__________________________________________________________________________________________________
259 template< class interface_type >
set(XInterface * pInterface,UnoReference_Query)260 inline sal_Bool Reference< interface_type >::set(
261 	XInterface * pInterface, UnoReference_Query ) SAL_THROW( (RuntimeException) )
262 {
263 	return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
264 }
265 //__________________________________________________________________________________________________
266 template< class interface_type >
set(const BaseReference & rRef,UnoReference_Query)267 inline sal_Bool Reference< interface_type >::set(
268 	const BaseReference & rRef, UnoReference_Query ) SAL_THROW( (RuntimeException) )
269 {
270 	return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
271 }
272 
273 //______________________________________________________________________________
274 template< class interface_type >
set(Any const & rAny,UnoReference_Query)275 inline bool Reference< interface_type >::set(
276     Any const & rAny, UnoReference_Query )
277 {
278 	return set(
279         castFromXInterface(
280             iquery(
281                 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
282                 ? static_cast< XInterface * >( rAny.pReserved ) : 0 )),
283         SAL_NO_ACQUIRE );
284 }
285 
286 #ifndef EXCEPTIONS_OFF
287 //__________________________________________________________________________________________________
288 template< class interface_type >
set(XInterface * pInterface,UnoReference_QueryThrow)289 inline void Reference< interface_type >::set(
290 	XInterface * pInterface, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
291 {
292 	set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
293 }
294 //__________________________________________________________________________________________________
295 template< class interface_type >
set(const BaseReference & rRef,UnoReference_QueryThrow)296 inline void Reference< interface_type >::set(
297 	const BaseReference & rRef, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
298 {
299 	set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
300 }
301 
302 //______________________________________________________________________________
303 template< class interface_type >
set(Any const & rAny,UnoReference_QueryThrow)304 inline void Reference< interface_type >::set(
305     Any const & rAny, UnoReference_QueryThrow )
306 {
307 	set( castFromXInterface(
308              iquery_throw(
309                  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
310                  ? static_cast< XInterface * >( rAny.pReserved ) : 0 )),
311          SAL_NO_ACQUIRE );
312 }
313 //__________________________________________________________________________________________________
314 template< class interface_type >
set(interface_type * pInterface,UnoReference_SetThrow)315 inline void Reference< interface_type >::set(
316 	interface_type * pInterface, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
317 {
318     set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
319 }
320 //__________________________________________________________________________________________________
321 template< class interface_type >
set(const Reference<interface_type> & rRef,UnoReference_SetThrow)322 inline void Reference< interface_type >::set(
323 	const Reference< interface_type > & rRef, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
324 {
325     set( rRef.get(), UNO_SET_THROW );
326 }
327 
328 #endif
329 
330 //__________________________________________________________________________________________________
331 template< class interface_type >
operator =(interface_type * pInterface)332 inline Reference< interface_type > & Reference< interface_type >::operator = (
333 	interface_type * pInterface ) SAL_THROW( () )
334 {
335 	set( pInterface );
336 	return *this;
337 }
338 //__________________________________________________________________________________________________
339 template< class interface_type >
operator =(const Reference<interface_type> & rRef)340 inline Reference< interface_type > & Reference< interface_type >::operator = (
341 	const Reference< interface_type > & rRef ) SAL_THROW( () )
342 {
343 	set( castFromXInterface( rRef._pInterface ) );
344 	return *this;
345 }
346 
347 //__________________________________________________________________________________________________
348 template< class interface_type >
query(const BaseReference & rRef)349 inline Reference< interface_type > Reference< interface_type >::query(
350 	const BaseReference & rRef ) SAL_THROW( (RuntimeException) )
351 {
352 	return Reference< interface_type >(
353         castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
354 }
355 //__________________________________________________________________________________________________
356 template< class interface_type >
query(XInterface * pInterface)357 inline Reference< interface_type > Reference< interface_type >::query(
358 	XInterface * pInterface ) SAL_THROW( (RuntimeException) )
359 {
360 	return Reference< interface_type >(
361         castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
362 }
363 
364 //##################################################################################################
365 
366 //__________________________________________________________________________________________________
operator ==(XInterface * pInterface) const367 inline sal_Bool BaseReference::operator == ( XInterface * pInterface ) const SAL_THROW( () )
368 {
369 	if (_pInterface == pInterface)
370 		return sal_True;
371 #ifndef EXCEPTIONS_OFF
372     try
373     {
374 #endif
375         // only the query to XInterface must return the same pointer if they belong to same objects
376         Reference< XInterface > x1( _pInterface, UNO_QUERY );
377         Reference< XInterface > x2( pInterface, UNO_QUERY );
378         return (x1._pInterface == x2._pInterface);
379 #ifndef EXCEPTIONS_OFF
380     }
381     catch (RuntimeException &)
382     {
383         return sal_False;
384     }
385 #endif
386 }
387 
388 //______________________________________________________________________________
operator <(const BaseReference & rRef) const389 inline sal_Bool BaseReference::operator < (
390     const BaseReference & rRef ) const SAL_THROW( () )
391 {
392     if (_pInterface == rRef._pInterface)
393         return sal_False;
394 #if ! defined EXCEPTIONS_OFF
395     try
396     {
397 #endif
398         // only the query to XInterface must return the same pointer:
399         Reference< XInterface > x1( _pInterface, UNO_QUERY );
400         Reference< XInterface > x2( rRef, UNO_QUERY );
401         return (x1._pInterface < x2._pInterface);
402 #if ! defined EXCEPTIONS_OFF
403     }
404     catch (RuntimeException &)
405     {
406         return sal_False;
407     }
408 #endif
409 }
410 
411 //__________________________________________________________________________________________________
operator !=(XInterface * pInterface) const412 inline sal_Bool BaseReference::operator != ( XInterface * pInterface ) const SAL_THROW( () )
413 {
414 	return (! operator == ( pInterface ));
415 }
416 //__________________________________________________________________________________________________
operator ==(const BaseReference & rRef) const417 inline sal_Bool BaseReference::operator == ( const BaseReference & rRef ) const SAL_THROW( () )
418 {
419 	return operator == ( rRef._pInterface );
420 }
421 //__________________________________________________________________________________________________
operator !=(const BaseReference & rRef) const422 inline sal_Bool BaseReference::operator != ( const BaseReference & rRef ) const SAL_THROW( () )
423 {
424 	return (! operator == ( rRef._pInterface ));
425 }
426 
427 }
428 }
429 }
430 }
431 
432 #endif
433