LibreOffice
LibreOffice 6.0 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 #ifndef INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
20 #define INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
21 
22 #include <sal/config.h>
23 
24 #include <cstddef>
25 #include <ostream>
26 
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <com/sun/star/uno/XInterface.hpp>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <cppu/cppudllapi.h>
32 
33 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
36 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
39 
40 namespace com
41 {
42 namespace sun
43 {
44 namespace star
45 {
46 namespace uno
47 {
48 
49 
50 inline XInterface * BaseReference::iquery(
51  XInterface * pInterface, const Type & rType )
52 {
53  if (pInterface)
54  {
55  Any aRet( pInterface->queryInterface( rType ) );
56  if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
57  {
58  XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
59  aRet.pReserved = NULL;
60  return pRet;
61  }
62  }
63  return NULL;
64 }
65 
66 template< class interface_type >
67 inline XInterface * Reference< interface_type >::iquery(
68  XInterface * pInterface )
69 {
70  return BaseReference::iquery(pInterface, interface_type::static_type());
71 }
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(
81  Reference< XInterface >( pInterface ) );
82 }
83 
84 template< class interface_type >
86  XInterface * pInterface )
87 {
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  castToXInterface(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 
106 template< class interface_type >
108 {
109  if (_pInterface)
110  _pInterface->release();
111 }
112 
113 template< class interface_type >
115 {
116  _pInterface = NULL;
117 }
118 
119 template< class interface_type >
121 {
122  _pInterface = rRef._pInterface;
123  if (_pInterface)
124  _pInterface->acquire();
125 }
126 
127 #if defined LIBO_INTERNAL_ONLY
128 template< class interface_type >
130 {
131  _pInterface = rRef._pInterface;
132  rRef._pInterface = nullptr;
133 }
134 #endif
135 
136 template< class interface_type > template< class derived_type >
138  const Reference< derived_type > & rRef,
139  typename detail::UpCast< interface_type, derived_type >::t )
140 {
141  interface_type * p = rRef.get();
142  _pInterface = p;
143  if (_pInterface)
144  _pInterface->acquire();
145 }
146 
147 template< class interface_type >
148 inline Reference< interface_type >::Reference( interface_type * pInterface )
149 {
150  _pInterface = castToXInterface(pInterface);
151  if (_pInterface)
152  _pInterface->acquire();
153 }
154 
155 template< class interface_type >
156 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
157 {
158  _pInterface = castToXInterface(pInterface);
159 }
160 
161 template< class interface_type >
163 {
164  _pInterface = castToXInterface(pInterface);
165 }
166 
167 template< class interface_type >
169 {
170  _pInterface = iquery( rRef.get() );
171 }
172 
173 template< class interface_type >
175 {
176  _pInterface = iquery( pInterface );
177 }
178 
179 template< class interface_type >
181 {
182  _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
183  ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
184 }
185 
186 template< class interface_type >
188 {
189  _pInterface = iquery_throw( rRef.get() );
190 }
191 
192 template< class interface_type >
194 {
195  _pInterface = iquery_throw( pInterface );
196 }
197 
198 template< class interface_type >
200 {
201  _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
202  ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
203 }
204 
205 template< class interface_type >
207 {
208  _pInterface = castToXInterface( iset_throw( rRef.get() ) );
209 }
210 
211 template< class interface_type >
212 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
213 {
214  _pInterface = castToXInterface( iset_throw( pInterface ) );
215 }
216 
217 
218 template< class interface_type >
220 {
221  if (_pInterface)
222  {
223  XInterface * const pOld = _pInterface;
224  _pInterface = NULL;
225  pOld->release();
226  }
227 }
228 
229 template< class interface_type >
231  interface_type * pInterface )
232 {
233  if (pInterface)
234  castToXInterface(pInterface)->acquire();
235  XInterface * const pOld = _pInterface;
236  _pInterface = castToXInterface(pInterface);
237  if (pOld)
238  pOld->release();
239  return (NULL != pInterface);
240 }
241 
242 template< class interface_type >
244  interface_type * pInterface, __sal_NoAcquire )
245 {
246  XInterface * const pOld = _pInterface;
247  _pInterface = castToXInterface(pInterface);
248  if (pOld)
249  pOld->release();
250  return (NULL != pInterface);
251 }
252 
253 template< class interface_type >
255  interface_type * pInterface, UnoReference_NoAcquire )
256 {
257  return set( pInterface, SAL_NO_ACQUIRE );
258 }
259 
260 
261 template< class interface_type >
263  const Reference< interface_type > & rRef )
264 {
265  return set( castFromXInterface( rRef._pInterface ) );
266 }
267 
268 template< class interface_type >
270  XInterface * pInterface, UnoReference_Query )
271 {
272  return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
273 }
274 
275 template< class interface_type >
277  const BaseReference & rRef, UnoReference_Query )
278 {
279  return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
280 }
281 
282 
283 template< class interface_type >
285  Any const & rAny, UnoReference_Query )
286 {
287  return set(
288  castFromXInterface(
289  iquery(
290  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
291  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
292  SAL_NO_ACQUIRE );
293 }
294 
295 
296 template< class interface_type >
298  XInterface * pInterface, UnoReference_QueryThrow )
299 {
300  set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
301 }
302 
303 template< class interface_type >
305  const BaseReference & rRef, UnoReference_QueryThrow )
306 {
307  set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
308 }
309 
310 
311 template< class interface_type >
313  Any const & rAny, UnoReference_QueryThrow )
314 {
315  set( castFromXInterface(
316  iquery_throw(
317  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
318  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
319  SAL_NO_ACQUIRE );
320 }
321 
322 template< class interface_type >
324  interface_type * pInterface, UnoReference_SetThrow )
325 {
326  set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
327 }
328 
329 template< class interface_type >
332 {
333  set( rRef.get(), UNO_SET_THROW );
334 }
335 
336 
337 template< class interface_type >
339  interface_type * pInterface )
340 {
341  set( pInterface );
342  return *this;
343 }
344 
345 template< class interface_type >
347  const Reference< interface_type > & rRef )
348 {
349  set( castFromXInterface( rRef._pInterface ) );
350  return *this;
351 }
352 
353 #if defined LIBO_INTERNAL_ONLY
354 template< class interface_type >
357 {
358  if (_pInterface)
359  _pInterface->release();
360  _pInterface = rRef._pInterface;
361  rRef._pInterface = nullptr;
362  return *this;
363 }
364 #endif
365 
366 template< class interface_type >
368  const BaseReference & rRef )
369 {
371  castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
372 }
373 
374 template< class interface_type >
376  XInterface * pInterface )
377 {
379  castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
380 }
381 
382 
383 inline bool BaseReference::operator == ( XInterface * pInterface ) const
384 {
385  if (_pInterface == pInterface)
386  return true;
387  try
388  {
389  // only the query to XInterface must return the same pointer if they belong to same objects
391  Reference< XInterface > x2( pInterface, UNO_QUERY );
392  return (x1._pInterface == x2._pInterface);
393  }
394  catch (RuntimeException &)
395  {
396  return false;
397  }
398 }
399 
400 
402  const BaseReference & rRef ) const
403 {
404  if (_pInterface == rRef._pInterface)
405  return false;
406  try
407  {
408  // only the query to XInterface must return the same pointer:
411  return (x1._pInterface < x2._pInterface);
412  }
413  catch (RuntimeException &)
414  {
415  return false;
416  }
417 }
418 
419 
420 inline bool BaseReference::operator != ( XInterface * pInterface ) const
421 {
422  return (! operator == ( pInterface ));
423 }
424 
425 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
426 {
427  return operator == ( rRef._pInterface );
428 }
429 
430 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
431 {
432  return (! operator == ( rRef._pInterface ));
433 }
434 
435 #if defined LIBO_INTERNAL_ONLY
436 
442 template<typename charT, typename traits> std::basic_ostream<charT, traits> &
444  std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
445 { return stream << ref.get(); }
446 #endif
447 
448 }
449 }
450 }
451 }
452 
453 #endif
454 
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This enum value can be used for implicit interface query.
Definition: Reference.h:149
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:664
#define CPPU_DLLPUBLIC
Definition: cppudllapi.h:10
type class of interface
Definition: typeclass.h:79
C++ class representing an IDL any.
Definition: Any.h:52
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:428
Definition: types.h:383
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:361
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:154
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor: Releases interface if set.
Definition: Reference.hxx:107
Reference()
Default Constructor: Sets null reference.
Definition: Reference.hxx:114
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iquery_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:86
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:154
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:145
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition: Reference.hxx:338
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:367
__sal_NoAcquire
Definition: types.h:376
C++ class representing an IDL meta type.
Definition: Type.h:54
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:383
bool operator!=(XInterface *pInterface) const
Unequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition: Reference.hxx:420
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:165
Definition: Reference.h:167
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:120
definition of a no acquire enum for ctors
Definition: types.h:380
void clear()
Clears reference, i.e.
Definition: Reference.hxx:219
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:39
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:352
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:262
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:58
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iset_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:50
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition: Reference.h:47
bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition: Reference.hxx:401
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:73
XInterface * _pInterface
the interface pointer
Definition: Reference.h:63