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