update credits
[LibreOffice.git] / cppuhelper / source / implbase_ex.cxx
blobb5818074821b2b2f27fddb70d765765603a591f2
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 .
20 #include <sal/alloca.h>
22 #include <string.h>
23 #include <osl/diagnose.h>
24 #include <rtl/byteseq.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <rtl/uuid.h>
27 #include <cppuhelper/compbase_ex.hxx>
29 #include "com/sun/star/uno/RuntimeException.hpp"
31 using namespace ::cppu;
32 using namespace ::osl;
33 using namespace ::rtl;
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
37 namespace cppu
40 /** Shared mutex for implementation helper initialization.
41 Not for public use.
43 ::osl::Mutex & SAL_CALL getImplHelperInitMutex(void) SAL_THROW(());
45 //--------------------------------------------------------------------------------------------------
46 static inline void checkInterface( Type const & rType )
47 SAL_THROW( (RuntimeException) )
49 if (TypeClass_INTERFACE != rType.getTypeClass())
51 OUStringBuffer buf( 64 );
52 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("querying for interface \"") );
53 buf.append( rType.getTypeName() );
54 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\": no interface type!") );
55 OUString msg( buf.makeStringAndClear() );
56 #if OSL_DEBUG_LEVEL > 0
57 OString str( OUStringToOString( msg, RTL_TEXTENCODING_ASCII_US ) );
58 OSL_FAIL( str.getStr() );
59 #endif
60 throw RuntimeException( msg, Reference< XInterface >() );
63 //--------------------------------------------------------------------------------------------------
64 static inline bool isXInterface( rtl_uString * pStr ) SAL_THROW(())
66 return (*((OUString const *)&pStr) == "com.sun.star.uno.XInterface");
68 //--------------------------------------------------------------------------------------------------
69 static inline void * makeInterface( sal_IntPtr nOffset, void * that ) SAL_THROW(())
71 return (((char *)that) + nOffset);
73 //--------------------------------------------------------------------------------------------------
74 static inline bool __td_equals(
75 typelib_TypeDescriptionReference const * pTDR1,
76 typelib_TypeDescriptionReference const * pTDR2 )
77 SAL_THROW(())
79 return ((pTDR1 == pTDR2) ||
80 ((OUString const *)&pTDR1->pTypeName)->equals( *(OUString const *)&pTDR2->pTypeName ) != sal_False);
82 //--------------------------------------------------------------------------------------------------
83 static inline type_entry * __getTypeEntries( class_data * cd )
84 SAL_THROW( (RuntimeException) )
86 type_entry * pEntries = cd->m_typeEntries;
87 if (! cd->m_storedTypeRefs) // not inited?
89 MutexGuard guard( getImplHelperInitMutex() );
90 if (! cd->m_storedTypeRefs) // not inited?
92 // get all types
93 for ( sal_Int32 n = cd->m_nTypes; n--; )
95 type_entry * pEntry = &pEntries[ n ];
96 Type const & rType = (*pEntry->m_type.getCppuType)( 0 );
97 OSL_ENSURE( rType.getTypeClass() == TypeClass_INTERFACE, "### wrong helper init: expected interface!" );
98 OSL_ENSURE( ! isXInterface( rType.getTypeLibType()->pTypeName ), "### want to implement XInterface: template argument is XInterface?!?!?!" );
99 if (rType.getTypeClass() != TypeClass_INTERFACE)
101 OUStringBuffer buf( 48 );
102 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("type \"") );
103 buf.append( rType.getTypeName() );
104 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" is no interface type!") );
105 OUString msg( buf.makeStringAndClear() );
106 #if OSL_DEBUG_LEVEL > 0
107 OString str( OUStringToOString( msg, RTL_TEXTENCODING_ASCII_US ) );
108 OSL_FAIL( str.getStr() );
109 #endif
110 throw RuntimeException( msg, Reference< XInterface >() );
112 // ref is statically held by getCppuType()
113 pEntry->m_type.typeRef = rType.getTypeLibType();
115 cd->m_storedTypeRefs = sal_True;
118 return pEntries;
120 //--------------------------------------------------------------------------------------------------
121 static inline void __fillTypes( Type * types, class_data * cd )
122 SAL_THROW( (RuntimeException) )
124 type_entry * pEntries = __getTypeEntries( cd );
125 for ( sal_Int32 n = cd->m_nTypes; n--; )
127 types[ n ] = pEntries[ n ].m_type.typeRef;
130 //--------------------------------------------------------------------------------------------------
131 namespace {
133 bool recursivelyFindType(
134 typelib_TypeDescriptionReference const * demandedType,
135 typelib_InterfaceTypeDescription const * type, sal_IntPtr * offset)
137 // This code assumes that the vtables of a multiple-inheritance class (the
138 // offset amount by which to adjust the this pointer) follow one another in
139 // the object layout, and that they contain slots for the inherited classes
140 // in a specifc order. In theory, that need not hold for any given
141 // platform; in practice, it seems to work well on all supported platforms:
142 next:
143 for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
144 if (i > 0) {
145 *offset += sizeof (void *);
147 typelib_InterfaceTypeDescription const * base = type->ppBaseTypes[i];
148 // ignore XInterface:
149 if (base->nBaseTypes > 0) {
150 if (__td_equals(
151 reinterpret_cast<
152 typelib_TypeDescriptionReference const * >(base),
153 demandedType))
155 return true;
157 // Profiling showed that it is important to speed up the common case
158 // of only one base:
159 if (type->nBaseTypes == 1) {
160 type = base;
161 goto next;
163 if (recursivelyFindType(demandedType, base, offset)) {
164 return true;
168 return false;
173 static inline void * __queryDeepNoXInterface(
174 typelib_TypeDescriptionReference * pDemandedTDR, class_data * cd, void * that )
175 SAL_THROW( (RuntimeException) )
177 type_entry * pEntries = __getTypeEntries( cd );
178 sal_Int32 nTypes = cd->m_nTypes;
179 sal_Int32 n;
181 // try top interfaces without getting td
182 for ( n = 0; n < nTypes; ++n )
184 if (__td_equals( pEntries[ n ].m_type.typeRef, pDemandedTDR ))
186 return makeInterface( pEntries[ n ].m_offset, that );
189 // query deep getting td
190 for ( n = 0; n < nTypes; ++n )
192 typelib_TypeDescription * pTD = 0;
193 TYPELIB_DANGER_GET( &pTD, pEntries[ n ].m_type.typeRef );
194 if (pTD)
196 // exclude top (already tested) and bottom (XInterface) interface
197 OSL_ENSURE(
198 reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD)->
199 nBaseTypes > 0,
200 "### want to implement XInterface:"
201 " template argument is XInterface?!?!?!" );
202 sal_IntPtr offset = pEntries[n].m_offset;
203 bool found = recursivelyFindType(
204 pDemandedTDR,
205 reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD),
206 &offset);
207 TYPELIB_DANGER_RELEASE( pTD );
208 if (found) {
209 return makeInterface( offset, that );
212 else
214 OUStringBuffer buf( 64 );
215 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("cannot get type description for type \"") );
216 buf.append( pEntries[ n ].m_type.typeRef->pTypeName );
217 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
218 OUString msg( buf.makeStringAndClear() );
219 #if OSL_DEBUG_LEVEL > 0
220 OString str( OUStringToOString( msg, RTL_TEXTENCODING_ASCII_US ) );
221 OSL_FAIL( str.getStr() );
222 #endif
223 throw RuntimeException( msg, Reference< XInterface >() );
226 return 0;
229 // ImplHelper
230 //==================================================================================================
231 Any SAL_CALL ImplHelper_query(
232 Type const & rType, class_data * cd, void * that )
233 SAL_THROW( (RuntimeException) )
235 checkInterface( rType );
236 typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
238 void * p;
239 // shortcut for XInterface
240 if (isXInterface( pTDR->pTypeName ))
242 // take first one
243 p = makeInterface( cd->m_typeEntries[ 0 ].m_offset, that );
245 else
247 p = __queryDeepNoXInterface( pTDR, cd, that );
248 if (! p)
250 return Any();
253 return Any( &p, pTDR );
255 //==================================================================================================
256 Any SAL_CALL ImplHelper_queryNoXInterface(
257 Type const & rType, class_data * cd, void * that )
258 SAL_THROW( (RuntimeException) )
260 checkInterface( rType );
261 typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
263 void * p = __queryDeepNoXInterface( pTDR, cd, that );
264 if (p)
266 return Any( &p, pTDR );
268 else
270 return Any();
273 //==================================================================================================
274 Sequence< sal_Int8 > SAL_CALL ImplHelper_getImplementationId( class_data * cd )
275 SAL_THROW( (RuntimeException) )
277 if (! cd->m_createdId)
279 sal_uInt8 * id = (sal_uInt8 *)alloca( 16 );
280 ::rtl_createUuid( (sal_uInt8 *)id, 0, sal_True );
282 MutexGuard guard( getImplHelperInitMutex() );
283 if (! cd->m_createdId)
285 memcpy( cd->m_id, id, 16 );
286 cd->m_createdId = sal_True;
290 sal_Sequence * seq = 0;
291 ::rtl_byte_sequence_constructFromArray( &seq, cd->m_id, 16 );
292 return Sequence< sal_Int8 >( seq, SAL_NO_ACQUIRE );
294 //==================================================================================================
295 Sequence< Type > SAL_CALL ImplHelper_getTypes(
296 class_data * cd )
297 SAL_THROW( (RuntimeException) )
299 Sequence< Type > types( cd->m_nTypes );
300 Type * pTypes = types.getArray();
301 __fillTypes( pTypes, cd );
302 return types;
304 //==================================================================================================
305 Sequence< Type > SAL_CALL ImplInhHelper_getTypes(
306 class_data * cd, Sequence< Type > const & rAddTypes )
307 SAL_THROW( (RuntimeException) )
309 sal_Int32 nImplTypes = cd->m_nTypes;
310 sal_Int32 nAddTypes = rAddTypes.getLength();
311 Sequence< Type > types( nImplTypes + nAddTypes );
312 Type * pTypes = types.getArray();
313 __fillTypes( pTypes, cd );
314 // append base types
315 Type const * pAddTypes = rAddTypes.getConstArray();
316 while (nAddTypes--)
318 pTypes[ nImplTypes + nAddTypes ] = pAddTypes[ nAddTypes ];
320 return types;
323 // WeakImplHelper
324 //==================================================================================================
325 Any SAL_CALL WeakImplHelper_query(
326 Type const & rType, class_data * cd, void * that, OWeakObject * pBase )
327 SAL_THROW( (RuntimeException) )
329 checkInterface( rType );
330 typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
332 // shortcut XInterface to OWeakObject
333 if (! isXInterface( pTDR->pTypeName ))
335 void * p = __queryDeepNoXInterface( pTDR, cd, that );
336 if (p)
338 return Any( &p, pTDR );
341 return pBase->OWeakObject::queryInterface( rType );
343 //==================================================================================================
344 Sequence< Type > SAL_CALL WeakImplHelper_getTypes(
345 class_data * cd )
346 SAL_THROW( (RuntimeException) )
348 sal_Int32 nTypes = cd->m_nTypes;
349 Sequence< Type > types( nTypes +1 );
350 Type * pTypes = types.getArray();
351 __fillTypes( pTypes, cd );
352 pTypes[ nTypes ] = ::getCppuType( (Reference< XWeak > const *)0 );
353 return types;
356 // WeakAggImplHelper
357 //==================================================================================================
358 Any SAL_CALL WeakAggImplHelper_queryAgg(
359 Type const & rType, class_data * cd, void * that, OWeakAggObject * pBase )
360 SAL_THROW( (RuntimeException) )
362 checkInterface( rType );
363 typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
365 // shortcut XInterface to OWeakAggObject
366 if (! isXInterface( pTDR->pTypeName ))
368 void * p = __queryDeepNoXInterface( pTDR, cd, that );
369 if (p)
371 return Any( &p, pTDR );
374 return pBase->OWeakAggObject::queryAggregation( rType );
376 //==================================================================================================
377 Sequence< Type > SAL_CALL WeakAggImplHelper_getTypes(
378 class_data * cd )
379 SAL_THROW( (RuntimeException) )
381 sal_Int32 nTypes = cd->m_nTypes;
382 Sequence< Type > types( nTypes +2 );
383 Type * pTypes = types.getArray();
384 __fillTypes( pTypes, cd );
385 pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
386 pTypes[ nTypes ] = ::getCppuType( (const Reference< XAggregation > *)0 );
387 return types;
390 // WeakComponentImplHelper
391 //==================================================================================================
392 Any SAL_CALL WeakComponentImplHelper_query(
393 Type const & rType, class_data * cd, void * that, WeakComponentImplHelperBase * pBase )
394 SAL_THROW( (RuntimeException) )
396 checkInterface( rType );
397 typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
399 // shortcut XInterface to WeakComponentImplHelperBase
400 if (! isXInterface( pTDR->pTypeName ))
402 void * p = __queryDeepNoXInterface( pTDR, cd, that );
403 if (p)
405 return Any( &p, pTDR );
408 return pBase->WeakComponentImplHelperBase::queryInterface( rType );
410 //==================================================================================================
411 Sequence< Type > SAL_CALL WeakComponentImplHelper_getTypes(
412 class_data * cd )
413 SAL_THROW( (RuntimeException) )
415 sal_Int32 nTypes = cd->m_nTypes;
416 Sequence< Type > types( nTypes +2 );
417 Type * pTypes = types.getArray();
418 __fillTypes( pTypes, cd );
419 pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
420 pTypes[ nTypes ] = ::getCppuType( (Reference< lang::XComponent > const *)0 );
421 return types;
424 // WeakAggComponentImplHelper
425 //==================================================================================================
426 Any SAL_CALL WeakAggComponentImplHelper_queryAgg(
427 Type const & rType, class_data * cd, void * that, WeakAggComponentImplHelperBase * pBase )
428 SAL_THROW( (RuntimeException) )
430 checkInterface( rType );
431 typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
433 // shortcut XInterface to WeakAggComponentImplHelperBase
434 if (! isXInterface( pTDR->pTypeName ))
436 void * p = __queryDeepNoXInterface( pTDR, cd, that );
437 if (p)
439 return Any( &p, pTDR );
442 return pBase->WeakAggComponentImplHelperBase::queryAggregation( rType );
444 //==================================================================================================
445 Sequence< Type > SAL_CALL WeakAggComponentImplHelper_getTypes(
446 class_data * cd )
447 SAL_THROW( (RuntimeException) )
449 sal_Int32 nTypes = cd->m_nTypes;
450 Sequence< Type > types( nTypes +3 );
451 Type * pTypes = types.getArray();
452 __fillTypes( pTypes, cd );
453 pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
454 pTypes[ nTypes++ ] = ::getCppuType( (const Reference< XAggregation > *)0 );
455 pTypes[ nTypes ] = ::getCppuType( (const Reference< lang::XComponent > *)0 );
456 return types;
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */