Line data Source code
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 : #include <sal/alloca.h>
21 :
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>
28 :
29 : #include "com/sun/star/uno/RuntimeException.hpp"
30 :
31 : using namespace ::cppu;
32 : using namespace ::osl;
33 : using namespace ::com::sun::star;
34 : using namespace ::com::sun::star::uno;
35 :
36 : using rtl::OString;
37 : using rtl::OUString;
38 : using rtl::OUStringBuffer;
39 :
40 : namespace cppu
41 : {
42 :
43 : /** Shared mutex for implementation helper initialization.
44 : Not for public use.
45 : */
46 : ::osl::Mutex & SAL_CALL getImplHelperInitMutex(void) SAL_THROW(());
47 :
48 :
49 26787602 : static inline void checkInterface( Type const & rType )
50 : SAL_THROW( (RuntimeException) )
51 : {
52 26787602 : if (TypeClass_INTERFACE != rType.getTypeClass())
53 : {
54 0 : OUString msg( "querying for interface \"" + rType.getTypeName() + "\": no interface type!" );
55 : SAL_WARN( "cppuhelper", msg );
56 0 : throw RuntimeException( msg, Reference< XInterface >() );
57 : }
58 26787611 : }
59 :
60 19162858 : static inline bool isXInterface( rtl_uString * pStr ) SAL_THROW(())
61 : {
62 19162858 : return (*((OUString const *)&pStr) == "com.sun.star.uno.XInterface");
63 : }
64 :
65 13789511 : static inline void * makeInterface( sal_IntPtr nOffset, void * that ) SAL_THROW(())
66 : {
67 13789511 : return (((char *)that) + nOffset);
68 : }
69 :
70 147862410 : static inline bool __td_equals(
71 : typelib_TypeDescriptionReference const * pTDR1,
72 : typelib_TypeDescriptionReference const * pTDR2 )
73 : SAL_THROW(())
74 : {
75 281991353 : return ((pTDR1 == pTDR2) ||
76 281991353 : ((OUString const *)&pTDR1->pTypeName)->equals( *(OUString const *)&pTDR2->pTypeName ));
77 : }
78 :
79 25264766 : static inline type_entry * __getTypeEntries( class_data * cd )
80 : SAL_THROW( (RuntimeException) )
81 : {
82 25264766 : type_entry * pEntries = cd->m_typeEntries;
83 25264766 : if (! cd->m_storedTypeRefs) // not inited?
84 : {
85 22443 : MutexGuard guard( getImplHelperInitMutex() );
86 22443 : if (! cd->m_storedTypeRefs) // not inited?
87 : {
88 : // get all types
89 132933 : for ( sal_Int32 n = cd->m_nTypes; n--; )
90 : {
91 88047 : type_entry * pEntry = &pEntries[ n ];
92 88047 : Type const & rType = (*pEntry->m_type.getCppuType)( 0 );
93 : OSL_ENSURE( rType.getTypeClass() == TypeClass_INTERFACE, "### wrong helper init: expected interface!" );
94 : OSL_ENSURE( ! isXInterface( rType.getTypeLibType()->pTypeName ), "### want to implement XInterface: template argument is XInterface?!?!?!" );
95 88047 : if (rType.getTypeClass() != TypeClass_INTERFACE)
96 : {
97 0 : OUString msg( "type \"" + rType.getTypeName() + "\" is no interface type!" );
98 : SAL_WARN( "cppuhelper", msg );
99 0 : throw RuntimeException( msg, Reference< XInterface >() );
100 : }
101 : // ref is statically held by getCppuType()
102 88047 : pEntry->m_type.typeRef = rType.getTypeLibType();
103 : }
104 22443 : cd->m_storedTypeRefs = sal_True;
105 22443 : }
106 : }
107 25264766 : return pEntries;
108 : }
109 :
110 17207 : static inline void __fillTypes( Type * types, class_data * cd )
111 : SAL_THROW( (RuntimeException) )
112 : {
113 17207 : type_entry * pEntries = __getTypeEntries( cd );
114 301258 : for ( sal_Int32 n = cd->m_nTypes; n--; )
115 : {
116 266844 : types[ n ] = pEntries[ n ].m_type.typeRef;
117 : }
118 17207 : }
119 :
120 : namespace {
121 :
122 87378525 : bool recursivelyFindType(
123 : typelib_TypeDescriptionReference const * demandedType,
124 : typelib_InterfaceTypeDescription const * type, sal_IntPtr * offset)
125 : {
126 : // This code assumes that the vtables of a multiple-inheritance class (the
127 : // offset amount by which to adjust the this pointer) follow one another in
128 : // the object layout, and that they contain slots for the inherited classes
129 : // in a specific order. In theory, that need not hold for any given
130 : // platform; in practice, it seems to work well on all supported platforms:
131 : next:
132 158867208 : for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
133 88657646 : if (i > 0) {
134 1279122 : *offset += sizeof (void *);
135 : }
136 88657646 : typelib_InterfaceTypeDescription const * base = type->ppBaseTypes[i];
137 : // ignore XInterface:
138 88657646 : if (base->nBaseTypes > 0) {
139 18830510 : if (__td_equals(
140 : reinterpret_cast<
141 : typelib_TypeDescriptionReference const * >(base),
142 : demandedType))
143 : {
144 2427667 : return true;
145 : }
146 : // Profiling showed that it is important to speed up the common case
147 : // of only one base:
148 16402849 : if (type->nBaseTypes == 1) {
149 14674770 : type = base;
150 14674770 : goto next;
151 : }
152 1728079 : if (recursivelyFindType(demandedType, base, offset)) {
153 66532 : return true;
154 : }
155 : }
156 : }
157 70209562 : return false;
158 : }
159 :
160 : }
161 :
162 25247559 : static inline void * __queryDeepNoXInterface(
163 : typelib_TypeDescriptionReference * pDemandedTDR, class_data * cd, void * that )
164 : SAL_THROW( (RuntimeException) )
165 : {
166 25247559 : type_entry * pEntries = __getTypeEntries( cd );
167 25247454 : sal_Int32 nTypes = cd->m_nTypes;
168 : sal_Int32 n;
169 :
170 : // try top interfaces without getting td
171 142973585 : for ( n = 0; n < nTypes; ++n )
172 : {
173 129031812 : if (__td_equals( pEntries[ n ].m_type.typeRef, pDemandedTDR ))
174 : {
175 11305792 : return makeInterface( pEntries[ n ].m_offset, that );
176 : }
177 : }
178 : // query deep getting td
179 82489792 : for ( n = 0; n < nTypes; ++n )
180 : {
181 70975685 : typelib_TypeDescription * pTD = 0;
182 70975685 : TYPELIB_DANGER_GET( &pTD, pEntries[ n ].m_type.typeRef );
183 70975687 : if (pTD)
184 : {
185 : // exclude top (already tested) and bottom (XInterface) interface
186 : OSL_ENSURE(
187 : reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD)->
188 : nBaseTypes > 0,
189 : "### want to implement XInterface:"
190 : " template argument is XInterface?!?!?!" );
191 70975687 : sal_IntPtr offset = pEntries[n].m_offset;
192 : bool found = recursivelyFindType(
193 : pDemandedTDR,
194 : reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD),
195 70975687 : &offset);
196 70975683 : TYPELIB_DANGER_RELEASE( pTD );
197 70975686 : if (found) {
198 2427667 : return makeInterface( offset, that );
199 : }
200 : }
201 : else
202 : {
203 0 : OUString msg( "cannot get type description for type \"" + OUString(pEntries[ n ].m_type.typeRef->pTypeName) + "\"!" );
204 : SAL_WARN( "cppuhelper", msg );
205 0 : throw RuntimeException( msg, Reference< XInterface >() );
206 : }
207 : }
208 11514107 : return 0;
209 : }
210 :
211 : // ImplHelper
212 :
213 1047105 : Any SAL_CALL ImplHelper_query(
214 : Type const & rType, class_data * cd, void * that )
215 : SAL_THROW( (RuntimeException) )
216 : {
217 1047105 : checkInterface( rType );
218 1047105 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
219 :
220 : void * p;
221 : // shortcut for XInterface
222 1047105 : if (isXInterface( pTDR->pTypeName ))
223 : {
224 : // take first one
225 56053 : p = makeInterface( cd->m_typeEntries[ 0 ].m_offset, that );
226 : }
227 : else
228 : {
229 991052 : p = __queryDeepNoXInterface( pTDR, cd, that );
230 991052 : if (! p)
231 : {
232 818458 : return Any();
233 : }
234 : }
235 228647 : return Any( &p, pTDR );
236 : }
237 :
238 7624757 : Any SAL_CALL ImplHelper_queryNoXInterface(
239 : Type const & rType, class_data * cd, void * that )
240 : SAL_THROW( (RuntimeException) )
241 : {
242 7624757 : checkInterface( rType );
243 7624757 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
244 :
245 7624757 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
246 7624757 : if (p)
247 : {
248 2697005 : return Any( &p, pTDR );
249 : }
250 : else
251 : {
252 4927752 : return Any();
253 : }
254 : }
255 :
256 6 : css::uno::Sequence<sal_Int8> ImplHelper_getImplementationId(
257 : SAL_UNUSED_PARAMETER class_data *) SAL_THROW((css::uno::RuntimeException))
258 : {
259 6 : return css::uno::Sequence<sal_Int8>();
260 : }
261 :
262 795 : Sequence< Type > SAL_CALL ImplHelper_getTypes(
263 : class_data * cd )
264 : SAL_THROW( (RuntimeException) )
265 : {
266 795 : Sequence< Type > types( cd->m_nTypes );
267 795 : Type * pTypes = types.getArray();
268 795 : __fillTypes( pTypes, cd );
269 795 : return types;
270 : }
271 :
272 1131 : Sequence< Type > SAL_CALL ImplInhHelper_getTypes(
273 : class_data * cd, Sequence< Type > const & rAddTypes )
274 : SAL_THROW( (RuntimeException) )
275 : {
276 1131 : sal_Int32 nImplTypes = cd->m_nTypes;
277 1131 : sal_Int32 nAddTypes = rAddTypes.getLength();
278 1131 : Sequence< Type > types( nImplTypes + nAddTypes );
279 1131 : Type * pTypes = types.getArray();
280 1131 : __fillTypes( pTypes, cd );
281 : // append base types
282 1131 : Type const * pAddTypes = rAddTypes.getConstArray();
283 9341 : while (nAddTypes--)
284 : {
285 7079 : pTypes[ nImplTypes + nAddTypes ] = pAddTypes[ nAddTypes ];
286 : }
287 1131 : return types;
288 : }
289 :
290 : // WeakImplHelper
291 :
292 12768508 : Any SAL_CALL WeakImplHelper_query(
293 : Type const & rType, class_data * cd, void * that, OWeakObject * pBase )
294 : SAL_THROW( (RuntimeException) )
295 : {
296 12768508 : checkInterface( rType );
297 12768513 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
298 :
299 : // shortcut XInterface to OWeakObject
300 12768513 : if (! isXInterface( pTDR->pTypeName ))
301 : {
302 12026288 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
303 12026290 : if (p)
304 : {
305 6842690 : return Any( &p, pTDR );
306 : }
307 : }
308 5925826 : return pBase->OWeakObject::queryInterface( rType );
309 : }
310 :
311 11019 : Sequence< Type > SAL_CALL WeakImplHelper_getTypes(
312 : class_data * cd )
313 : SAL_THROW( (RuntimeException) )
314 : {
315 11019 : sal_Int32 nTypes = cd->m_nTypes;
316 11019 : Sequence< Type > types( nTypes +1 );
317 11019 : Type * pTypes = types.getArray();
318 11019 : __fillTypes( pTypes, cd );
319 11019 : pTypes[ nTypes ] = ::getCppuType( (Reference< XWeak > const *)0 );
320 11019 : return types;
321 : }
322 :
323 : // WeakAggImplHelper
324 :
325 1984530 : Any SAL_CALL WeakAggImplHelper_queryAgg(
326 : Type const & rType, class_data * cd, void * that, OWeakAggObject * pBase )
327 : SAL_THROW( (RuntimeException) )
328 : {
329 1984530 : checkInterface( rType );
330 1984530 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
331 :
332 : // shortcut XInterface to OWeakAggObject
333 1984530 : if (! isXInterface( pTDR->pTypeName ))
334 : {
335 1301838 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
336 1301838 : if (p)
337 : {
338 1028373 : return Any( &p, pTDR );
339 : }
340 : }
341 956157 : return pBase->OWeakAggObject::queryAggregation( rType );
342 : }
343 :
344 3920 : Sequence< Type > SAL_CALL WeakAggImplHelper_getTypes(
345 : class_data * cd )
346 : SAL_THROW( (RuntimeException) )
347 : {
348 3920 : sal_Int32 nTypes = cd->m_nTypes;
349 3920 : Sequence< Type > types( nTypes +2 );
350 3920 : Type * pTypes = types.getArray();
351 3920 : __fillTypes( pTypes, cd );
352 3920 : pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
353 3920 : pTypes[ nTypes ] = ::getCppuType( (const Reference< XAggregation > *)0 );
354 3920 : return types;
355 : }
356 :
357 : // WeakComponentImplHelper
358 :
359 3337255 : Any SAL_CALL WeakComponentImplHelper_query(
360 : Type const & rType, class_data * cd, void * that, WeakComponentImplHelperBase * pBase )
361 : SAL_THROW( (RuntimeException) )
362 : {
363 3337255 : checkInterface( rType );
364 3337256 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
365 :
366 : // shortcut XInterface to WeakComponentImplHelperBase
367 3337256 : if (! isXInterface( pTDR->pTypeName ))
368 : {
369 3287459 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
370 3287459 : if (p)
371 : {
372 2991566 : return Any( &p, pTDR );
373 : }
374 : }
375 345690 : return pBase->WeakComponentImplHelperBase::queryInterface( rType );
376 : }
377 :
378 186 : Sequence< Type > SAL_CALL WeakComponentImplHelper_getTypes(
379 : class_data * cd )
380 : SAL_THROW( (RuntimeException) )
381 : {
382 186 : sal_Int32 nTypes = cd->m_nTypes;
383 186 : Sequence< Type > types( nTypes +2 );
384 186 : Type * pTypes = types.getArray();
385 186 : __fillTypes( pTypes, cd );
386 186 : pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
387 186 : pTypes[ nTypes ] = ::getCppuType( (Reference< lang::XComponent > const *)0 );
388 186 : return types;
389 : }
390 :
391 : // WeakAggComponentImplHelper
392 :
393 25453 : Any SAL_CALL WeakAggComponentImplHelper_queryAgg(
394 : Type const & rType, class_data * cd, void * that, WeakAggComponentImplHelperBase * pBase )
395 : SAL_THROW( (RuntimeException) )
396 : {
397 25453 : checkInterface( rType );
398 25453 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
399 :
400 : // shortcut XInterface to WeakAggComponentImplHelperBase
401 25453 : if (! isXInterface( pTDR->pTypeName ))
402 : {
403 16169 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
404 16169 : if (p)
405 : {
406 1230 : return Any( &p, pTDR );
407 : }
408 : }
409 24223 : return pBase->WeakAggComponentImplHelperBase::queryAggregation( rType );
410 : }
411 :
412 156 : Sequence< Type > SAL_CALL WeakAggComponentImplHelper_getTypes(
413 : class_data * cd )
414 : SAL_THROW( (RuntimeException) )
415 : {
416 156 : sal_Int32 nTypes = cd->m_nTypes;
417 156 : Sequence< Type > types( nTypes +3 );
418 156 : Type * pTypes = types.getArray();
419 156 : __fillTypes( pTypes, cd );
420 156 : pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
421 156 : pTypes[ nTypes++ ] = ::getCppuType( (const Reference< XAggregation > *)0 );
422 156 : pTypes[ nTypes ] = ::getCppuType( (const Reference< lang::XComponent > *)0 );
423 156 : return types;
424 : }
425 :
426 : }
427 :
428 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|