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