AlterOffice
AlterOffice 3.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #ifndef INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
4 #define INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
5 
6 #include "sal/config.h"
7 
8 #include <cstddef>
9 #include <ostream>
10 
12 #include "com/sun/star/uno/RuntimeException.hpp"
13 #include "com/sun/star/uno/XInterface.hpp"
14 #include "com/sun/star/uno/Any.hxx"
15 #include "cppu/cppudllapi.h"
16 
17 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
20 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
23 
24 namespace com
25 {
26 namespace sun
27 {
28 namespace star
29 {
30 namespace uno
31 {
32 
33 
34 inline XInterface * BaseReference::iquery(
35  XInterface * pInterface, const Type & rType )
36 {
37  if (pInterface)
38  {
39  Any aRet( pInterface->queryInterface( rType ) );
40  if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
41  {
42  XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
43  aRet.pReserved = NULL;
44  return pRet;
45  }
46  }
47  return NULL;
48 }
49 
50 template< class interface_type >
51 inline XInterface * Reference< interface_type >::iquery(
52  XInterface * pInterface )
53 {
54  return BaseReference::iquery(pInterface, interface_type::static_type());
55 }
56 
57 inline XInterface * BaseReference::iquery_throw(
58  XInterface * pInterface, const Type & rType )
59 {
60  XInterface * pQueried = iquery( pInterface, rType );
61  if (pQueried)
62  return pQueried;
63  throw RuntimeException(
65  Reference< XInterface >( pInterface ) );
66 }
67 
68 template< class interface_type >
70  XInterface * pInterface )
71 {
73  pInterface, interface_type::static_type());
74 }
75 
76 template< class interface_type >
77 inline interface_type * Reference< interface_type >::iset_throw(
78  interface_type * pInterface )
79 {
80  if (pInterface)
81  {
82  castToXInterface(pInterface)->acquire();
83  return pInterface;
84  }
85  throw RuntimeException(
86  ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
87  NULL );
88 }
89 
90 template< class interface_type >
92 {
93  if (_pInterface)
94  _pInterface->release();
95 }
96 
97 template< class interface_type >
99 {
100  _pInterface = NULL;
101 }
102 
103 template< class interface_type >
105 {
106  _pInterface = rRef._pInterface;
107  if (_pInterface)
108  _pInterface->acquire();
109 }
110 
111 #if defined LIBO_INTERNAL_ONLY
112 template< class interface_type >
114 {
115  _pInterface = rRef._pInterface;
116  rRef._pInterface = nullptr;
117 }
118 
119 template< class interface_type > template< class derived_type >
121  const Reference< derived_type > & rRef,
122  std::enable_if_t<
123  std::is_base_of_v<interface_type, derived_type>
124  && !std::is_same_v<interface_type, XInterface>, void *>)
125 {
126  interface_type * p = rRef.get();
127  _pInterface = castToXInterface(p);
128  if (_pInterface)
129  _pInterface->acquire();
130 }
131 #endif
132 
133 template< class interface_type >
134 inline Reference< interface_type >::Reference( interface_type * pInterface )
135 {
136  _pInterface = castToXInterface(pInterface);
137  if (_pInterface)
138  _pInterface->acquire();
139 }
140 
141 template< class interface_type >
142 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
143 {
144  _pInterface = castToXInterface(pInterface);
145 }
146 
147 template< class interface_type >
149 {
150  _pInterface = castToXInterface(pInterface);
151 }
152 
153 template< class interface_type >
155 {
156  _pInterface = iquery( rRef.get() );
157 }
158 
159 template< class interface_type >
161 {
162  _pInterface = iquery( pInterface );
163 }
164 
165 template< class interface_type >
167 {
168  _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
169  ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
170 }
171 
172 template< class interface_type >
174 {
175  _pInterface = iquery_throw( rRef.get() );
176 }
177 
178 template< class interface_type >
180 {
181  _pInterface = iquery_throw( pInterface );
182 }
183 
184 template< class interface_type >
186 {
187  _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
188  ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
189 }
190 
191 template< class interface_type >
193 {
194  _pInterface = castToXInterface( iset_throw( rRef.get() ) );
195 }
196 
197 template< class interface_type >
198 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
199 {
200  _pInterface = castToXInterface( iset_throw( pInterface ) );
201 }
202 
203 
204 template< class interface_type >
206 {
207  if (_pInterface)
208  {
209  XInterface * const pOld = _pInterface;
210  _pInterface = NULL;
211  pOld->release();
212  }
213 }
214 
215 template< class interface_type >
217  interface_type * pInterface )
218 {
219  if (pInterface)
220  castToXInterface(pInterface)->acquire();
221  XInterface * const pOld = _pInterface;
222  _pInterface = castToXInterface(pInterface);
223  if (pOld)
224  pOld->release();
225  return (NULL != pInterface);
226 }
227 
228 template< class interface_type >
230  interface_type * pInterface, __sal_NoAcquire )
231 {
232  XInterface * const pOld = _pInterface;
233  _pInterface = castToXInterface(pInterface);
234  if (pOld)
235  pOld->release();
236  return (NULL != pInterface);
237 }
238 
239 template< class interface_type >
241  interface_type * pInterface, UnoReference_NoAcquire )
242 {
243  return set( pInterface, SAL_NO_ACQUIRE );
244 }
245 
246 
247 template< class interface_type >
249  const Reference< interface_type > & rRef )
250 {
251  return set( castFromXInterface( rRef._pInterface ) );
252 }
253 
254 template< class interface_type >
256  XInterface * pInterface, UnoReference_Query )
257 {
258  return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
259 }
260 
261 template< class interface_type >
263  const BaseReference & rRef, UnoReference_Query )
264 {
265  return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
266 }
267 
268 
269 template< class interface_type >
271  Any const & rAny, UnoReference_Query )
272 {
273  return set(
274  castFromXInterface(
275  iquery(
276  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
277  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
278  SAL_NO_ACQUIRE );
279 }
280 
281 
282 template< class interface_type >
284  XInterface * pInterface, UnoReference_QueryThrow )
285 {
286  set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
287 }
288 
289 template< class interface_type >
291  const BaseReference & rRef, UnoReference_QueryThrow )
292 {
293  set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
294 }
295 
296 
297 template< class interface_type >
299  Any const & rAny, UnoReference_QueryThrow )
300 {
301  set( castFromXInterface(
302  iquery_throw(
303  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
304  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
305  SAL_NO_ACQUIRE );
306 }
307 
308 template< class interface_type >
310  interface_type * pInterface, UnoReference_SetThrow )
311 {
312  set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
313 }
314 
315 template< class interface_type >
318 {
319  set( rRef.get(), UNO_SET_THROW );
320 }
321 
322 
323 template< class interface_type >
325  interface_type * pInterface )
326 {
327  set( pInterface );
328  return *this;
329 }
330 
331 template< class interface_type >
333  const Reference< interface_type > & rRef )
334 {
335  set( castFromXInterface( rRef._pInterface ) );
336  return *this;
337 }
338 
339 #if defined LIBO_INTERNAL_ONLY
340 template< class interface_type >
342  Reference< interface_type > && rRef ) noexcept
343 {
344  if (_pInterface)
345  _pInterface->release();
346  _pInterface = rRef._pInterface;
347  rRef._pInterface = nullptr;
348  return *this;
349 }
350 #endif
351 
352 template< class interface_type >
354  const BaseReference & rRef )
355 {
357  castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
358 }
359 
360 template< class interface_type >
362  XInterface * pInterface )
363 {
365  castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
366 }
367 
368 
369 inline bool BaseReference::operator == ( XInterface * pInterface ) const
370 {
371  if (_pInterface == pInterface)
372  return true;
373  try
374  {
375  // only the query to XInterface must return the same pointer if they belong to same objects
377  Reference< XInterface > x2( pInterface, UNO_QUERY );
378  return (x1._pInterface == x2._pInterface);
379  }
380  catch (RuntimeException &)
381  {
382  return false;
383  }
384 }
385 
386 
388  const BaseReference & rRef ) const
389 {
390  if (_pInterface == rRef._pInterface)
391  return false;
392  try
393  {
394  // only the query to XInterface must return the same pointer:
397  return (x1._pInterface < x2._pInterface);
398  }
399  catch (RuntimeException &)
400  {
401  return false;
402  }
403 }
404 
405 
406 inline bool BaseReference::operator != ( XInterface * pInterface ) const
407 {
408  return (! operator == ( pInterface ));
409 }
410 
411 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
412 {
413  return operator == ( rRef._pInterface );
414 }
415 
416 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
417 {
418  return (! operator == ( rRef._pInterface ));
419 }
420 
421 #if defined LIBO_INTERNAL_ONLY
422 
428 template<typename charT, typename traits> std::basic_ostream<charT, traits> &
430  std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
431 { return stream << ref.get(); }
432 #endif
433 
434 }
435 }
436 }
437 }
438 
439 #if defined LIBO_INTERNAL_ONLY
440 namespace std
441 {
442 
448 template<typename T>
449 struct hash<::css::uno::Reference<T>>
450 {
451  std::size_t operator()(::css::uno::Reference<T> const & s) const
452  { return size_t(s.get()); }
453 };
454 
455 }
456 
457 #endif
458 
459 #endif
460 
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iset_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:182
__sal_NoAcquire
Definition: types.h:332
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iquery_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition: Reference.hxx:324
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:353
C++ class representing an IDL any.
Definition: Any.h:36
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:142
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:142
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:329
This enum value can be used for implicit interface query.
Definition: Reference.h:137
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:133
CPPU_DLLPUBLIC void SAL_THROW_EXTERN_C()
This method is called to wait for a reply of a previously sent request.
type class of interface
Definition: typeclass.h:62
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:74
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor: Releases interface if set.
Definition: Reference.hxx:91
Reference()
Default Constructor: Sets null reference.
Definition: Reference.hxx:98
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:684
C++ class representing an IDL meta type.
Definition: Type.h:38
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:383
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:369
bool operator!=(XInterface *pInterface) const
Inequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition: Reference.hxx:406
definition of a no acquire enum for ctors
Definition: types.h:336
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:248
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:23
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:153
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition: Reference.h:35
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:46
void clear()
Clears reference, i.e.
Definition: Reference.hxx:205
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:34
Definition: Reference.h:155
bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition: Reference.hxx:387
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:57
#define CPPU_DLLPUBLIC
Definition: cppudllapi.h:11
XInterface * _pInterface
the interface pointer
Definition: Reference.h:51