Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
20 : #define INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
21 :
22 : #include <com/sun/star/uno/Reference.h>
23 : #include <com/sun/star/uno/RuntimeException.hpp>
24 : #include <com/sun/star/uno/XInterface.hpp>
25 : #include <com/sun/star/uno/genfunc.hxx>
26 :
27 : namespace com
28 : {
29 : namespace sun
30 : {
31 : namespace star
32 : {
33 : namespace uno
34 : {
35 :
36 :
37 78 : inline XInterface * BaseReference::iquery(
38 : XInterface * pInterface, const Type & rType )
39 : SAL_THROW( (RuntimeException) )
40 : {
41 78 : if (pInterface)
42 : {
43 72 : Any aRet( pInterface->queryInterface( rType ) );
44 72 : if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
45 : {
46 69 : XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
47 69 : aRet.pReserved = 0;
48 69 : return pRet;
49 3 : }
50 : }
51 9 : return 0;
52 : }
53 :
54 : template< class interface_type >
55 67 : inline XInterface * Reference< interface_type >::iquery(
56 : XInterface * pInterface ) SAL_THROW( (RuntimeException) )
57 : {
58 67 : return BaseReference::iquery(pInterface, interface_type::static_type());
59 : }
60 : extern "C" rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
61 : typelib_TypeDescriptionReference * pType )
62 : SAL_THROW_EXTERN_C();
63 : extern "C" rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
64 : typelib_TypeDescriptionReference * pType )
65 : SAL_THROW_EXTERN_C();
66 :
67 11 : inline XInterface * BaseReference::iquery_throw(
68 : XInterface * pInterface, const Type & rType )
69 : SAL_THROW( (RuntimeException) )
70 : {
71 11 : XInterface * pQueried = iquery( pInterface, rType );
72 11 : if (pQueried)
73 22 : return pQueried;
74 : throw RuntimeException(
75 : ::rtl::OUString( cppu_unsatisfied_iquery_msg( rType.getTypeLibType() ), SAL_NO_ACQUIRE ),
76 0 : Reference< XInterface >( pInterface ) );
77 : }
78 :
79 : template< class interface_type >
80 11 : inline XInterface * Reference< interface_type >::iquery_throw(
81 : XInterface * pInterface ) SAL_THROW( (RuntimeException) )
82 : {
83 : return BaseReference::iquery_throw(
84 11 : pInterface, interface_type::static_type());
85 : }
86 :
87 : template< class interface_type >
88 0 : inline interface_type * Reference< interface_type >::iset_throw(
89 : interface_type * pInterface ) SAL_THROW( (RuntimeException) )
90 : {
91 0 : if (pInterface)
92 : {
93 0 : castToXInterface(pInterface)->acquire();
94 0 : return pInterface;
95 : }
96 0 : throw RuntimeException(
97 0 : ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
98 0 : NULL );
99 : }
100 :
101 :
102 : template< class interface_type >
103 5955 : inline Reference< interface_type >::~Reference() SAL_THROW(())
104 : {
105 5955 : if (_pInterface)
106 230 : _pInterface->release();
107 5955 : }
108 :
109 : template< class interface_type >
110 4718 : inline Reference< interface_type >::Reference() SAL_THROW(())
111 : {
112 4718 : _pInterface = 0;
113 4718 : }
114 :
115 : template< class interface_type >
116 1091 : inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef ) SAL_THROW(())
117 : {
118 1091 : _pInterface = rRef._pInterface;
119 1091 : if (_pInterface)
120 23 : _pInterface->acquire();
121 1091 : }
122 :
123 : template< class interface_type > template< class derived_type >
124 0 : inline Reference< interface_type >::Reference(
125 : const Reference< derived_type > & rRef,
126 0 : typename detail::UpCast< interface_type, derived_type >::t )
127 : SAL_THROW(())
128 : {
129 0 : interface_type * p = rRef.get();
130 0 : _pInterface = p;
131 0 : if (_pInterface)
132 0 : _pInterface->acquire();
133 0 : }
134 :
135 : template< class interface_type >
136 75 : inline Reference< interface_type >::Reference( interface_type * pInterface ) SAL_THROW(())
137 : {
138 75 : _pInterface = castToXInterface(pInterface);
139 75 : if (_pInterface)
140 74 : _pInterface->acquire();
141 75 : }
142 :
143 : template< class interface_type >
144 21 : inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire ) SAL_THROW(())
145 : {
146 21 : _pInterface = castToXInterface(pInterface);
147 21 : }
148 :
149 : template< class interface_type >
150 0 : inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_NoAcquire ) SAL_THROW(())
151 : {
152 0 : _pInterface = castToXInterface(pInterface);
153 0 : }
154 :
155 : template< class interface_type >
156 24 : inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_Query ) SAL_THROW( (RuntimeException) )
157 : {
158 24 : _pInterface = iquery( rRef.get() );
159 24 : }
160 :
161 : template< class interface_type >
162 3 : inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_Query ) SAL_THROW( (RuntimeException) )
163 : {
164 3 : _pInterface = iquery( pInterface );
165 3 : }
166 :
167 : template< class interface_type >
168 12 : inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_Query ) SAL_THROW( (RuntimeException) )
169 : {
170 24 : _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
171 12 : ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : 0);
172 12 : }
173 :
174 : template< class interface_type >
175 5 : inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
176 : {
177 5 : _pInterface = iquery_throw( rRef.get() );
178 5 : }
179 :
180 : template< class interface_type >
181 0 : inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
182 : {
183 0 : _pInterface = iquery_throw( pInterface );
184 0 : }
185 :
186 : template< class interface_type >
187 5 : inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
188 : {
189 5 : _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
190 5 : ? static_cast< XInterface * >( rAny.pReserved ) : 0 );
191 5 : }
192 :
193 : template< class interface_type >
194 0 : inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
195 : {
196 0 : _pInterface = castToXInterface( iset_throw( rRef.get() ) );
197 0 : }
198 :
199 : template< class interface_type >
200 0 : inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
201 : {
202 0 : _pInterface = castToXInterface( iset_throw( pInterface ) );
203 0 : }
204 :
205 :
206 : template< class interface_type >
207 7 : inline void Reference< interface_type >::clear() SAL_THROW(())
208 : {
209 7 : if (_pInterface)
210 : {
211 1 : XInterface * const pOld = _pInterface;
212 1 : _pInterface = 0;
213 1 : pOld->release();
214 : }
215 7 : }
216 :
217 : template< class interface_type >
218 87 : inline bool Reference< interface_type >::set(
219 : interface_type * pInterface ) SAL_THROW(())
220 : {
221 87 : if (pInterface)
222 77 : castToXInterface(pInterface)->acquire();
223 87 : XInterface * const pOld = _pInterface;
224 87 : _pInterface = castToXInterface(pInterface);
225 87 : if (pOld)
226 1 : pOld->release();
227 87 : return (0 != pInterface);
228 : }
229 :
230 : template< class interface_type >
231 16 : inline bool Reference< interface_type >::set(
232 : interface_type * pInterface, __sal_NoAcquire ) SAL_THROW(())
233 : {
234 16 : XInterface * const pOld = _pInterface;
235 16 : _pInterface = castToXInterface(pInterface);
236 16 : if (pOld)
237 0 : pOld->release();
238 16 : return (0 != pInterface);
239 : }
240 :
241 : template< class interface_type >
242 0 : inline bool Reference< interface_type >::set(
243 : interface_type * pInterface, UnoReference_NoAcquire ) SAL_THROW(())
244 : {
245 0 : return set( pInterface, SAL_NO_ACQUIRE );
246 : }
247 :
248 :
249 : template< class interface_type >
250 0 : inline bool Reference< interface_type >::set(
251 : const Reference< interface_type > & rRef ) SAL_THROW(())
252 : {
253 0 : return set( castFromXInterface( rRef._pInterface ) );
254 : }
255 :
256 : template< class interface_type >
257 0 : inline bool Reference< interface_type >::set(
258 : XInterface * pInterface, UnoReference_Query ) SAL_THROW( (RuntimeException) )
259 : {
260 0 : return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
261 : }
262 :
263 : template< class interface_type >
264 4 : inline bool Reference< interface_type >::set(
265 : const BaseReference & rRef, UnoReference_Query ) SAL_THROW( (RuntimeException) )
266 : {
267 4 : return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
268 : }
269 :
270 :
271 : template< class interface_type >
272 11 : inline bool Reference< interface_type >::set(
273 : Any const & rAny, UnoReference_Query )
274 : {
275 : return set(
276 : castFromXInterface(
277 : iquery(
278 : rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
279 : ? static_cast< XInterface * >( rAny.pReserved ) : 0 )),
280 11 : SAL_NO_ACQUIRE );
281 : }
282 :
283 :
284 : template< class interface_type >
285 0 : inline void Reference< interface_type >::set(
286 : XInterface * pInterface, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
287 : {
288 0 : set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
289 0 : }
290 :
291 : template< class interface_type >
292 1 : inline void Reference< interface_type >::set(
293 : const BaseReference & rRef, UnoReference_QueryThrow ) SAL_THROW( (RuntimeException) )
294 : {
295 1 : set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
296 1 : }
297 :
298 :
299 : template< class interface_type >
300 0 : inline void Reference< interface_type >::set(
301 : Any const & rAny, UnoReference_QueryThrow )
302 : {
303 0 : set( castFromXInterface(
304 : iquery_throw(
305 : rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
306 : ? static_cast< XInterface * >( rAny.pReserved ) : 0 )),
307 0 : SAL_NO_ACQUIRE );
308 0 : }
309 :
310 : template< class interface_type >
311 0 : inline void Reference< interface_type >::set(
312 : interface_type * pInterface, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
313 : {
314 0 : set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
315 0 : }
316 :
317 : template< class interface_type >
318 0 : inline void Reference< interface_type >::set(
319 : const Reference< interface_type > & rRef, UnoReference_SetThrow ) SAL_THROW( (RuntimeException) )
320 : {
321 0 : set( rRef.get(), UNO_SET_THROW );
322 0 : }
323 :
324 :
325 : template< class interface_type >
326 26 : inline Reference< interface_type > & Reference< interface_type >::operator = (
327 : interface_type * pInterface ) SAL_THROW(())
328 : {
329 26 : set( pInterface );
330 26 : return *this;
331 : }
332 :
333 : template< class interface_type >
334 60 : inline Reference< interface_type > & Reference< interface_type >::operator = (
335 : const Reference< interface_type > & rRef ) SAL_THROW(())
336 : {
337 60 : set( castFromXInterface( rRef._pInterface ) );
338 60 : return *this;
339 : }
340 :
341 :
342 : template< class interface_type >
343 8 : inline Reference< interface_type > Reference< interface_type >::query(
344 : const BaseReference & rRef ) SAL_THROW( (RuntimeException) )
345 : {
346 : return Reference< interface_type >(
347 8 : castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
348 : }
349 :
350 : template< class interface_type >
351 5 : inline Reference< interface_type > Reference< interface_type >::query(
352 : XInterface * pInterface ) SAL_THROW( (RuntimeException) )
353 : {
354 : return Reference< interface_type >(
355 5 : castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
356 : }
357 :
358 :
359 :
360 :
361 1 : inline bool BaseReference::operator == ( XInterface * pInterface ) const SAL_THROW(())
362 : {
363 1 : if (_pInterface == pInterface)
364 0 : return true;
365 : try
366 : {
367 : // only the query to XInterface must return the same pointer if they belong to same objects
368 1 : Reference< XInterface > x1( _pInterface, UNO_QUERY );
369 2 : Reference< XInterface > x2( pInterface, UNO_QUERY );
370 2 : return (x1._pInterface == x2._pInterface);
371 : }
372 0 : catch (RuntimeException &)
373 : {
374 0 : return false;
375 : }
376 : }
377 :
378 :
379 0 : inline bool BaseReference::operator < (
380 : const BaseReference & rRef ) const SAL_THROW(())
381 : {
382 0 : if (_pInterface == rRef._pInterface)
383 0 : return false;
384 : try
385 : {
386 : // only the query to XInterface must return the same pointer:
387 0 : Reference< XInterface > x1( _pInterface, UNO_QUERY );
388 0 : Reference< XInterface > x2( rRef, UNO_QUERY );
389 0 : return (x1._pInterface < x2._pInterface);
390 : }
391 0 : catch (RuntimeException &)
392 : {
393 0 : return false;
394 : }
395 : }
396 :
397 :
398 0 : inline bool BaseReference::operator != ( XInterface * pInterface ) const SAL_THROW(())
399 : {
400 0 : return (! operator == ( pInterface ));
401 : }
402 :
403 0 : inline bool BaseReference::operator == ( const BaseReference & rRef ) const SAL_THROW(())
404 : {
405 0 : return operator == ( rRef._pInterface );
406 : }
407 :
408 1 : inline bool BaseReference::operator != ( const BaseReference & rRef ) const SAL_THROW(())
409 : {
410 1 : return (! operator == ( rRef._pInterface ));
411 : }
412 :
413 : }
414 : }
415 : }
416 : }
417 :
418 : #endif
419 :
420 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|