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