LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
Loading...
Searching...
No Matches
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
20/*
21 * This file is part of LibreOffice published API.
22 */
23#ifndef INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
24#define INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
25
26#include "sal/config.h"
27
28#include <cstddef>
29#include <ostream>
30
32#include "com/sun/star/uno/RuntimeException.hpp"
33#include "com/sun/star/uno/XInterface.hpp"
35#include "cppu/cppudllapi.h"
36
37extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
40extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
43
44namespace com
45{
46namespace sun
47{
48namespace star
49{
50namespace uno
51{
52
53
54inline XInterface * BaseReference::iquery(
55 XInterface * pInterface, const Type & rType )
56{
57 if (pInterface)
58 {
59 Any aRet( pInterface->queryInterface( rType ) );
60 if (typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass)
61 {
62 XInterface * pRet = static_cast< XInterface * >( aRet.pReserved );
63 aRet.pReserved = NULL;
64 return pRet;
65 }
66 }
67 return NULL;
68}
69
70template< class interface_type >
71inline XInterface * Reference< interface_type >::iquery(
72 XInterface * pInterface )
73{
74 return BaseReference::iquery(pInterface, interface_type::static_type());
75}
76
77inline XInterface * BaseReference::iquery_throw(
78 XInterface * pInterface, const Type & rType )
79{
80 XInterface * pQueried = iquery( pInterface, rType );
81 if (pQueried)
82 return pQueried;
83 throw RuntimeException(
85 Reference< XInterface >( pInterface ) );
86}
87
88template< class interface_type >
90 XInterface * pInterface )
91{
93 pInterface, interface_type::static_type());
94}
95
96template< class interface_type >
97inline interface_type * Reference< interface_type >::iset_throw(
98 interface_type * pInterface )
99{
100 if (pInterface)
101 {
102 castToXInterface(pInterface)->acquire();
103 return pInterface;
104 }
105 throw RuntimeException(
106 ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
107 NULL );
108}
109
110template< class interface_type >
112{
113 if (_pInterface)
114 _pInterface->release();
115}
116
117template< class interface_type >
119{
120 _pInterface = NULL;
121}
122
123template< class interface_type >
125{
126 _pInterface = rRef._pInterface;
127 if (_pInterface)
128 _pInterface->acquire();
129}
130
131#if defined LIBO_INTERNAL_ONLY
132template< class interface_type >
134{
135 _pInterface = rRef._pInterface;
136 rRef._pInterface = nullptr;
137}
138
139template< class interface_type > template< class derived_type >
141 const Reference< derived_type > & rRef,
142 std::enable_if_t<
143 std::is_base_of_v<interface_type, derived_type>
144 && !std::is_same_v<interface_type, XInterface>, void *>)
145{
146 interface_type * p = rRef.get();
147 _pInterface = castToXInterface(p);
148 if (_pInterface)
149 _pInterface->acquire();
150}
151#endif
152
153template< class interface_type >
154inline Reference< interface_type >::Reference( interface_type * pInterface )
155{
156 _pInterface = castToXInterface(pInterface);
157 if (_pInterface)
158 _pInterface->acquire();
159}
160
161template< class interface_type >
162inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
163{
164 _pInterface = castToXInterface(pInterface);
165}
166
167template< class interface_type >
169{
170 _pInterface = castToXInterface(pInterface);
171}
172
173template< class interface_type >
175{
176 _pInterface = iquery( rRef.get() );
177}
178
179template< class interface_type >
181{
182 _pInterface = iquery( pInterface );
183}
184
185template< class interface_type >
187{
188 _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
189 ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
190}
191
192template< class interface_type >
194{
195 _pInterface = iquery_throw( rRef.get() );
196}
197
198template< class interface_type >
200{
201 _pInterface = iquery_throw( pInterface );
202}
203
204template< class interface_type >
206{
207 _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
208 ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
209}
210
211template< class interface_type >
213{
214 _pInterface = castToXInterface( iset_throw( rRef.get() ) );
215}
216
217template< class interface_type >
219{
220 _pInterface = castToXInterface( iset_throw( pInterface ) );
221}
222
223
224template< class interface_type >
226{
227 if (_pInterface)
228 {
229 XInterface * const pOld = _pInterface;
230 _pInterface = NULL;
231 pOld->release();
232 }
233}
234
235template< class interface_type >
237 interface_type * pInterface )
238{
239 if (pInterface)
240 castToXInterface(pInterface)->acquire();
241 XInterface * const pOld = _pInterface;
242 _pInterface = castToXInterface(pInterface);
243 if (pOld)
244 pOld->release();
245 return (NULL != pInterface);
246}
247
248template< class interface_type >
250 interface_type * pInterface, __sal_NoAcquire )
251{
252 XInterface * const pOld = _pInterface;
253 _pInterface = castToXInterface(pInterface);
254 if (pOld)
255 pOld->release();
256 return (NULL != pInterface);
257}
258
259template< class interface_type >
261 interface_type * pInterface, UnoReference_NoAcquire )
262{
263 return set( pInterface, SAL_NO_ACQUIRE );
264}
265
266
267template< class interface_type >
269 const Reference< interface_type > & rRef )
270{
271 return set( castFromXInterface( rRef._pInterface ) );
272}
273
274template< class interface_type >
276 XInterface * pInterface, UnoReference_Query )
277{
278 return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
279}
280
281template< class interface_type >
283 const BaseReference & rRef, UnoReference_Query )
284{
285 return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
286}
287
288
289template< class interface_type >
291 Any const & rAny, UnoReference_Query )
292{
293 return set(
294 castFromXInterface(
295 iquery(
296 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
297 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
299}
300
301
302template< class interface_type >
304 XInterface * pInterface, UnoReference_QueryThrow )
305{
306 set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
307}
308
309template< class interface_type >
312{
313 set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
314}
315
316
317template< class interface_type >
319 Any const & rAny, UnoReference_QueryThrow )
320{
321 set( castFromXInterface(
322 iquery_throw(
323 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
324 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
326}
327
328template< class interface_type >
330 interface_type * pInterface, UnoReference_SetThrow )
332 set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
333}
334
335template< class interface_type >
338{
339 set( rRef.get(), UNO_SET_THROW );
340}
341
342
343template< class interface_type >
345 interface_type * pInterface )
346{
347 set( pInterface );
348 return *this;
349}
350
351template< class interface_type >
354{
355 set( castFromXInterface( rRef._pInterface ) );
356 return *this;
357}
358
359#if defined LIBO_INTERNAL_ONLY
360template< class interface_type >
363{
364 if (_pInterface)
365 _pInterface->release();
366 _pInterface = rRef._pInterface;
367 rRef._pInterface = nullptr;
368 return *this;
369}
370#endif
372template< class interface_type >
374 const BaseReference & rRef )
375{
377 castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
378}
379
380template< class interface_type >
382 XInterface * pInterface )
383{
385 castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
386}
387
388#if defined LIBO_INTERNAL_ONLY
389template< class interface_type > template< class other_type >
391{
392 return Reference< other_type >(*this, UNO_QUERY);
393}
394
395template< class interface_type > template< class other_type >
396inline Reference< other_type > Reference< interface_type >::queryThrow() const
397{
398 return Reference< other_type >(*this, UNO_QUERY_THROW);
399}
400
401template< class interface_type >
402inline Reference< interface_type > Any::query() const
403{
404 return Reference< interface_type >(*this, UNO_QUERY);
405}
406
407template< class interface_type >
408inline Reference< interface_type > Any::queryThrow() const
409{
411}
412#endif
413
414
415inline bool BaseReference::operator == ( XInterface * pInterface ) const
416{
417 if (_pInterface == pInterface)
418 return true;
419 try
420 {
421 // only the query to XInterface must return the same pointer if they belong to same objects
423 Reference< XInterface > x2( pInterface, UNO_QUERY );
424 return (x1._pInterface == x2._pInterface);
425 }
426 catch (RuntimeException &)
427 {
428 return false;
430}
431
432
434 const BaseReference & rRef ) const
435{
436 if (_pInterface == rRef._pInterface)
437 return false;
438 try
439 {
440 // only the query to XInterface must return the same pointer:
443 return (x1._pInterface < x2._pInterface);
444 }
445 catch (RuntimeException &)
446 {
447 return false;
448 }
449}
450
451
452inline bool BaseReference::operator != ( XInterface * pInterface ) const
453{
454 return (! operator == ( pInterface ));
456
457inline bool BaseReference::operator == ( const BaseReference & rRef ) const
458{
459 return operator == ( rRef._pInterface );
460}
461
462inline bool BaseReference::operator != ( const BaseReference & rRef ) const
463{
464 return (! operator == ( rRef._pInterface ));
465}
466
467#if defined LIBO_INTERNAL_ONLY
474template<typename charT, typename traits> std::basic_ostream<charT, traits> &
476 std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
477{ return stream << ref.get(); }
478#endif
479
480}
481}
482}
483}
484
485#if defined LIBO_INTERNAL_ONLY
486namespace std
488
494template<typename T>
495struct hash<::css::uno::Reference<T>>
496{
497 std::size_t operator()(::css::uno::Reference<T> const & s) const
498 { return size_t(s.get()); }
499};
500
501}
502
503#endif
505#endif
506
507/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
__sal_NoAcquire
Definition types.h:353
@ SAL_NO_ACQUIRE
definition of a no acquire enum for ctors
Definition types.h:356
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won't throw in practice,...
Definition types.h:349
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition types.h:334
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
@ typelib_TypeClass_INTERFACE
type class of interface
Definition typeclass.h:82
#define CPPU_DLLPUBLIC
Definition cppudllapi.h:13
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iset_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
CPPU_DLLPUBLIC rtl_uString * cppu_unsatisfied_iquery_msg(typelib_TypeDescriptionReference *pType) SAL_THROW_EXTERN_C()
Definition types.h:359
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:706
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition Reference.h:56
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition Reference.h:163
@ UNO_QUERY_THROW
This enum value can be used for implicit interface query.
Definition Reference.h:166
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition Reference.h:154
@ UNO_QUERY
This enum value can be used for implicit interface query.
Definition Reference.h:157
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition Reference.h:174
@ UNO_SET_THROW
Definition Reference.h:175
This String class provides base functionality for C++ like Unicode character array handling.
Definition ustring.hxx:171
Template reference class for interface type derived from BaseReference.
Definition Reference.h:184
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition Reference.hxx:373
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition Reference.hxx:344
void clear()
Clears reference, i.e.
Definition Reference.hxx:225
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition Reference.hxx:268
interface_type * get() const
Gets interface pointer.
Definition Reference.h:403
Reference()
Default Constructor: Sets null reference.
Definition Reference.hxx:118
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor: Releases interface if set.
Definition Reference.hxx:111
C++ class representing an IDL any.
Definition Any.h:58
This base class serves as a base class for all template reference classes and has been introduced due...
Definition Reference.h:67
bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition Reference.hxx:433
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition Reference.hxx:54
bool operator!=(XInterface *pInterface) const
Inequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition Reference.hxx:452
XInterface * get() const
Gets interface pointer.
Definition Reference.h:94
XInterface * _pInterface
the interface pointer
Definition Reference.h:71
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition Reference.hxx:77
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:415
C++ class representing an IDL meta type.
Definition Type.h:59
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition Type.h:162