LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
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 
20 /*
21  * This file is part of LibreOffice published API.
22  */
23 #ifndef INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
24 #define INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
25 
26 #include "sal/config.h"
27 
28 #include <cstddef>
29 #include <ostream>
30 
32 #include "com/sun/star/uno/RuntimeException.hpp"
33 #include "com/sun/star/uno/XInterface.hpp"
34 #include "com/sun/star/uno/Any.hxx"
35 #include "cppu/cppudllapi.h"
36 
37 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
40 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
43 
44 namespace com
45 {
46 namespace sun
47 {
48 namespace star
49 {
50 namespace uno
51 {
52 
53 
54 inline XInterface * BaseReference::iquery(
55  XInterface * pInterface, const Type & rType )
56 {
57  if (pInterface)
58  {
59  Any aRet( pInterface->queryInterface( rType ) );
60  if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
61  {
62  XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
63  aRet.pReserved = NULL;
64  return pRet;
65  }
66  }
67  return NULL;
68 }
69 
70 template< class interface_type >
71 inline XInterface * Reference< interface_type >::iquery(
72  XInterface * pInterface )
73 {
74  return BaseReference::iquery(pInterface, interface_type::static_type());
75 }
76 
77 inline XInterface * BaseReference::iquery_throw(
78  XInterface * pInterface, const Type & rType )
79 {
80  XInterface * pQueried = iquery( pInterface, rType );
81  if (pQueried)
82  return pQueried;
83  throw RuntimeException(
85  Reference< XInterface >( pInterface ) );
86 }
87 
88 template< class interface_type >
90  XInterface * pInterface )
91 {
93  pInterface, interface_type::static_type());
94 }
95 
96 template< class interface_type >
97 inline interface_type * Reference< interface_type >::iset_throw(
98  interface_type * pInterface )
99 {
100  if (pInterface)
101  {
102  castToXInterface(pInterface)->acquire();
103  return pInterface;
104  }
105  throw RuntimeException(
106  ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
107  NULL );
108 }
109 
110 template< class interface_type >
112 {
113  if (_pInterface)
114  _pInterface->release();
115 }
116 
117 template< class interface_type >
119 {
120  _pInterface = NULL;
121 }
122 
123 template< class interface_type >
125 {
126  _pInterface = rRef._pInterface;
127  if (_pInterface)
128  _pInterface->acquire();
129 }
130 
131 #if defined LIBO_INTERNAL_ONLY
132 template< class interface_type >
134 {
135  _pInterface = rRef._pInterface;
136  rRef._pInterface = nullptr;
137 }
138 
139 template< class interface_type > template< class derived_type >
141  const Reference< derived_type > & rRef,
142  std::enable_if_t<
143  std::is_base_of_v<interface_type, derived_type>
144  && !std::is_same_v<interface_type, XInterface>, void *>)
145 {
146  interface_type * p = rRef.get();
147  _pInterface = castToXInterface(p);
148  if (_pInterface)
149  _pInterface->acquire();
150 }
151 #endif
152 
153 template< class interface_type >
154 inline Reference< interface_type >::Reference( interface_type * pInterface )
155 {
156  _pInterface = castToXInterface(pInterface);
157  if (_pInterface)
158  _pInterface->acquire();
159 }
160 
161 template< class interface_type >
162 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
163 {
164  _pInterface = castToXInterface(pInterface);
165 }
166 
167 template< class interface_type >
169 {
170  _pInterface = castToXInterface(pInterface);
171 }
172 
173 template< class interface_type >
175 {
176  _pInterface = iquery( rRef.get() );
177 }
178 
179 template< class interface_type >
181 {
182  _pInterface = iquery( pInterface );
183 }
184 
185 template< class interface_type >
187 {
188  _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
189  ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
190 }
191 
192 template< class interface_type >
194 {
195  _pInterface = iquery_throw( rRef.get() );
196 }
197 
198 template< class interface_type >
200 {
201  _pInterface = iquery_throw( pInterface );
202 }
203 
204 template< class interface_type >
206 {
207  _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
208  ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
209 }
210 
211 template< class interface_type >
213 {
214  _pInterface = castToXInterface( iset_throw( rRef.get() ) );
215 }
216 
217 template< class interface_type >
218 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
219 {
220  _pInterface = castToXInterface( iset_throw( pInterface ) );
221 }
222 
223 
224 template< class interface_type >
226 {
227  if (_pInterface)
228  {
229  XInterface * const pOld = _pInterface;
230  _pInterface = NULL;
231  pOld->release();
232  }
233 }
234 
235 template< class interface_type >
237  interface_type * pInterface )
238 {
239  if (pInterface)
240  castToXInterface(pInterface)->acquire();
241  XInterface * const pOld = _pInterface;
242  _pInterface = castToXInterface(pInterface);
243  if (pOld)
244  pOld->release();
245  return (NULL != pInterface);
246 }
247 
248 template< class interface_type >
250  interface_type * pInterface, __sal_NoAcquire )
251 {
252  XInterface * const pOld = _pInterface;
253  _pInterface = castToXInterface(pInterface);
254  if (pOld)
255  pOld->release();
256  return (NULL != pInterface);
257 }
258 
259 template< class interface_type >
261  interface_type * pInterface, UnoReference_NoAcquire )
262 {
263  return set( pInterface, SAL_NO_ACQUIRE );
264 }
265 
266 
267 template< class interface_type >
269  const Reference< interface_type > & rRef )
270 {
271  return set( castFromXInterface( rRef._pInterface ) );
272 }
273 
274 template< class interface_type >
276  XInterface * pInterface, UnoReference_Query )
277 {
278  return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
279 }
280 
281 template< class interface_type >
283  const BaseReference & rRef, UnoReference_Query )
284 {
285  return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
286 }
287 
288 
289 template< class interface_type >
291  Any const & rAny, UnoReference_Query )
292 {
293  return set(
294  castFromXInterface(
295  iquery(
296  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
297  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
298  SAL_NO_ACQUIRE );
299 }
300 
301 
302 template< class interface_type >
304  XInterface * pInterface, UnoReference_QueryThrow )
305 {
306  set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
307 }
308 
309 template< class interface_type >
311  const BaseReference & rRef, UnoReference_QueryThrow )
312 {
313  set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
314 }
315 
316 
317 template< class interface_type >
319  Any const & rAny, UnoReference_QueryThrow )
320 {
321  set( castFromXInterface(
322  iquery_throw(
323  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
324  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
325  SAL_NO_ACQUIRE );
326 }
327 
328 template< class interface_type >
330  interface_type * pInterface, UnoReference_SetThrow )
331 {
332  set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
333 }
334 
335 template< class interface_type >
338 {
339  set( rRef.get(), UNO_SET_THROW );
340 }
341 
342 
343 template< class interface_type >
345  interface_type * pInterface )
346 {
347  set( pInterface );
348  return *this;
349 }
350 
351 template< class interface_type >
353  const Reference< interface_type > & rRef )
354 {
355  set( castFromXInterface( rRef._pInterface ) );
356  return *this;
357 }
358 
359 #if defined LIBO_INTERNAL_ONLY
360 template< class interface_type >
362  Reference< interface_type > && rRef ) noexcept
363 {
364  if (_pInterface)
365  _pInterface->release();
366  _pInterface = rRef._pInterface;
367  rRef._pInterface = nullptr;
368  return *this;
369 }
370 #endif
371 
372 template< class interface_type >
374  const BaseReference & rRef )
375 {
377  castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
378 }
379 
380 template< class interface_type >
382  XInterface * pInterface )
383 {
385  castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
386 }
387 
388 #if defined LIBO_INTERNAL_ONLY
389 template< class interface_type > template< class other_type >
391 {
392  return Reference< other_type >(*this, UNO_QUERY);
393 }
394 
395 template< class interface_type > template< class other_type >
396 inline Reference< other_type > Reference< interface_type >::queryThrow() const
397 {
398  return Reference< other_type >(*this, UNO_QUERY_THROW);
399 }
400 
401 template< class interface_type >
402 inline Reference< interface_type > Any::query() const
403 {
404  return Reference< interface_type >(*this, UNO_QUERY);
405 }
406 
407 template< class interface_type >
408 inline Reference< interface_type > Any::queryThrow() const
409 {
410  return Reference< interface_type >(*this, UNO_QUERY_THROW);
411 }
412 #endif
413 
414 
415 inline bool BaseReference::operator == ( XInterface * pInterface ) const
416 {
417  if (_pInterface == pInterface)
418  return true;
419  try
420  {
421  // only the query to XInterface must return the same pointer if they belong to same objects
423  Reference< XInterface > x2( pInterface, UNO_QUERY );
424  return (x1._pInterface == x2._pInterface);
425  }
426  catch (RuntimeException &)
427  {
428  return false;
429  }
430 }
431 
432 
434  const BaseReference & rRef ) const
435 {
436  if (_pInterface == rRef._pInterface)
437  return false;
438  try
439  {
440  // only the query to XInterface must return the same pointer:
443  return (x1._pInterface < x2._pInterface);
444  }
445  catch (RuntimeException &)
446  {
447  return false;
448  }
449 }
450 
451 
452 inline bool BaseReference::operator != ( XInterface * pInterface ) const
453 {
454  return (! operator == ( pInterface ));
455 }
456 
457 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
458 {
459  return operator == ( rRef._pInterface );
460 }
461 
462 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
463 {
464  return (! operator == ( rRef._pInterface ));
465 }
466 
467 #if defined LIBO_INTERNAL_ONLY
468 
474 template<typename charT, typename traits> std::basic_ostream<charT, traits> &
476  std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
477 { return stream << ref.get(); }
478 #endif
479 
480 }
481 }
482 }
483 }
484 
485 #if defined LIBO_INTERNAL_ONLY
486 namespace std
487 {
488 
494 template<typename T>
495 struct hash<::css::uno::Reference<T>>
496 {
497  std::size_t operator()(::css::uno::Reference<T> const & s) const
498  { return size_t(s.get()); }
499 };
500 
501 }
502 
503 #endif
504 
505 #endif
506 
507 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:334
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition: Reference.h:55
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:268
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:373
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iset_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:349
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:162
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:77
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor: Releases interface if set.
Definition: Reference.hxx:111
bool operator!=(XInterface *pInterface) const
Inequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition: Reference.hxx:452
This enum value can be used for implicit interface query.
Definition: Reference.h:157
This enum value can be used for implicit interface query.
Definition: Reference.h:166
bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition: Reference.hxx:433
bool operator==(XInterface *pInterface) const
Equality operator: compares two interfaces Checks if both references are null or refer to the same ob...
Definition: Reference.hxx:415
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &o, Any const &any)
Support for Any in std::ostream (and thus in CPPUNIT_ASSERT or SAL_INFO macros, for example)...
Definition: Any.hxx:706
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition: Reference.hxx:344
C++ class representing an IDL any.
Definition: Any.h:57
#define CPPU_DLLPUBLIC
Definition: cppudllapi.h:13
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:153
Definition: Reference.h:175
Definition: types.h:359
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:43
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:403
__sal_NoAcquire
Definition: types.h:352
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iquery_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
void clear()
Clears reference, i.e.
Definition: Reference.hxx:225
type class of interface
Definition: typeclass.h:82
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:170
definition of a no acquire enum for ctors
Definition: types.h:356
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:162
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:66
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:54
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:173
Reference()
Default Constructor: Sets null reference.
Definition: Reference.hxx:118
C++ class representing an IDL meta type.
Definition: Type.h:58
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:94
XInterface * _pInterface
the interface pointer
Definition: Reference.h:71