bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / com / sun / star / uno / Reference.hxx
blobc6869d922d7ef64e3984acdcd0f88b4dec2294a5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
24 #include <cstddef>
25 #include <ostream>
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 )
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();
40 namespace com
42 namespace sun
44 namespace star
46 namespace uno
50 inline XInterface * BaseReference::iquery(
51 XInterface * pInterface, const Type & rType )
53 if (pInterface)
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;
60 return pRet;
63 return 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 );
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 ) );
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 )
96 if (pInterface)
98 castToXInterface(pInterface)->acquire();
99 return pInterface;
101 throw RuntimeException(
102 ::rtl::OUString( cppu_unsatisfied_iset_msg( interface_type::static_type().getTypeLibType() ), SAL_NO_ACQUIRE ),
103 NULL );
106 template< class interface_type >
107 inline Reference< interface_type >::~Reference() COVERITY_NOEXCEPT_FALSE
109 if (_pInterface)
110 _pInterface->release();
113 template< class interface_type >
114 inline Reference< interface_type >::Reference()
116 _pInterface = NULL;
119 template< class interface_type >
120 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef )
122 _pInterface = rRef._pInterface;
123 if (_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 )
131 _pInterface = rRef._pInterface;
132 rRef._pInterface = nullptr;
134 #endif
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 )
141 interface_type * p = rRef.get();
142 _pInterface = p;
143 if (_pInterface)
144 _pInterface->acquire();
147 template< class interface_type >
148 inline Reference< interface_type >::Reference( interface_type * pInterface )
150 _pInterface = castToXInterface(pInterface);
151 if (_pInterface)
152 _pInterface->acquire();
155 template< class interface_type >
156 inline Reference< interface_type >::Reference( interface_type * pInterface, __sal_NoAcquire )
158 _pInterface = castToXInterface(pInterface);
161 template< class interface_type >
162 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_NoAcquire )
164 _pInterface = castToXInterface(pInterface);
167 template< class interface_type >
168 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_Query )
170 _pInterface = iquery( rRef.get() );
173 template< class interface_type >
174 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_Query )
176 _pInterface = iquery( pInterface );
179 template< class interface_type >
180 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_Query )
182 _pInterface = (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
183 ? iquery( static_cast< XInterface * >( rAny.pReserved ) ) : NULL);
186 template< class interface_type >
187 inline Reference< interface_type >::Reference( const BaseReference & rRef, UnoReference_QueryThrow )
189 _pInterface = iquery_throw( rRef.get() );
192 template< class interface_type >
193 inline Reference< interface_type >::Reference( XInterface * pInterface, UnoReference_QueryThrow )
195 _pInterface = iquery_throw( pInterface );
198 template< class interface_type >
199 inline Reference< interface_type >::Reference( const Any & rAny, UnoReference_QueryThrow )
201 _pInterface = iquery_throw( typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass
202 ? static_cast< XInterface * >( rAny.pReserved ) : NULL );
205 template< class interface_type >
206 inline Reference< interface_type >::Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow )
208 _pInterface = castToXInterface( iset_throw( rRef.get() ) );
211 template< class interface_type >
212 inline Reference< interface_type >::Reference( interface_type * pInterface, UnoReference_SetThrow )
214 _pInterface = castToXInterface( iset_throw( pInterface ) );
218 template< class interface_type >
219 inline void Reference< interface_type >::clear()
221 if (_pInterface)
223 XInterface * const pOld = _pInterface;
224 _pInterface = NULL;
225 pOld->release();
229 template< class interface_type >
230 inline bool Reference< interface_type >::set(
231 interface_type * pInterface )
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);
242 template< class interface_type >
243 inline bool Reference< interface_type >::set(
244 interface_type * pInterface, __sal_NoAcquire )
246 XInterface * const pOld = _pInterface;
247 _pInterface = castToXInterface(pInterface);
248 if (pOld)
249 pOld->release();
250 return (NULL != pInterface);
253 template< class interface_type >
254 inline bool Reference< interface_type >::set(
255 interface_type * pInterface, UnoReference_NoAcquire )
257 return set( pInterface, SAL_NO_ACQUIRE );
261 template< class interface_type >
262 inline bool Reference< interface_type >::set(
263 const Reference< interface_type > & rRef )
265 return set( castFromXInterface( rRef._pInterface ) );
268 template< class interface_type >
269 inline bool Reference< interface_type >::set(
270 XInterface * pInterface, UnoReference_Query )
272 return set( castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
275 template< class interface_type >
276 inline bool Reference< interface_type >::set(
277 const BaseReference & rRef, UnoReference_Query )
279 return set( castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
283 template< class interface_type >
284 inline bool Reference< interface_type >::set(
285 Any const & rAny, UnoReference_Query )
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 );
296 template< class interface_type >
297 inline void Reference< interface_type >::set(
298 XInterface * pInterface, UnoReference_QueryThrow )
300 set( castFromXInterface(iquery_throw( pInterface )), SAL_NO_ACQUIRE );
303 template< class interface_type >
304 inline void Reference< interface_type >::set(
305 const BaseReference & rRef, UnoReference_QueryThrow )
307 set( castFromXInterface(iquery_throw( rRef.get() )), SAL_NO_ACQUIRE );
311 template< class interface_type >
312 inline void Reference< interface_type >::set(
313 Any const & rAny, UnoReference_QueryThrow )
315 set( castFromXInterface(
316 iquery_throw(
317 rAny.pType->eTypeClass == typelib_TypeClass_INTERFACE
318 ? static_cast< XInterface * >( rAny.pReserved ) : NULL )),
319 SAL_NO_ACQUIRE );
322 template< class interface_type >
323 inline void Reference< interface_type >::set(
324 interface_type * pInterface, UnoReference_SetThrow )
326 set( iset_throw( pInterface ), SAL_NO_ACQUIRE );
329 template< class interface_type >
330 inline void Reference< interface_type >::set(
331 const Reference< interface_type > & rRef, UnoReference_SetThrow )
333 set( rRef.get(), UNO_SET_THROW );
337 template< class interface_type >
338 inline Reference< interface_type > & Reference< interface_type >::operator = (
339 interface_type * pInterface )
341 set( pInterface );
342 return *this;
345 template< class interface_type >
346 inline Reference< interface_type > & Reference< interface_type >::operator = (
347 const Reference< interface_type > & rRef )
349 set( castFromXInterface( rRef._pInterface ) );
350 return *this;
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 )
358 if (_pInterface)
359 _pInterface->release();
360 _pInterface = rRef._pInterface;
361 rRef._pInterface = nullptr;
362 return *this;
364 #endif
366 template< class interface_type >
367 inline Reference< interface_type > Reference< interface_type >::query(
368 const BaseReference & rRef )
370 return Reference< interface_type >(
371 castFromXInterface(iquery( rRef.get() )), SAL_NO_ACQUIRE );
374 template< class interface_type >
375 inline Reference< interface_type > Reference< interface_type >::query(
376 XInterface * pInterface )
378 return Reference< interface_type >(
379 castFromXInterface(iquery( pInterface )), SAL_NO_ACQUIRE );
383 inline bool BaseReference::operator == ( XInterface * pInterface ) const
385 if (_pInterface == pInterface)
386 return true;
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);
394 catch (RuntimeException &)
396 return false;
401 inline bool BaseReference::operator < (
402 const BaseReference & rRef ) const
404 if (_pInterface == rRef._pInterface)
405 return false;
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);
413 catch (RuntimeException &)
415 return false;
420 inline bool BaseReference::operator != ( XInterface * pInterface ) const
422 return (! operator == ( pInterface ));
425 inline bool BaseReference::operator == ( const BaseReference & rRef ) const
427 return operator == ( rRef._pInterface );
430 inline bool BaseReference::operator != ( const BaseReference & rRef ) const
432 return (! operator == ( rRef._pInterface ));
435 #if defined LIBO_INTERNAL_ONLY
437 Support for BaseReference in std::ostream (and thus in CPPUNIT_ASSERT or
438 SAL_INFO macros, for example).
440 @since LibreOffice 5.4
442 template<typename charT, typename traits> std::basic_ostream<charT, traits> &
443 operator <<(
444 std::basic_ostream<charT, traits> & stream, BaseReference const & ref)
445 { return stream << ref.get(); }
446 #endif
453 #if defined LIBO_INTERNAL_ONLY
454 namespace std
458 Make css::uno::Reference hashable by default for use in STL containers.
460 @since LibreOffice 6.3
462 template<typename T>
463 struct hash<::css::uno::Reference<T>>
465 std::size_t operator()(::css::uno::Reference<T> const & s) const
466 { return size_t(s.get()); }
471 #endif
473 #endif
475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */