LibreOffice LibreOffice 5.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
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 = NULL;
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 #if defined LIBO_INTERNAL_ONLY
128 template< class interface_type >
129 inline Reference< interface_type >::Reference( Reference< interface_type > && rRef )
130 {
131 _pInterface = rRef._pInterface;
132 rRef._pInterface = nullptr;
133 }
134 #endif
135
136 template< class interface_type > template< class derived_type >
137 inline Reference< interface_type >::Reference(
138 const Reference< derived_type > & rRef,
139 typename detail::UpCast< interface_type, derived_type >::t )
140 {
141 interface_type * p = rRef.get();
142 _pInterface = p;
143 if (_pInterface)
144 _pInterface->acquire();
145 }
146
147 template< class interface_type >
148 inline Reference< interface_type >::Reference( interface_type * pInterface )
149 {
150 _pInterface = castToXInterface(pInterface);
151 if (_pInterface)
152 _pInterface->acquire();
153 }
154
155 template< class interface_type >
156 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
157 {
158 _pInterface = castToXInterface(pInterface);
159 }
160
161 template< class interface_type >
162 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_NoAcquire )
163 {
164 _pInterface = castToXInterface(pInterface);
165 }
166
167 template< class interface_type >
168 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_Query )
169 {
170 _pInterface = iquery( rRef.get() );
171 }
172
173 template< class interface_type >
174 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_Query )
175 {
176 _pInterface = iquery( pInterface );
177 }
178
179 template< class interface_type >
180 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_Query )
181 {
182 _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
183 ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
184 }
185
186 template< class interface_type >
187 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_QueryThrow )
188 {
189 _pInterface = iquery_throw( rRef.get() );
190 }
191
192 template< class interface_type >
193 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_QueryThrow )
194 {
195 _pInterface = iquery_throw( pInterface );
196 }
197
198 template< class interface_type >
199 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_QueryThrow )
200 {
201 _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
202 ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
203 }
204
205 template< class interface_type >
206 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow )
207 {
208 _pInterface = castToXInterface( iset_throw( rRef.get() ) );
209 }
210
211 template< class interface_type >
212 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
213 {
214 _pInterface = castToXInterface( iset_throw( pInterface ) );
215 }
216
217
218 template< class interface_type >
219 inline void Reference< interface_type >::clear()
220 {
221 if (_pInterface)
222 {
223 XInterface * const pOld = _pInterface;
224 _pInterface = NULL;
225 pOld->release();
226 }
227 }
228
229 template< class interface_type >
230 inline bool Reference< interface_type >::set(
231 interface_type * pInterface )
232 {
233 if (pInterface)
234 castToXInterface(pInterface)->acquire();
235 XInterface * const pOld = _pInterface;
236 _pInterface = castToXInterface(pInterface);
237 if (pOld)
238 pOld->release();
239 return (NULL != pInterface);
240 }
241
242 template< class interface_type >
243 inline bool Reference< interface_type >::set(
244 interface_type * pInterface, __sal_NoAcquire )
245 {
246 XInterface * const pOld = _pInterface;
247 _pInterface = castToXInterface(pInterface);
248 if (pOld)
249 pOld->release();
250 return (NULL != pInterface);
251 }
252
253 template< class interface_type >
254 inline bool Reference< interface_type >::set(
255 interface_type * pInterface, UnoReference_NoAcquire )
256 {
257 return set( pInterface, SAL_NO_ACQUIRE );
258 }
259
260
261 template< class interface_type >
262 inline bool Reference< interface_type >::set(
263 const Reference< interface_type > & rRef )
264 {
265 return set( castFromXInterface( rRef._pInterface ) );
266 }
267
268 template< class interface_type >
269 inline bool Reference< interface_type >::set(
270 XInterface * pInterface, UnoReference_Query )
271 {
272 return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
273 }
274
275 template< class interface_type >
276 inline bool Reference< interface_type >::set(
277 const BaseReference & rRef, UnoReference_Query )
278 {
279 return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
280 }
281
282
283 template< class interface_type >
284 inline bool Reference< interface_type >::set(
285 Any const & rAny, UnoReference_Query )
286 {
287 return set(
288 castFromXInterface(
289 iquery(
290 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
291 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
292 SAL_NO_ACQUIRE );
293 }
294
295
296 template< class interface_type >
297 inline void Reference< interface_type >::set(
298 XInterface * pInterface, UnoReference_QueryThrow )
299 {
300 set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
301 }
302
303 template< class interface_type >
304 inline void Reference< interface_type >::set(
305 const BaseReference & rRef, UnoReference_QueryThrow )
306 {
307 set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
308 }
309
310
311 template< class interface_type >
312 inline void Reference< interface_type >::set(
313 Any const & rAny, UnoReference_QueryThrow )
314 {
315 set( castFromXInterface(
316 iquery_throw(
317 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
318 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
319 SAL_NO_ACQUIRE );
320 }
321
322 template< class interface_type >
323 inline void Reference< interface_type >::set(
324 interface_type * pInterface, UnoReference_SetThrow )
325 {
326 set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
327 }
328
329 template< class interface_type >
330 inline void Reference< interface_type >::set(
331 const Reference< interface_type > & rRef, UnoReference_SetThrow )
332 {
333 set( rRef.get(), UNO_SET_THROW );
334 }
335
336
337 template< class interface_type >
338 inline Reference< interface_type > & Reference< interface_type >::operator = (
339 interface_type * pInterface )
340 {
341 set( pInterface );
342 return *this;
343 }
344
345 template< class interface_type >
346 inline Reference< interface_type > & Reference< interface_type >::operator = (
347 const Reference< interface_type > & rRef )
348 {
349 set( castFromXInterface( rRef._pInterface ) );
350 return *this;
351 }
352
353 #if defined LIBO_INTERNAL_ONLY
354 template< class interface_type >
355 inline Reference< interface_type > & Reference< interface_type >::operator = (
356 Reference< interface_type > && rRef )
357 {
358 if (_pInterface)
359 _pInterface->release();
360 _pInterface = rRef._pInterface;
361 rRef._pInterface = nullptr;
362 return *this;
363 }
364 #endif
365
366 template< class interface_type >
367 inline Reference< interface_type > Reference< interface_type >::query(
368 const BaseReference & rRef )
369 {
370 return Reference< interface_type >(
371 castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
372 }
373
374 template< class interface_type >
375 inline Reference< interface_type > Reference< interface_type >::query(
376 XInterface * pInterface )
377 {
378 return Reference< interface_type >(
379 castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
380 }
381
382
383 inline bool BaseReference::operator == ( XInterface * pInterface ) const
384 {
385 if (_pInterface == pInterface)
386 return true;
387 try
388 {
389 // only the query to XInterface must return the same pointer if they belong to same objects
390 Reference< XInterface > x1( _pInterface, UNO_QUERY );
391 Reference< XInterface > x2( pInterface, 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 < (
402 const BaseReference & rRef ) const
403 {
404 if (_pInterface == rRef._pInterface)
405 return false;
406 try
407 {
408 // only the query to XInterface must return the same pointer:
409 Reference< XInterface > x1( _pInterface, UNO_QUERY );
410 Reference< XInterface > x2( rRef, UNO_QUERY );
411 return (x1._pInterface < x2._pInterface);
412 }
413 catch (RuntimeException &)
414 {
415 return false;
416 }
417 }
418
419
420 inline bool BaseReference::operator != ( XInterface * pInterface ) const
421 {
422 return (! operator == ( pInterface ));
423 }
424
425 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
426 {
427 return operator == ( rRef._pInterface );
428 }
429
430 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
431 {
432 return (! operator == ( rRef._pInterface ));
433 }
434
435 }
436 }
437 }
438 }
439
440 #endif
441
442 /* 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:401
SAL_THROW_EXTERN_C
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:370
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:419
com::sun::star::uno::BaseReference::get
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:86
com
Definition: types.h:391
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:384
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:367
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:388
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:219
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:383
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:262
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:338
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:52
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:420