LibreOffice
LibreOffice 5.1 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 
27 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <com/sun/star/uno/XInterface.hpp>
29 #include <com/sun/star/uno/Any.hxx>
30 #include <cppu/cppudllapi.h>
31 
32 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
35 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
38 
39 namespace com
40 {
41 namespace sun
42 {
43 namespace star
44 {
45 namespace uno
46 {
47 
48 
49 inline XInterface * BaseReference::iquery(
50  XInterface * pInterface, const Type & rType )
51 {
52  if (pInterface)
53  {
54  Any aRet( pInterface->queryInterface( rType ) );
55  if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
56  {
57  XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
58  aRet.pReserved = NULL;
59  return pRet;
60  }
61  }
62  return NULL;
63 }
64 
65 template< class interface_type >
66 inline XInterface * Reference< interface_type >::iquery(
67  XInterface * pInterface )
68 {
69  return BaseReference::iquery(pInterface, interface_type::static_type());
70 }
71 
72 inline XInterface * BaseReference::iquery_throw(
73  XInterface * pInterface, const Type & rType )
74 {
75  XInterface * pQueried = iquery( pInterface, rType );
76  if (pQueried)
77  return pQueried;
78  throw RuntimeException(
80  Reference< XInterface >( pInterface ) );
81 }
82 
83 template< class interface_type >
85  XInterface * pInterface )
86 {
88  pInterface, interface_type::static_type());
89 }
90 
91 template< class interface_type >
92 inline interface_type * Reference< interface_type >::iset_throw(
93  interface_type * pInterface )
94 {
95  if (pInterface)
96  {
97  castToXInterface(pInterface)->acquire();
98  return pInterface;
99  }
100  throw RuntimeException(
101  ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
102  NULL );
103 }
104 
105 
106 template< class interface_type >
108 {
109  if (_pInterface)
110  _pInterface->release();
111 }
112 
113 template< class interface_type >
115 {
116  _pInterface = 0;
117 }
118 
119 template< class interface_type >
121 {
122  _pInterface = rRef._pInterface;
123  if (_pInterface)
124  _pInterface->acquire();
125 }
126 
127 template< class interface_type > template< class derived_type >
129  const Reference< derived_type > & rRef,
130  typename detail::UpCast< interface_type, derived_type >::t )
131 {
132  interface_type * p = rRef.get();
133  _pInterface = p;
134  if (_pInterface)
135  _pInterface->acquire();
136 }
137 
138 template< class interface_type >
139 inline Reference< interface_type >::Reference( interface_type * pInterface )
140 {
141  _pInterface = castToXInterface(pInterface);
142  if (_pInterface)
143  _pInterface->acquire();
144 }
145 
146 template< class interface_type >
147 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
148 {
149  _pInterface = castToXInterface(pInterface);
150 }
151 
152 template< class interface_type >
154 {
155  _pInterface = castToXInterface(pInterface);
156 }
157 
158 template< class interface_type >
160 {
161  _pInterface = iquery( rRef.get() );
162 }
163 
164 template< class interface_type >
166 {
167  _pInterface = iquery( pInterface );
168 }
169 
170 template< class interface_type >
172 {
173  _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
174  ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
175 }
176 
177 template< class interface_type >
179 {
180  _pInterface = iquery_throw( rRef.get() );
181 }
182 
183 template< class interface_type >
185 {
186  _pInterface = iquery_throw( pInterface );
187 }
188 
189 template< class interface_type >
191 {
192  _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
193  ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
194 }
195 
196 template< class interface_type >
198 {
199  _pInterface = castToXInterface( iset_throw( rRef.get() ) );
200 }
201 
202 template< class interface_type >
203 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
204 {
205  _pInterface = castToXInterface( iset_throw( pInterface ) );
206 }
207 
208 
209 template< class interface_type >
211 {
212  if (_pInterface)
213  {
214  XInterface * const pOld = _pInterface;
215  _pInterface = 0;
216  pOld->release();
217  }
218 }
219 
220 template< class interface_type >
222  interface_type * pInterface )
223 {
224  if (pInterface)
225  castToXInterface(pInterface)->acquire();
226  XInterface * const pOld = _pInterface;
227  _pInterface = castToXInterface(pInterface);
228  if (pOld)
229  pOld->release();
230  return (0 != pInterface);
231 }
232 
233 template< class interface_type >
235  interface_type * pInterface, __sal_NoAcquire )
236 {
237  XInterface * const pOld = _pInterface;
238  _pInterface = castToXInterface(pInterface);
239  if (pOld)
240  pOld->release();
241  return (0 != pInterface);
242 }
243 
244 template< class interface_type >
246  interface_type * pInterface, UnoReference_NoAcquire )
247 {
248  return set( pInterface, SAL_NO_ACQUIRE );
249 }
250 
251 
252 template< class interface_type >
254  const Reference< interface_type > & rRef )
255 {
256  return set( castFromXInterface( rRef._pInterface ) );
257 }
258 
259 template< class interface_type >
261  XInterface * pInterface, UnoReference_Query )
262 {
263  return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
264 }
265 
266 template< class interface_type >
268  const BaseReference & rRef, UnoReference_Query )
269 {
270  return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
271 }
272 
273 
274 template< class interface_type >
276  Any const & rAny, UnoReference_Query )
277 {
278  return set(
279  castFromXInterface(
280  iquery(
281  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
282  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
283  SAL_NO_ACQUIRE );
284 }
285 
286 
287 template< class interface_type >
289  XInterface * pInterface, UnoReference_QueryThrow )
290 {
291  set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
292 }
293 
294 template< class interface_type >
296  const BaseReference & rRef, UnoReference_QueryThrow )
297 {
298  set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
299 }
300 
301 
302 template< class interface_type >
304  Any const & rAny, UnoReference_QueryThrow )
305 {
306  set( castFromXInterface(
307  iquery_throw(
308  rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
309  ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
310  SAL_NO_ACQUIRE );
311 }
312 
313 template< class interface_type >
315  interface_type * pInterface, UnoReference_SetThrow )
316 {
317  set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
318 }
319 
320 template< class interface_type >
323 {
324  set( rRef.get(), UNO_SET_THROW );
325 }
326 
327 
328 template< class interface_type >
330  interface_type * pInterface )
331 {
332  set( pInterface );
333  return *this;
334 }
335 
336 template< class interface_type >
338  const Reference< interface_type > & rRef )
339 {
340  set( castFromXInterface( rRef._pInterface ) );
341  return *this;
342 }
343 
344 
345 template< class interface_type >
347  const BaseReference & rRef )
348 {
350  castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
351 }
352 
353 template< class interface_type >
355  XInterface * pInterface )
356 {
358  castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
359 }
360 
361 
362 
363 
364 inline bool BaseReference::operator == ( XInterface * pInterface ) const
365 {
366  if (_pInterface == pInterface)
367  return true;
368  try
369  {
370  // only the query to XInterface must return the same pointer if they belong to same objects
372  Reference< XInterface > x2( pInterface, UNO_QUERY );
373  return (x1._pInterface == x2._pInterface);
374  }
375  catch (RuntimeException &)
376  {
377  return false;
378  }
379 }
380 
381 
383  const BaseReference & rRef ) const
384 {
385  if (_pInterface == rRef._pInterface)
386  return false;
387  try
388  {
389  // only the query to XInterface must return the same pointer:
392  return (x1._pInterface < x2._pInterface);
393  }
394  catch (RuntimeException &)
395  {
396  return false;
397  }
398 }
399 
400 
401 inline bool BaseReference::operator != ( XInterface * pInterface ) const
402 {
403  return (! operator == ( pInterface ));
404 }
405 
406 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
407 {
408  return operator == ( rRef._pInterface );
409 }
410 
411 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
412 {
413  return (! operator == ( rRef._pInterface ));
414 }
415 
416 }
417 }
418 }
419 }
420 
421 #endif
422 
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This enum value can be used for implicit interface query.
Definition: Reference.h:140
~Reference()
Destructor: Releases interface if set.
Definition: Reference.hxx:107
#define CPPU_DLLPUBLIC
Definition: cppudllapi.h:10
type class of interface
Definition: typeclass.h:79
C++ class representing an IDL any.
Definition: Any.h:49
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:411
Definition: types.h:389
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:145
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:136
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition: Reference.hxx:329
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:346
__sal_NoAcquire
Definition: types.h:382
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:364
bool operator!=(XInterface *pInterface) const
Unequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition: Reference.hxx:401
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:156
Definition: Reference.h:158
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:106
definition of a no acquire enum for ctors
Definition: types.h:386
void clear()
Clears reference, i.e.
Definition: Reference.hxx:210
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:368
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:253
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:49
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:382
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:72
XInterface * _pInterface
the interface pointer
Definition: Reference.h:63