LibreOffice
LibreOffice 5.0 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Reference.hxx
Go to the documentation of this file.
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 
23 #include <com/sun/star/uno/RuntimeException.hpp>
24 #include <com/sun/star/uno/XInterface.hpp>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <cppu/cppudllapi.h>
27 
28 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
31 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
34 
35 namespace com
36 {
37 namespace sun
38 {
39 namespace star
40 {
41 namespace uno
42 {
43 
44 
45 inline XInterface * BaseReference::iquery(
46  XInterface * pInterface, const Type & rType )
47 {
48  if (pInterface)
49  {
50  Any aRet( pInterface->queryInterface( rType ) );
51  if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
52  {
53  XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
54  aRet.pReserved = 0;
55  return pRet;
56  }
57  }
58  return 0;
59 }
60 
61 template< class interface_type >
62 inline XInterface * Reference< interface_type >::iquery(
63  XInterface * pInterface )
64 {
65  return BaseReference::iquery(pInterface, interface_type::static_type());
66 }
67 
68 inline XInterface * BaseReference::iquery_throw(
69  XInterface * pInterface, const Type & rType )
70 {
71  XInterface * pQueried = iquery( pInterface, rType );
72  if (pQueried)
73  return pQueried;
74  throw RuntimeException(
76  Reference< XInterface >( pInterface ) );
77 }
78 
79 template< class interface_type >
81  XInterface * pInterface )
82 {
84  pInterface, interface_type::static_type());
85 }
86 
87 template< class interface_type >
88 inline interface_type * Reference< interface_type >::iset_throw(
89  interface_type * pInterface )
90 {
91  if (pInterface)
92  {
93  castToXInterface(pInterface)->acquire();
94  return pInterface;
95  }
96  throw RuntimeException(
97  ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
98  NULL );
99 }
100 
101 
102 template< class interface_type >
104 {
105  if (_pInterface)
106  _pInterface->release();
107 }
108 
109 template< class interface_type >
111 {
112  _pInterface = 0;
113 }
114 
115 template< class interface_type >
117 {
118  _pInterface = rRef._pInterface;
119  if (_pInterface)
120  _pInterface->acquire();
121 }
122 
123 template< class interface_type > template< class derived_type >
125  const Reference< derived_type > & rRef,
126  typename detail::UpCast< interface_type, derived_type >::t )
127 {
128  interface_type * p = rRef.get();
129  _pInterface = p;
130  if (_pInterface)
131  _pInterface->acquire();
132 }
133 
134 template< class interface_type >
135 inline Reference< interface_type >::Reference( interface_type * pInterface )
136 {
137  _pInterface = castToXInterface(pInterface);
138  if (_pInterface)
139  _pInterface->acquire();
140 }
141 
142 template< class interface_type >
143 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
144 {
145  _pInterface = castToXInterface(pInterface);
146 }
147 
148 template< class interface_type >
150 {
151  _pInterface = castToXInterface(pInterface);
152 }
153 
154 template< class interface_type >
156 {
157  _pInterface = iquery( rRef.get() );
158 }
159 
160 template< class interface_type >
162 {
163  _pInterface = iquery( pInterface );
164 }
165 
166 template< class interface_type >
168 {
169  _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
170  ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : 0);
171 }
172 
173 template< class interface_type >
175 {
176  _pInterface = iquery_throw( rRef.get() );
177 }
178 
179 template< class interface_type >
181 {
182  _pInterface = iquery_throw( pInterface );
183 }
184 
185 template< class interface_type >
187 {
188  _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
189  ? static_cast< XInterface * >( rAny.pReserved ) : 0 );
190 }
191 
192 template< class interface_type >
194 {
195  _pInterface = castToXInterface( 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 = castToXInterface( iset_throw( pInterface ) );
202 }
203 
204 
205 template< class interface_type >
207 {
208  if (_pInterface)
209  {
210  XInterface * const pOld = _pInterface;
211  _pInterface = 0;
212  pOld->release();
213  }
214 }
215 
216 template< class interface_type >
218  interface_type * pInterface )
219 {
220  if (pInterface)
221  castToXInterface(pInterface)->acquire();
222  XInterface * const pOld = _pInterface;
223  _pInterface = castToXInterface(pInterface);
224  if (pOld)
225  pOld->release();
226  return (0 != pInterface);
227 }
228 
229 template< class interface_type >
231  interface_type * pInterface, __sal_NoAcquire )
232 {
233  XInterface * const pOld = _pInterface;
234  _pInterface = castToXInterface(pInterface);
235  if (pOld)
236  pOld->release();
237  return (0 != pInterface);
238 }
239 
240 template< class interface_type >
242  interface_type * pInterface, UnoReference_NoAcquire )
243 {
244  return set( pInterface, SAL_NO_ACQUIRE );
245 }
246 
247 
248 template< class interface_type >
250  const Reference< interface_type > & rRef )
251 {
252  return set( castFromXInterface( rRef._pInterface ) );
253 }
254 
255 template< class interface_type >
257  XInterface * pInterface, UnoReference_Query )
258 {
259  return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
260 }
261 
262 template< class interface_type >
264  const BaseReference & rRef, UnoReference_Query )
265 {
266  return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
267 }
268 
269 
270 template< class interface_type >
272  Any const & rAny, UnoReference_Query )
273 {
274  return set(
275  castFromXInterface(
276  iquery(
277  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
278  ? static_cast< XInterface * >( rAny.pReserved ) : 0 )),
279  SAL_NO_ACQUIRE );
280 }
281 
282 
283 template< class interface_type >
285  XInterface * pInterface, UnoReference_QueryThrow )
286 {
287  set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
288 }
289 
290 template< class interface_type >
292  const BaseReference & rRef, UnoReference_QueryThrow )
293 {
294  set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
295 }
296 
297 
298 template< class interface_type >
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 >
311  interface_type * pInterface, UnoReference_SetThrow )
312 {
313  set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
314 }
315 
316 template< class interface_type >
319 {
320  set( rRef.get(), UNO_SET_THROW );
321 }
322 
323 
324 template< class interface_type >
326  interface_type * pInterface )
327 {
328  set( pInterface );
329  return *this;
330 }
331 
332 template< class interface_type >
334  const Reference< interface_type > & rRef )
335 {
336  set( castFromXInterface( rRef._pInterface ) );
337  return *this;
338 }
339 
340 
341 template< class interface_type >
343  const BaseReference & rRef )
344 {
346  castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
347 }
348 
349 template< class interface_type >
351  XInterface * pInterface )
352 {
354  castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
355 }
356 
357 
358 
359 
360 inline bool BaseReference::operator == ( XInterface * pInterface ) const
361 {
362  if (_pInterface == pInterface)
363  return true;
364  try
365  {
366  // only the query to XInterface must return the same pointer if they belong to same objects
368  Reference< XInterface > x2( pInterface, UNO_QUERY );
369  return (x1._pInterface == x2._pInterface);
370  }
371  catch (RuntimeException &)
372  {
373  return false;
374  }
375 }
376 
377 
379  const BaseReference & rRef ) const
380 {
381  if (_pInterface == rRef._pInterface)
382  return false;
383  try
384  {
385  // only the query to XInterface must return the same pointer:
388  return (x1._pInterface < x2._pInterface);
389  }
390  catch (RuntimeException &)
391  {
392  return false;
393  }
394 }
395 
396 
397 inline bool BaseReference::operator != ( XInterface * pInterface ) const
398 {
399  return (! operator == ( pInterface ));
400 }
401 
402 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
403 {
404  return operator == ( rRef._pInterface );
405 }
406 
407 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
408 {
409  return (! operator == ( rRef._pInterface ));
410 }
411 
412 }
413 }
414 }
415 }
416 
417 #endif
418 
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */