LibreOffice LibreOffice 5.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
22 #include <sal/config.h>
23
24 #include <cstddef>
25 #include <ostream>
26
27 #include <com/sun/star/uno/Reference.h>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <com/sun/star/uno/XInterface.hpp>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <cppu/cppudllapi.h>
32
33 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
34 typelib_TypeDescriptionReference * pType )
35 SAL_THROW_EXTERN_C();
36 extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
37 typelib_TypeDescriptionReference * pType )
38 SAL_THROW_EXTERN_C();
39
40 namespace com
41 {
42 namespace sun
43 {
44 namespace star
45 {
46 namespace uno
47 {
48
49
50 inline XInterface * BaseReference::iquery(
51 XInterface * pInterface, const Type & rType )
52 {
53 if (pInterface)
54 {
55 Any aRet( pInterface->queryInterface( rType ) );
56 if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
57 {
58 XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
59 aRet.pReserved = NULL;
60 return pRet;
61 }
62 }
63 return NULL;
64 }
65
66 template< class interface_type >
67 inline XInterface * Reference< interface_type >::iquery(
68 XInterface * pInterface )
69 {
70 return BaseReference::iquery(pInterface, interface_type::static_type());
71 }
72
73 inline XInterface * BaseReference::iquery_throw(
74 XInterface * pInterface, const Type & rType )
75 {
76 XInterface * pQueried = iquery( pInterface, rType );
77 if (pQueried)
78 return pQueried;
79 throw RuntimeException(
80 ::rtl::OUString( cppu_unsatisfied_iquery_msg( rType.getTypeLibType() ), SAL_NO_ACQUIRE ),
81 Reference< XInterface >( pInterface ) );
82 }
83
84 template< class interface_type >
85 inline XInterface * Reference< interface_type >::iquery_throw(
86 XInterface * pInterface )
87 {
88 return BaseReference::iquery_throw(
89 pInterface, interface_type::static_type());
90 }
91
92 template< class interface_type >
93 inline interface_type * Reference< interface_type >::iset_throw(
94 interface_type * pInterface )
95 {
96 if (pInterface)
97 {
98 castToXInterface(pInterface)->acquire();
99 return pInterface;
100 }
101 throw RuntimeException(
102 ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
103 NULL );
104 }
105
106
107 template< class interface_type >
108 inline Reference< interface_type >::~Reference()
109 {
110 if (_pInterface)
111 _pInterface->release();
112 }
113
114 template< class interface_type >
115 inline Reference< interface_type >::Reference()
116 {
117 _pInterface = NULL;
118 }
119
120 template< class interface_type >
121 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef )
122 {
123 _pInterface = rRef._pInterface;
124 if (_pInterface)
125 _pInterface->acquire();
126 }
127
128 #if defined LIBO_INTERNAL_ONLY
129 template< class interface_type >
130 inline Reference< interface_type >::Reference( Reference< interface_type > && rRef )
131 {
132 _pInterface = rRef._pInterface;
133 rRef._pInterface = nullptr;
134 }
135 #endif
136
137 template< class interface_type > template< class derived_type >
138 inline Reference< interface_type >::Reference(
139 const Reference< derived_type > & rRef,
140 typename detail::UpCast< interface_type, derived_type >::t )
141 {
142 interface_type * p = rRef.get();
143 _pInterface = p;
144 if (_pInterface)
145 _pInterface->acquire();
146 }
147
148 template< class interface_type >
149 inline Reference< interface_type >::Reference( interface_type * pInterface )
150 {
151 _pInterface = castToXInterface(pInterface);
152 if (_pInterface)
153 _pInterface->acquire();
154 }
155
156 template< class interface_type >
157 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
158 {
159 _pInterface = castToXInterface(pInterface);
160 }
161
162 template< class interface_type >
163 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_NoAcquire )
164 {
165 _pInterface = castToXInterface(pInterface);
166 }
167
168 template< class interface_type >
169 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_Query )
170 {
171 _pInterface = iquery( rRef.get() );
172 }
173
174 template< class interface_type >
175 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_Query )
176 {
177 _pInterface = iquery( pInterface );
178 }
179
180 template< class interface_type >
181 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_Query )
182 {
183 _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
184 ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
185 }
186
187 template< class interface_type >
188 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_QueryThrow )
189 {
190 _pInterface = iquery_throw( rRef.get() );
191 }
192
193 template< class interface_type >
194 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_QueryThrow )
195 {
196 _pInterface = iquery_throw( pInterface );
197 }
198
199 template< class interface_type >
200 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_QueryThrow )
201 {
202 _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
203 ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
204 }
205
206 template< class interface_type >
207 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow )
208 {
209 _pInterface = castToXInterface( iset_throw( rRef.get() ) );
210 }
211
212 template< class interface_type >
213 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
214 {
215 _pInterface = castToXInterface( iset_throw( pInterface ) );
216 }
217
218
219 template< class interface_type >
220 inline void Reference< interface_type >::clear()
221 {
222 if (_pInterface)
223 {
224 XInterface * const pOld = _pInterface;
225 _pInterface = NULL;
226 pOld->release();
227 }
228 }
229
230 template< class interface_type >
231 inline bool Reference< interface_type >::set(
232 interface_type * pInterface )
233 {
234 if (pInterface)
235 castToXInterface(pInterface)->acquire();
236 XInterface * const pOld = _pInterface;
237 _pInterface = castToXInterface(pInterface);
238 if (pOld)
239 pOld->release();
240 return (NULL != pInterface);
241 }
242
243 template< class interface_type >
244 inline bool Reference< interface_type >::set(
245 interface_type * pInterface, __sal_NoAcquire )
246 {
247 XInterface * const pOld = _pInterface;
248 _pInterface = castToXInterface(pInterface);
249 if (pOld)
250 pOld->release();
251 return (NULL != pInterface);
252 }
253
254 template< class interface_type >
255 inline bool Reference< interface_type >::set(
256 interface_type * pInterface, UnoReference_NoAcquire )
257 {
258 return set( pInterface, SAL_NO_ACQUIRE );
259 }
260
261
262 template< class interface_type >
263 inline bool Reference< interface_type >::set(
264 const Reference< interface_type > & rRef )
265 {
266 return set( castFromXInterface( rRef._pInterface ) );
267 }
268
269 template< class interface_type >
270 inline bool Reference< interface_type >::set(
271 XInterface * pInterface, UnoReference_Query )
272 {
273 return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
274 }
275
276 template< class interface_type >
277 inline bool Reference< interface_type >::set(
278 const BaseReference & rRef, UnoReference_Query )
279 {
280 return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
281 }
282
283
284 template< class interface_type >
285 inline bool Reference< interface_type >::set(
286 Any const & rAny, UnoReference_Query )
287 {
288 return set(
289 castFromXInterface(
290 iquery(
291 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
292 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
293 SAL_NO_ACQUIRE );
294 }
295
296
297 template< class interface_type >
298 inline void Reference< interface_type >::set(
299 XInterface * pInterface, UnoReference_QueryThrow )
300 {
301 set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
302 }
303
304 template< class interface_type >
305 inline void Reference< interface_type >::set(
306 const BaseReference & rRef, UnoReference_QueryThrow )
307 {
308 set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
309 }
310
311
312 template< class interface_type >
313 inline void Reference< interface_type >::set(
314 Any const & rAny, UnoReference_QueryThrow )
315 {
316 set( castFromXInterface(
317 iquery_throw(
318 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
319 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
320 SAL_NO_ACQUIRE );
321 }
322
323 template< class interface_type >
324 inline void Reference< interface_type >::set(
325 interface_type * pInterface, UnoReference_SetThrow )
326 {
327 set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
328 }
329
330 template< class interface_type >
331 inline void Reference< interface_type >::set(
332 const Reference< interface_type > & rRef, UnoReference_SetThrow )
333 {
334 set( rRef.get(), UNO_SET_THROW );
335 }
336
337
338 template< class interface_type >
339 inline Reference< interface_type > & Reference< interface_type >::operator = (
340 interface_type * pInterface )
341 {
342 set( pInterface );
343 return *this;
344 }
345
346 template< class interface_type >
347 inline Reference< interface_type > & Reference< interface_type >::operator = (
348 const Reference< interface_type > & rRef )
349 {
350 set( castFromXInterface( rRef._pInterface ) );
351 return *this;
352 }
353
354 #if defined LIBO_INTERNAL_ONLY
355 template< class interface_type >
356 inline Reference< interface_type > & Reference< interface_type >::operator = (
357 Reference< interface_type > && rRef )
358 {
359 if (_pInterface)
360 _pInterface->release();
361 _pInterface = rRef._pInterface;
362 rRef._pInterface = nullptr;
363 return *this;
364 }
365 #endif
366
367 template< class interface_type >
368 inline Reference< interface_type > Reference< interface_type >::query(
369 const BaseReference & rRef )
370 {
371 return Reference< interface_type >(
372 castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
373 }
374
375 template< class interface_type >
376 inline Reference< interface_type > Reference< interface_type >::query(
377 XInterface * pInterface )
378 {
379 return Reference< interface_type >(
380 castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
381 }
382
383
384 inline bool BaseReference::operator == ( XInterface * pInterface ) const
385 {
386 if (_pInterface == pInterface)
387 return true;
388 try
389 {
390 // only the query to XInterface must return the same pointer if they belong to same objects
391 Reference< XInterface > x1( _pInterface, UNO_QUERY );
392 Reference< XInterface > x2( pInterface, UNO_QUERY );
393 return (x1._pInterface == x2._pInterface);
394 }
395 catch (RuntimeException &)
396 {
397 return false;
398 }
399 }
400
401
402 inline bool BaseReference::operator < (
403 const BaseReference & rRef ) const
404 {
405 if (_pInterface == rRef._pInterface)
406 return false;
407 try
408 {
409 // only the query to XInterface must return the same pointer:
410 Reference< XInterface > x1( _pInterface, UNO_QUERY );
411 Reference< XInterface > x2( rRef, UNO_QUERY );
412 return (x1._pInterface < x2._pInterface);
413 }
414 catch (RuntimeException &)
415 {
416 return false;
417 }
418 }
419
420
421 inline bool BaseReference::operator != ( XInterface * pInterface ) const
422 {
423 return (! operator == ( pInterface ));
424 }
425
426 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
427 {
428 return operator == ( rRef._pInterface );
429 }
430
431 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
432 {
433 return (! operator == ( rRef._pInterface ));
434 }
435
436 #if defined LIBO_INTERNAL_ONLY
437
443 template<typename charT, typename traits> std::basic_ostream<charT, traits> &
444 operator <<(
445 std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
446 { return stream << ref.get(); }
447 #endif
448
449 }
450 }
451 }
452 }
453
454 #endif
455
456 /* 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:402
SAL_THROW_EXTERN_C
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:352
com::sun::star::uno::UnoReference_Query
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:145
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:115
Reference.h
com::sun::star::uno::Reference::get
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:428
com::sun::star::uno::BaseReference::get
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:86
com
Definition: types.h:373
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:154
com::sun::star::uno::BaseReference::iquery
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:50
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:165
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:73
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:366
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:368
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:370
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:149
config.h
rtl::OUString
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:120
cppu_unsatisfied_iset_msg
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iset_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
com::sun::star::uno::operator<<
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &o, Any const &any)
Support for Any in std::ostream (and thus in CPPUNIT_ASSERT or SAL_INFO macros, for example)...
Definition: Any.hxx:664
com::sun::star::uno::Reference::clear
void clear()
Clears reference, i.e.
Definition: Reference.hxx:220
cppudllapi.h
com::sun::star::uno::Reference::~Reference
~Reference()
Destructor: Releases interface if set.
Definition: Reference.hxx:108
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:384
com::sun::star::uno::UNO_SET_THROW
Definition: Reference.h:167
com::sun::star::uno::Reference::set
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:263
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:339
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:421