1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
20 #define INCLUDED_COM_SUN_STAR_UNO_REFERENCE_HXX
22 #include "sal/config.h"
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"
33 extern "C" CPPU_DLLPUBLIC rtl_uString
* SAL_CALL
cppu_unsatisfied_iquery_msg(
34 typelib_TypeDescriptionReference
* pType
)
36 extern "C" CPPU_DLLPUBLIC rtl_uString
* SAL_CALL
cppu_unsatisfied_iset_msg(
37 typelib_TypeDescriptionReference
* pType
)
50 inline XInterface
* BaseReference::iquery(
51 XInterface
* pInterface
, const Type
& rType
)
55 Any
aRet( pInterface
->queryInterface( rType
) );
56 if (typelib_TypeClass_INTERFACE
== aRet
.pType
->eTypeClass
)
58 XInterface
* pRet
= static_cast< XInterface
* >( aRet
.pReserved
);
59 aRet
.pReserved
= NULL
;
66 template< class interface_type
>
67 inline XInterface
* Reference
< interface_type
>::iquery(
68 XInterface
* pInterface
)
70 return BaseReference::iquery(pInterface
, interface_type::static_type());
73 inline XInterface
* BaseReference::iquery_throw(
74 XInterface
* pInterface
, const Type
& rType
)
76 XInterface
* pQueried
= iquery( pInterface
, rType
);
79 throw RuntimeException(
80 ::rtl::OUString( cppu_unsatisfied_iquery_msg( rType
.getTypeLibType() ), SAL_NO_ACQUIRE
),
81 Reference
< XInterface
>( pInterface
) );
84 template< class interface_type
>
85 inline XInterface
* Reference
< interface_type
>::iquery_throw(
86 XInterface
* pInterface
)
88 return BaseReference::iquery_throw(
89 pInterface
, interface_type::static_type());
92 template< class interface_type
>
93 inline interface_type
* Reference
< interface_type
>::iset_throw(
94 interface_type
* pInterface
)
98 castToXInterface(pInterface
)->acquire();
101 throw RuntimeException(
102 ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE
),
106 template< class interface_type
>
107 inline Reference
< interface_type
>::~Reference() COVERITY_NOEXCEPT_FALSE
110 _pInterface
->release();
113 template< class interface_type
>
114 inline Reference
< interface_type
>::Reference()
119 template< class interface_type
>
120 inline Reference
< interface_type
>::Reference( const Reference
< interface_type
> & rRef
)
122 _pInterface
= rRef
._pInterface
;
124 _pInterface
->acquire();
127 #if defined LIBO_INTERNAL_ONLY
128 template< class interface_type
>
129 inline Reference
< interface_type
>::Reference( Reference
< interface_type
> && rRef
) noexcept
131 _pInterface
= rRef
._pInterface
;
132 rRef
._pInterface
= nullptr;
135 template< class interface_type
> template< class derived_type
>
136 inline Reference
< interface_type
>::Reference(
137 const Reference
< derived_type
> & rRef
,
139 std::is_base_of_v
<interface_type
, derived_type
>
140 && !std::is_same_v
<interface_type
, XInterface
>, void *>)
142 interface_type
* p
= rRef
.get();
145 _pInterface
->acquire();
149 template< class interface_type
>
150 inline Reference
< interface_type
>::Reference( interface_type
* pInterface
)
152 _pInterface
= castToXInterface(pInterface
);
154 _pInterface
->acquire();
157 template< class interface_type
>
158 inline Reference
< interface_type
>::Reference( interface_type
* pInterface
, __sal_NoAcquire
)
160 _pInterface
= castToXInterface(pInterface
);
163 template< class interface_type
>
164 inline Reference
< interface_type
>::Reference( interface_type
* pInterface
, UnoReference_NoAcquire
)
166 _pInterface
= castToXInterface(pInterface
);
169 template< class interface_type
>
170 inline Reference
< interface_type
>::Reference( const BaseReference
& rRef
, UnoReference_Query
)
172 _pInterface
= iquery( rRef
.get() );
175 template< class interface_type
>
176 inline Reference
< interface_type
>::Reference( XInterface
* pInterface
, UnoReference_Query
)
178 _pInterface
= iquery( pInterface
);
181 template< class interface_type
>
182 inline Reference
< interface_type
>::Reference( const Any
& rAny
, UnoReference_Query
)
184 _pInterface
= (typelib_TypeClass_INTERFACE
== rAny
.pType
->eTypeClass
185 ? iquery( static_cast< XInterface
* >( rAny
.pReserved
) ) : NULL
);
188 template< class interface_type
>
189 inline Reference
< interface_type
>::Reference( const BaseReference
& rRef
, UnoReference_QueryThrow
)
191 _pInterface
= iquery_throw( rRef
.get() );
194 template< class interface_type
>
195 inline Reference
< interface_type
>::Reference( XInterface
* pInterface
, UnoReference_QueryThrow
)
197 _pInterface
= iquery_throw( pInterface
);
200 template< class interface_type
>
201 inline Reference
< interface_type
>::Reference( const Any
& rAny
, UnoReference_QueryThrow
)
203 _pInterface
= iquery_throw( typelib_TypeClass_INTERFACE
== rAny
.pType
->eTypeClass
204 ? static_cast< XInterface
* >( rAny
.pReserved
) : NULL
);
207 template< class interface_type
>
208 inline Reference
< interface_type
>::Reference( const Reference
< interface_type
> & rRef
, UnoReference_SetThrow
)
210 _pInterface
= castToXInterface( iset_throw( rRef
.get() ) );
213 template< class interface_type
>
214 inline Reference
< interface_type
>::Reference( interface_type
* pInterface
, UnoReference_SetThrow
)
216 _pInterface
= castToXInterface( iset_throw( pInterface
) );
220 template< class interface_type
>
221 inline void Reference
< interface_type
>::clear()
225 XInterface
* const pOld
= _pInterface
;
231 template< class interface_type
>
232 inline bool Reference
< interface_type
>::set(
233 interface_type
* pInterface
)
236 castToXInterface(pInterface
)->acquire();
237 XInterface
* const pOld
= _pInterface
;
238 _pInterface
= castToXInterface(pInterface
);
241 return (NULL
!= pInterface
);
244 template< class interface_type
>
245 inline bool Reference
< interface_type
>::set(
246 interface_type
* pInterface
, __sal_NoAcquire
)
248 XInterface
* const pOld
= _pInterface
;
249 _pInterface
= castToXInterface(pInterface
);
252 return (NULL
!= pInterface
);
255 template< class interface_type
>
256 inline bool Reference
< interface_type
>::set(
257 interface_type
* pInterface
, UnoReference_NoAcquire
)
259 return set( pInterface
, SAL_NO_ACQUIRE
);
263 template< class interface_type
>
264 inline bool Reference
< interface_type
>::set(
265 const Reference
< interface_type
> & rRef
)
267 return set( castFromXInterface( rRef
._pInterface
) );
270 template< class interface_type
>
271 inline bool Reference
< interface_type
>::set(
272 XInterface
* pInterface
, UnoReference_Query
)
274 return set( castFromXInterface(iquery( pInterface
)), SAL_NO_ACQUIRE
);
277 template< class interface_type
>
278 inline bool Reference
< interface_type
>::set(
279 const BaseReference
& rRef
, UnoReference_Query
)
281 return set( castFromXInterface(iquery( rRef
.get() )), SAL_NO_ACQUIRE
);
285 template< class interface_type
>
286 inline bool Reference
< interface_type
>::set(
287 Any
const & rAny
, UnoReference_Query
)
292 rAny
.pType
->eTypeClass
== typelib_TypeClass_INTERFACE
293 ? static_cast< XInterface
* >( rAny
.pReserved
) : NULL
)),
298 template< class interface_type
>
299 inline void Reference
< interface_type
>::set(
300 XInterface
* pInterface
, UnoReference_QueryThrow
)
302 set( castFromXInterface(iquery_throw( pInterface
)), SAL_NO_ACQUIRE
);
305 template< class interface_type
>
306 inline void Reference
< interface_type
>::set(
307 const BaseReference
& rRef
, UnoReference_QueryThrow
)
309 set( castFromXInterface(iquery_throw( rRef
.get() )), SAL_NO_ACQUIRE
);
313 template< class interface_type
>
314 inline void Reference
< interface_type
>::set(
315 Any
const & rAny
, UnoReference_QueryThrow
)
317 set( castFromXInterface(
319 rAny
.pType
->eTypeClass
== typelib_TypeClass_INTERFACE
320 ? static_cast< XInterface
* >( rAny
.pReserved
) : NULL
)),
324 template< class interface_type
>
325 inline void Reference
< interface_type
>::set(
326 interface_type
* pInterface
, UnoReference_SetThrow
)
328 set( iset_throw( pInterface
), SAL_NO_ACQUIRE
);
331 template< class interface_type
>
332 inline void Reference
< interface_type
>::set(
333 const Reference
< interface_type
> & rRef
, UnoReference_SetThrow
)
335 set( rRef
.get(), UNO_SET_THROW
);
339 template< class interface_type
>
340 inline Reference
< interface_type
> & Reference
< interface_type
>::operator = (
341 interface_type
* pInterface
)
347 template< class interface_type
>
348 inline Reference
< interface_type
> & Reference
< interface_type
>::operator = (
349 const Reference
< interface_type
> & rRef
)
351 set( castFromXInterface( rRef
._pInterface
) );
355 #if defined LIBO_INTERNAL_ONLY
356 template< class interface_type
>
357 inline Reference
< interface_type
> & Reference
< interface_type
>::operator = (
358 Reference
< interface_type
> && rRef
) noexcept
361 _pInterface
->release();
362 _pInterface
= rRef
._pInterface
;
363 rRef
._pInterface
= nullptr;
368 template< class interface_type
>
369 inline Reference
< interface_type
> Reference
< interface_type
>::query(
370 const BaseReference
& rRef
)
372 return Reference
< interface_type
>(
373 castFromXInterface(iquery( rRef
.get() )), SAL_NO_ACQUIRE
);
376 template< class interface_type
>
377 inline Reference
< interface_type
> Reference
< interface_type
>::query(
378 XInterface
* pInterface
)
380 return Reference
< interface_type
>(
381 castFromXInterface(iquery( pInterface
)), SAL_NO_ACQUIRE
);
385 inline bool BaseReference::operator == ( XInterface
* pInterface
) const
387 if (_pInterface
== pInterface
)
391 // only the query to XInterface must return the same pointer if they belong to same objects
392 Reference
< XInterface
> x1( _pInterface
, UNO_QUERY
);
393 Reference
< XInterface
> x2( pInterface
, UNO_QUERY
);
394 return (x1
._pInterface
== x2
._pInterface
);
396 catch (RuntimeException
&)
403 inline bool BaseReference::operator < (
404 const BaseReference
& rRef
) const
406 if (_pInterface
== rRef
._pInterface
)
410 // only the query to XInterface must return the same pointer:
411 Reference
< XInterface
> x1( _pInterface
, UNO_QUERY
);
412 Reference
< XInterface
> x2( rRef
, UNO_QUERY
);
413 return (x1
._pInterface
< x2
._pInterface
);
415 catch (RuntimeException
&)
422 inline bool BaseReference::operator != ( XInterface
* pInterface
) const
424 return (! operator == ( pInterface
));
427 inline bool BaseReference::operator == ( const BaseReference
& rRef
) const
429 return operator == ( rRef
._pInterface
);
432 inline bool BaseReference::operator != ( const BaseReference
& rRef
) const
434 return (! operator == ( rRef
._pInterface
));
437 #if defined LIBO_INTERNAL_ONLY
439 Support for BaseReference in std::ostream (and thus in CPPUNIT_ASSERT or
440 SAL_INFO macros, for example).
442 @since LibreOffice 5.4
444 template<typename charT
, typename traits
> std::basic_ostream
<charT
, traits
> &
446 std::basic_ostream
<charT
, traits
> & stream
, BaseReference
const & ref
)
447 { return stream
<< ref
.get(); }
455 #if defined LIBO_INTERNAL_ONLY
460 Make css::uno::Reference hashable by default for use in STL containers.
462 @since LibreOffice 6.3
465 struct hash
<::css::uno::Reference
<T
>>
467 std::size_t operator()(::css::uno::Reference
<T
> const & s
) const
468 { return size_t(s
.get()); }
477 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */