LibreOffice
LibreOffice 5.4 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 
107 template< class interface_type >
109 {
110  if (_pInterface)
111  _pInterface->release();
112 }
113 
114 template< class interface_type >
116 {
117  _pInterface = NULL;
118 }
119 
120 template< class interface_type >
122 {
123  _pInterface = rRef._pInterface;
124  if (_pInterface)
125  _pInterface->acquire();
126 }
127 
128 #if defined LIBO_INTERNAL_ONLY
129 template< class interface_type >
131 {
132  _pInterface = rRef._pInterface;
133  rRef._pInterface = nullptr;
134 }
135 #endif
136 
137 template< class interface_type > template< class derived_type >
139  const Reference< derived_type > & rRef,
140  typename detail::UpCast< interface_type, derived_type >::t )
141 {
142  interface_type * p = rRef.get();
143  _pInterface = p;
144  if (_pInterface)
145  _pInterface->acquire();
146 }
147 
148 template< class interface_type >
149 inline Reference< interface_type >::Reference( interface_type * pInterface )
150 {
151  _pInterface = castToXInterface(pInterface);
152  if (_pInterface)
153  _pInterface->acquire();
154 }
155 
156 template< class interface_type >
157 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
158 {
159  _pInterface = castToXInterface(pInterface);
160 }
161 
162 template< class interface_type >
164 {
165  _pInterface = castToXInterface(pInterface);
166 }
167 
168 template< class interface_type >
170 {
171  _pInterface = iquery( rRef.get() );
172 }
173 
174 template< class interface_type >
176 {
177  _pInterface = iquery( pInterface );
178 }
179 
180 template< class interface_type >
182 {
183  _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
184  ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
185 }
186 
187 template< class interface_type >
189 {
190  _pInterface = iquery_throw( rRef.get() );
191 }
192 
193 template< class interface_type >
195 {
196  _pInterface = iquery_throw( pInterface );
197 }
198 
199 template< class interface_type >
201 {
202  _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
203  ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
204 }
205 
206 template< class interface_type >
208 {
209  _pInterface = castToXInterface( iset_throw( rRef.get() ) );
210 }
211 
212 template< class interface_type >
213 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
214 {
215  _pInterface = castToXInterface( iset_throw( pInterface ) );
216 }
217 
218 
219 template< class interface_type >
221 {
222  if (_pInterface)
223  {
224  XInterface * const pOld = _pInterface;
225  _pInterface = NULL;
226  pOld->release();
227  }
228 }
229 
230 template< class interface_type >
232  interface_type * pInterface )
233 {
234  if (pInterface)
235  castToXInterface(pInterface)->acquire();
236  XInterface * const pOld = _pInterface;
237  _pInterface = castToXInterface(pInterface);
238  if (pOld)
239  pOld->release();
240  return (NULL != pInterface);
241 }
242 
243 template< class interface_type >
245  interface_type * pInterface, __sal_NoAcquire )
246 {
247  XInterface * const pOld = _pInterface;
248  _pInterface = castToXInterface(pInterface);
249  if (pOld)
250  pOld->release();
251  return (NULL != pInterface);
252 }
253 
254 template< class interface_type >
256  interface_type * pInterface, UnoReference_NoAcquire )
257 {
258  return set( pInterface, SAL_NO_ACQUIRE );
259 }
260 
261 
262 template< class interface_type >
264  const Reference< interface_type > & rRef )
265 {
266  return set( castFromXInterface( rRef._pInterface ) );
267 }
268 
269 template< class interface_type >
271  XInterface * pInterface, UnoReference_Query )
272 {
273  return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
274 }
275 
276 template< class interface_type >
278  const BaseReference & rRef, UnoReference_Query )
279 {
280  return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
281 }
282 
283 
284 template< class interface_type >
286  Any const & rAny, UnoReference_Query )
287 {
288  return set(
289  castFromXInterface(
290  iquery(
291  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
292  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
293  SAL_NO_ACQUIRE );
294 }
295 
296 
297 template< class interface_type >
299  XInterface * pInterface, UnoReference_QueryThrow )
300 {
301  set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
302 }
303 
304 template< class interface_type >
306  const BaseReference & rRef, UnoReference_QueryThrow )
307 {
308  set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
309 }
310 
311 
312 template< class interface_type >
314  Any const & rAny, UnoReference_QueryThrow )
315 {
316  set( castFromXInterface(
317  iquery_throw(
318  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
319  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
320  SAL_NO_ACQUIRE );
321 }
322 
323 template< class interface_type >
325  interface_type * pInterface, UnoReference_SetThrow )
326 {
327  set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
328 }
329 
330 template< class interface_type >
333 {
334  set( rRef.get(), UNO_SET_THROW );
335 }
336 
337 
338 template< class interface_type >
340  interface_type * pInterface )
341 {
342  set( pInterface );
343  return *this;
344 }
345 
346 template< class interface_type >
348  const Reference< interface_type > & rRef )
349 {
350  set( castFromXInterface( rRef._pInterface ) );
351  return *this;
352 }
353 
354 #if defined LIBO_INTERNAL_ONLY
355 template< class interface_type >
358 {
359  if (_pInterface)
360  _pInterface->release();
361  _pInterface = rRef._pInterface;
362  rRef._pInterface = nullptr;
363  return *this;
364 }
365 #endif
366 
367 template< class interface_type >
369  const BaseReference & rRef )
370 {
372  castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
373 }
374 
375 template< class interface_type >
377  XInterface * pInterface )
378 {
380  castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
381 }
382 
383 
384 inline bool BaseReference::operator == ( XInterface * pInterface ) const
385 {
386  if (_pInterface == pInterface)
387  return true;
388  try
389  {
390  // only the query to XInterface must return the same pointer if they belong to same objects
392  Reference< XInterface > x2( pInterface, UNO_QUERY );
393  return (x1._pInterface == x2._pInterface);
394  }
395  catch (RuntimeException &)
396  {
397  return false;
398  }
399 }
400 
401 
403  const BaseReference & rRef ) const
404 {
405  if (_pInterface == rRef._pInterface)
406  return false;
407  try
408  {
409  // only the query to XInterface must return the same pointer:
412  return (x1._pInterface < x2._pInterface);
413  }
414  catch (RuntimeException &)
415  {
416  return false;
417  }
418 }
419 
420 
421 inline bool BaseReference::operator != ( XInterface * pInterface ) const
422 {
423  return (! operator == ( pInterface ));
424 }
425 
426 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
427 {
428  return operator == ( rRef._pInterface );
429 }
430 
431 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
432 {
433  return (! operator == ( rRef._pInterface ));
434 }
435 
436 #if defined LIBO_INTERNAL_ONLY
437 
443 template<typename charT, typename traits> std::basic_ostream<charT, traits> &
445  std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
446 { return stream << ref.get(); }
447 #endif
448 
449 }
450 }
451 }
452 }
453 
454 #endif
455 
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define CPPU_DLLPUBLIC
Definition: cppudllapi.h:10
bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition: Reference.hxx:402
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:352
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:145
type class of interface
Definition: typeclass.h:79
Reference()
Default Constructor: Sets null reference.
Definition: Reference.hxx:115
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:428
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:86
Definition: types.h:373
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:154
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:50
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:165
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:73
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:39
XInterface * _pInterface
the interface pointer
Definition: Reference.h:63
__sal_NoAcquire
Definition: types.h:366
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:368
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition: Reference.h:47
definition of a no acquire enum for ctors
Definition: types.h:370
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:58
This enum value can be used for implicit interface query.
Definition: Reference.h:149
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:120
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iset_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
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
void clear()
Clears reference, i.e.
Definition: Reference.hxx:220
~Reference()
Destructor: Releases interface if set.
Definition: Reference.hxx:108
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:384
Definition: Reference.h:167
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:263
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:154
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition: Reference.hxx:339
C++ class representing an IDL meta type.
Definition: Type.h:54
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iquery_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
C++ class representing an IDL any.
Definition: Any.h:52
bool operator!=(XInterface *pInterface) const
Unequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition: Reference.hxx:421