LibreOffice
LibreOffice 7.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#include <ostream>
26
28#include "com/sun/star/uno/RuntimeException.hpp"
29#include "com/sun/star/uno/XInterface.hpp"
31#include "cppu/cppudllapi.h"
32
33extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iquery_msg(
36extern "C" CPPU_DLLPUBLIC rtl_uString * SAL_CALL cppu_unsatisfied_iset_msg(
39
40namespace com
41{
42namespace sun
43{
44namespace star
45{
46namespace uno
47{
48
49
50inline 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
66template< class interface_type >
67inline XInterface * Reference< interface_type >::iquery(
68 XInterface * pInterface )
69{
70 return BaseReference::iquery(pInterface, interface_type::static_type());
71}
72
73inline 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(
81 Reference< XInterface >( pInterface ) );
82}
83
84template< class interface_type >
86 XInterface * pInterface )
87{
89 pInterface, interface_type::static_type());
90}
91
92template< class interface_type >
93inline 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
106template< class interface_type >
108{
109 if (_pInterface)
110 _pInterface->release();
111}
112
113template< class interface_type >
115{
116 _pInterface = NULL;
117}
118
119template< class interface_type >
121{
122 _pInterface = rRef._pInterface;
123 if (_pInterface)
124 _pInterface->acquire();
125}
126
127#if defined LIBO_INTERNAL_ONLY
128template< class interface_type >
130{
131 _pInterface = rRef._pInterface;
132 rRef._pInterface = nullptr;
133}
134
135template< class interface_type > template< class derived_type >
137 const Reference< derived_type > & rRef,
138 std::enable_if_t<
139 std::is_base_of_v<interface_type, derived_type>
140 && !std::is_same_v<interface_type, XInterface>, void *>)
141{
142 interface_type * p = rRef.get();
143 _pInterface = p;
144 if (_pInterface)
145 _pInterface->acquire();
146}
147#endif
148
149template< class interface_type >
150inline Reference< interface_type >::Reference( interface_type * pInterface )
151{
152 _pInterface = castToXInterface(pInterface);
153 if (_pInterface)
154 _pInterface->acquire();
155}
156
157template< class interface_type >
158inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
159{
160 _pInterface = castToXInterface(pInterface);
161}
162
163template< class interface_type >
165{
166 _pInterface = castToXInterface(pInterface);
167}
168
169template< class interface_type >
171{
172 _pInterface = iquery( rRef.get() );
173}
174
175template< class interface_type >
177{
178 _pInterface = iquery( pInterface );
179}
180
181template< class interface_type >
183{
184 _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
185 ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
186}
187
188template< class interface_type >
190{
191 _pInterface = iquery_throw( rRef.get() );
192}
193
194template< class interface_type >
196{
197 _pInterface = iquery_throw( pInterface );
198}
199
200template< class interface_type >
202{
203 _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
204 ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
205}
206
207template< class interface_type >
209{
210 _pInterface = castToXInterface( iset_throw( rRef.get() ) );
211}
212
213template< class interface_type >
215{
216 _pInterface = castToXInterface( iset_throw( pInterface ) );
217}
218
219
220template< class interface_type >
222{
223 if (_pInterface)
224 {
225 XInterface * const pOld = _pInterface;
226 _pInterface = NULL;
227 pOld->release();
228 }
229}
230
231template< class interface_type >
233 interface_type * pInterface )
234{
235 if (pInterface)
236 castToXInterface(pInterface)->acquire();
237 XInterface * const pOld = _pInterface;
238 _pInterface = castToXInterface(pInterface);
239 if (pOld)
240 pOld->release();
241 return (NULL != pInterface);
242}
243
244template< class interface_type >
246 interface_type * pInterface, __sal_NoAcquire )
247{
248 XInterface * const pOld = _pInterface;
249 _pInterface = castToXInterface(pInterface);
250 if (pOld)
251 pOld->release();
252 return (NULL != pInterface);
253}
254
255template< class interface_type >
257 interface_type * pInterface, UnoReference_NoAcquire )
258{
259 return set( pInterface, SAL_NO_ACQUIRE );
260}
261
262
263template< class interface_type >
265 const Reference< interface_type > & rRef )
266{
267 return set( castFromXInterface( rRef._pInterface ) );
268}
269
270template< class interface_type >
272 XInterface * pInterface, UnoReference_Query )
273{
274 return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
275}
276
277template< class interface_type >
279 const BaseReference & rRef, UnoReference_Query )
280{
281 return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
282}
283
284
285template< class interface_type >
287 Any const & rAny, UnoReference_Query )
288{
289 return set(
290 castFromXInterface(
291 iquery(
292 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
293 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
295}
296
297
298template< class interface_type >
300 XInterface * pInterface, UnoReference_QueryThrow )
301{
302 set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
303}
304
305template< class interface_type >
308{
309 set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
310}
311
312
313template< class interface_type >
315 Any const & rAny, UnoReference_QueryThrow )
316{
317 set( castFromXInterface(
318 iquery_throw(
319 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
320 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
322}
323
324template< class interface_type >
326 interface_type * pInterface, UnoReference_SetThrow )
327{
328 set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
329}
330
331template< class interface_type >
334{
335 set( rRef.get(), UNO_SET_THROW );
336}
337
338
339template< class interface_type >
341 interface_type * pInterface )
342{
343 set( pInterface );
344 return *this;
345}
346
347template< class interface_type >
349 const Reference< interface_type > & rRef )
350{
351 set( castFromXInterface( rRef._pInterface ) );
352 return *this;
353}
354
355#if defined LIBO_INTERNAL_ONLY
356template< class interface_type >
358 Reference< interface_type > && rRef ) noexcept
359{
360 if (_pInterface)
361 _pInterface->release();
362 _pInterface = rRef._pInterface;
363 rRef._pInterface = nullptr;
364 return *this;
365}
366#endif
367
368template< class interface_type >
370 const BaseReference & rRef )
371{
373 castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
374}
375
376template< class interface_type >
378 XInterface * pInterface )
379{
381 castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
382}
383
384
385inline bool BaseReference::operator == ( XInterface * pInterface ) const
386{
387 if (_pInterface == pInterface)
388 return true;
389 try
390 {
391 // only the query to XInterface must return the same pointer if they belong to same objects
393 Reference< XInterface > x2( pInterface, UNO_QUERY );
394 return (x1._pInterface == x2._pInterface);
395 }
396 catch (RuntimeException &)
397 {
398 return false;
399 }
400}
401
402
404 const BaseReference & rRef ) const
405{
406 if (_pInterface == rRef._pInterface)
407 return false;
408 try
409 {
410 // only the query to XInterface must return the same pointer:
413 return (x1._pInterface < x2._pInterface);
414 }
415 catch (RuntimeException &)
416 {
417 return false;
418 }
419}
420
421
422inline bool BaseReference::operator != ( XInterface * pInterface ) const
423{
424 return (! operator == ( pInterface ));
425}
426
427inline bool BaseReference::operator == ( const BaseReference & rRef ) const
428{
429 return operator == ( rRef._pInterface );
430}
431
432inline bool BaseReference::operator != ( const BaseReference & rRef ) const
433{
434 return (! operator == ( rRef._pInterface ));
435}
436
437#if defined LIBO_INTERNAL_ONLY
444template<typename charT, typename traits> std::basic_ostream<charT, traits> &
446 std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
447{ return stream << ref.get(); }
448#endif
449
450}
451}
452}
453}
454
455#if defined LIBO_INTERNAL_ONLY
456namespace std
457{
458
464template<typename T>
465struct hash<::css::uno::Reference<T>>
466{
467 std::size_t operator()(::css::uno::Reference<T> const & s) const
468 { return size_t(s.get()); }
469};
470
471}
472
473#endif
474
475#endif
476
477/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
__sal_NoAcquire
Definition: types.h:349
@ SAL_NO_ACQUIRE
definition of a no acquire enum for ctors
Definition: types.h:352
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won't throw in practice,...
Definition: types.h:333
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:324
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:78
#define CPPU_DLLPUBLIC
Definition: cppudllapi.h:10
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: unotype.hxx:35
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:695
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition: Reference.h:52
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:159
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:150
@ UNO_QUERY
This enum value can be used for implicit interface query.
Definition: Reference.h:153
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:170
@ UNO_SET_THROW
Definition: Reference.h:171
Template reference class for interface type derived from BaseReference.
Definition: Reference.h:180
static SAL_WARN_UNUSED_RESULT Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:369
Reference< interface_type > & operator=(interface_type *pInterface)
Assignment operator: Acquires given interface pointer and sets reference.
Definition: Reference.hxx:340
void clear()
Clears reference, i.e.
Definition: Reference.hxx:221
bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:264
interface_type * get() const
Gets interface pointer.
Definition: Reference.h:399
Reference()
Default Constructor: Sets null reference.
Definition: Reference.hxx:114
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor: Releases interface if set.
Definition: Reference.hxx:107
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:161
C++ class representing an IDL any.
Definition: Any.h:53
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:63
bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition: Reference.hxx:403
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:50
bool operator!=(XInterface *pInterface) const
Inequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition: Reference.hxx:422
XInterface * get() const
Gets interface pointer.
Definition: Reference.h:90
XInterface * _pInterface
the interface pointer
Definition: Reference.h:67
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:73
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:385
C++ class representing an IDL meta type.
Definition: Type.h:55
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:154