LibreOffice: com/sun/star/uno/Reference.hxx Source File

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
26 #include <com/sun/star/uno/Reference.h>
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(
33 typelib_TypeDescriptionReference * pType )
34 SAL_THROW_EXTERN_C();
35 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
36 typelib_TypeDescriptionReference * pType )
37 SAL_THROW_EXTERN_C();
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(
79 ::rtl::OUString( cppu_unsatisfied_iquery_msg( rType.getTypeLibType() ), SAL_NO_ACQUIRE ),
80 Reference< XInterface >( pInterface ) );
81 }
82
83 template< class interface_type >
84 inline XInterface * Reference< interface_type >::iquery_throw(
85 XInterface * pInterface )
86 {
87 return BaseReference::iquery_throw(
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 >
107 inline Reference< interface_type >::~Reference()
108 {
109 if (_pInterface)
110 _pInterface->release();
111 }
112
113 template< class interface_type >
114 inline Reference< interface_type >::Reference()
115 {
116 _pInterface = 0;
117 }
118
119 template< class interface_type >
120 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef )
121 {
122 _pInterface = rRef._pInterface;
123 if (_pInterface)
124 _pInterface->acquire();
125 }
126
127 template< class interface_type > template< class derived_type >
128 inline Reference< interface_type >::Reference(
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 >
153 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_NoAcquire )
154 {
155 _pInterface = castToXInterface(pInterface);
156 }
157
158 template< class interface_type >
159 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_Query )
160 {
161 _pInterface = iquery( rRef.get() );
162 }
163
164 template< class interface_type >
165 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_Query )
166 {
167 _pInterface = iquery( pInterface );
168 }
169
170 template< class interface_type >
171 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_Query )
172 {
173 _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
174 ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
175 }
176
177 template< class interface_type >
178 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_QueryThrow )
179 {
180 _pInterface = iquery_throw( rRef.get() );
181 }
182
183 template< class interface_type >
184 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_QueryThrow )
185 {
186 _pInterface = iquery_throw( pInterface );
187 }
188
189 template< class interface_type >
190 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_QueryThrow )
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 >
197 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow )
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 >
210 inline void Reference< interface_type >::clear()
211 {
212 if (_pInterface)
213 {
214 XInterface * const pOld = _pInterface;
215 _pInterface = 0;
216 pOld->release();
217 }
218 }
219
220 template< class interface_type >
221 inline bool Reference< interface_type >::set(
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 >
234 inline bool Reference< interface_type >::set(
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 >
245 inline bool Reference< interface_type >::set(
246 interface_type * pInterface, UnoReference_NoAcquire )
247 {
248 return set( pInterface, SAL_NO_ACQUIRE );
249 }
250
251
252 template< class interface_type >
253 inline bool Reference< interface_type >::set(
254 const Reference< interface_type > & rRef )
255 {
256 return set( castFromXInterface( rRef._pInterface ) );
257 }
258
259 template< class interface_type >
260 inline bool Reference< interface_type >::set(
261 XInterface * pInterface, UnoReference_Query )
262 {
263 return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
264 }
265
266 template< class interface_type >
267 inline bool Reference< interface_type >::set(
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 >
275 inline bool Reference< interface_type >::set(
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 >
288 inline void Reference< interface_type >::set(
289 XInterface * pInterface, UnoReference_QueryThrow )
290 {
291 set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
292 }
293
294 template< class interface_type >
295 inline void Reference< interface_type >::set(
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 >
303 inline void Reference< interface_type >::set(
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 >
314 inline void Reference< interface_type >::set(
315 interface_type * pInterface, UnoReference_SetThrow )
316 {
317 set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
318 }
319
320 template< class interface_type >
321 inline void Reference< interface_type >::set(
322 const Reference< interface_type > & rRef, UnoReference_SetThrow )
323 {
324 set( rRef.get(), UNO_SET_THROW );
325 }
326
327
328 template< class interface_type >
329 inline Reference< interface_type > & Reference< interface_type >::operator = (
330 interface_type * pInterface )
331 {
332 set( pInterface );
333 return *this;
334 }
335
336 template< class interface_type >
337 inline Reference< interface_type > & Reference< interface_type >::operator = (
338 const Reference< interface_type > & rRef )
339 {
340 set( castFromXInterface( rRef._pInterface ) );
341 return *this;
342 }
343
344
345 template< class interface_type >
346 inline Reference< interface_type > Reference< interface_type >::query(
347 const BaseReference & rRef )
348 {
349 return Reference< interface_type >(
350 castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
351 }
352
353 template< class interface_type >
354 inline Reference< interface_type > Reference< interface_type >::query(
355 XInterface * pInterface )
356 {
357 return Reference< interface_type >(
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
371 Reference< XInterface > x1( _pInterface, UNO_QUERY );
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
382 inline bool BaseReference::operator < (
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:
390 Reference< XInterface > x1( _pInterface, UNO_QUERY );
391 Reference< XInterface > x2( rRef, UNO_QUERY );
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: */
CPPU_DLLPUBLIC
#define CPPU_DLLPUBLIC
Definition: cppudllapi.h:10
com::sun::star::uno::BaseReference::operator<
bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition: Reference.hxx:382
SAL_THROW_EXTERN_C
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:368
com::sun::star::uno::UnoReference_Query
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:136
typelib_TypeClass_INTERFACE
type class of interface
Definition: typeclass.h:79
com::sun::star::uno::Reference::Reference
Reference()
Default Constructor: Sets null reference.
Definition: Reference.hxx:114
Reference.h
com::sun::star::uno::Reference::get
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:411
com::sun::star::uno::BaseReference::get
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:86
com
Definition: types.h:389
typelib_TypeDescriptionReference
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
com::sun::star::uno::UnoReference_QueryThrow
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:145
com::sun::star::uno::BaseReference::iquery
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:49
com::sun::star::uno::UnoReference_SetThrow
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:156
com::sun::star::uno::BaseReference::iquery_throw
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:72
com::sun::star::uno::Reference
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:39
com::sun::star::uno::BaseReference::_pInterface
XInterface * _pInterface
the interface pointer
Definition: Reference.h:63
__sal_NoAcquire
__sal_NoAcquire
Definition: types.h:382
com::sun::star::uno::Reference::query
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:346
com::sun::star::uno::UnoReference_NoAcquire
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition: Reference.h:47
SAL_NO_ACQUIRE
definition of a no acquire enum for ctors
Definition: types.h:386
com::sun::star::uno::BaseReference
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:58
com::sun::star::uno::UNO_QUERY
This enum value can be used for implicit interface query.
Definition: Reference.h:140
config.h
rtl::OUString
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:106
cppu_unsatisfied_iset_msg
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iset_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
com::sun::star::uno::Reference::clear
void clear()
Clears reference, i.e.
Definition: Reference.hxx:210
cppudllapi.h
com::sun::star::uno::Reference::~Reference
~Reference()
Destructor: Releases interface if set.
Definition: Reference.hxx:107
Any.hxx
com::sun::star::uno::BaseReference::operator==
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
com::sun::star::uno::UNO_SET_THROW
Definition: Reference.h:158
com::sun::star::uno::Reference::set
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:253
com::sun::star::uno::Type::getTypeLibType
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:154
com::sun::star::uno::Reference::operator=
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition: Reference.hxx:329
com::sun::star::uno::Type
C++ class representing an IDL meta type.
Definition: Type.h:54
cppu_unsatisfied_iquery_msg
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iquery_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
com::sun::star::uno::Any
C++ class representing an IDL any.
Definition: Any.h:49
com::sun::star::uno::BaseReference::operator!=
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