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 90 : static inline void checkInterface( Type const & rType )
50 : SAL_THROW( (RuntimeException) )
51 : {
52 90 : 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 90 : }
59 :
60 71 : static inline bool isXInterface( rtl_uString * pStr ) SAL_THROW(())
61 : {
62 71 : return (*((OUString const *)&pStr) == "com.sun.star.uno.XInterface");
63 : }
64 :
65 60 : static inline void * makeInterface( sal_IntPtr nOffset, void * that ) SAL_THROW(())
66 : {
67 60 : return (((char *)that) + nOffset);
68 : }
69 :
70 253 : static inline bool __td_equals(
71 : typelib_TypeDescriptionReference const * pTDR1,
72 : typelib_TypeDescriptionReference const * pTDR2 )
73 : SAL_THROW(())
74 : {
75 446 : return ((pTDR1 == pTDR2) ||
76 446 : ((OUString const *)&pTDR1->pTypeName)->equals( *(OUString const *)&pTDR2->pTypeName ));
77 : }
78 :
79 88 : static inline type_entry * __getTypeEntries( class_data * cd )
80 : SAL_THROW( (RuntimeException) )
81 : {
82 88 : type_entry * pEntries = cd->m_typeEntries;
83 88 : if (! cd->m_storedTypeRefs) // not inited?
84 : {
85 14 : MutexGuard guard( getImplHelperInitMutex() );
86 14 : if (! cd->m_storedTypeRefs) // not inited?
87 : {
88 : // get all types
89 76 : for ( sal_Int32 n = cd->m_nTypes; n--; )
90 : {
91 48 : type_entry * pEntry = &pEntries[ n ];
92 48 : 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 48 : 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 48 : pEntry->m_type.typeRef = rType.getTypeLibType();
103 : }
104 14 : cd->m_storedTypeRefs = sal_True;
105 14 : }
106 : }
107 88 : return pEntries;
108 : }
109 :
110 0 : static inline void __fillTypes( Type * types, class_data * cd )
111 : SAL_THROW( (RuntimeException) )
112 : {
113 0 : type_entry * pEntries = __getTypeEntries( cd );
114 0 : for ( sal_Int32 n = cd->m_nTypes; n--; )
115 : {
116 0 : types[ n ] = pEntries[ n ].m_type.typeRef;
117 : }
118 0 : }
119 :
120 : namespace {
121 :
122 101 : 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 175 : for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
133 101 : if (i > 0) {
134 0 : *offset += sizeof (void *);
135 : }
136 101 : typelib_InterfaceTypeDescription const * base = type->ppBaseTypes[i];
137 : // ignore XInterface:
138 101 : if (base->nBaseTypes > 0) {
139 27 : if (__td_equals(
140 : reinterpret_cast<
141 : typelib_TypeDescriptionReference const * >(base),
142 : demandedType))
143 : {
144 6 : return true;
145 : }
146 : // Profiling showed that it is important to speed up the common case
147 : // of only one base:
148 21 : if (type->nBaseTypes == 1) {
149 21 : type = base;
150 21 : goto next;
151 : }
152 0 : if (recursivelyFindType(demandedType, base, offset)) {
153 0 : return true;
154 : }
155 : }
156 : }
157 74 : return false;
158 : }
159 :
160 : }
161 :
162 88 : static inline void * __queryDeepNoXInterface(
163 : typelib_TypeDescriptionReference * pDemandedTDR, class_data * cd, void * that )
164 : SAL_THROW( (RuntimeException) )
165 : {
166 88 : type_entry * pEntries = __getTypeEntries( cd );
167 88 : sal_Int32 nTypes = cd->m_nTypes;
168 : sal_Int32 n;
169 :
170 : // try top interfaces without getting td
171 260 : for ( n = 0; n < nTypes; ++n )
172 : {
173 226 : if (__td_equals( pEntries[ n ].m_type.typeRef, pDemandedTDR ))
174 : {
175 54 : return makeInterface( pEntries[ n ].m_offset, that );
176 : }
177 : }
178 : // query deep getting td
179 108 : for ( n = 0; n < nTypes; ++n )
180 : {
181 80 : typelib_TypeDescription * pTD = 0;
182 80 : TYPELIB_DANGER_GET( &pTD, pEntries[ n ].m_type.typeRef );
183 80 : 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 80 : sal_IntPtr offset = pEntries[n].m_offset;
192 : bool found = recursivelyFindType(
193 : pDemandedTDR,
194 : reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD),
195 80 : &offset);
196 80 : TYPELIB_DANGER_RELEASE( pTD );
197 80 : if (found) {
198 6 : 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 28 : return 0;
209 : }
210 :
211 : // ImplHelper
212 :
213 0 : Any SAL_CALL ImplHelper_query(
214 : Type const & rType, class_data * cd, void * that )
215 : SAL_THROW( (RuntimeException) )
216 : {
217 0 : checkInterface( rType );
218 0 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
219 :
220 : void * p;
221 : // shortcut for XInterface
222 0 : if (isXInterface( pTDR->pTypeName ))
223 : {
224 : // take first one
225 0 : p = makeInterface( cd->m_typeEntries[ 0 ].m_offset, that );
226 : }
227 : else
228 : {
229 0 : p = __queryDeepNoXInterface( pTDR, cd, that );
230 0 : if (! p)
231 : {
232 0 : return Any();
233 : }
234 : }
235 0 : return Any( &p, pTDR );
236 : }
237 :
238 19 : Any SAL_CALL ImplHelper_queryNoXInterface(
239 : Type const & rType, class_data * cd, void * that )
240 : SAL_THROW( (RuntimeException) )
241 : {
242 19 : checkInterface( rType );
243 19 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
244 :
245 19 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
246 19 : if (p)
247 : {
248 16 : return Any( &p, pTDR );
249 : }
250 : else
251 : {
252 3 : return Any();
253 : }
254 : }
255 :
256 0 : css::uno::Sequence<sal_Int8> ImplHelper_getImplementationId(
257 : SAL_UNUSED_PARAMETER class_data *) SAL_THROW((css::uno::RuntimeException))
258 : {
259 0 : return css::uno::Sequence<sal_Int8>();
260 : }
261 :
262 0 : Sequence< Type > SAL_CALL ImplHelper_getTypes(
263 : class_data * cd )
264 : SAL_THROW( (RuntimeException) )
265 : {
266 0 : Sequence< Type > types( cd->m_nTypes );
267 0 : Type * pTypes = types.getArray();
268 0 : __fillTypes( pTypes, cd );
269 0 : return types;
270 : }
271 :
272 0 : Sequence< Type > SAL_CALL ImplInhHelper_getTypes(
273 : class_data * cd, Sequence< Type > const & rAddTypes )
274 : SAL_THROW( (RuntimeException) )
275 : {
276 0 : sal_Int32 nImplTypes = cd->m_nTypes;
277 0 : sal_Int32 nAddTypes = rAddTypes.getLength();
278 0 : Sequence< Type > types( nImplTypes + nAddTypes );
279 0 : Type * pTypes = types.getArray();
280 0 : __fillTypes( pTypes, cd );
281 : // append base types
282 0 : Type const * pAddTypes = rAddTypes.getConstArray();
283 0 : while (nAddTypes--)
284 : {
285 0 : pTypes[ nImplTypes + nAddTypes ] = pAddTypes[ nAddTypes ];
286 : }
287 0 : return types;
288 : }
289 :
290 : // WeakImplHelper
291 :
292 32 : Any SAL_CALL WeakImplHelper_query(
293 : Type const & rType, class_data * cd, void * that, OWeakObject * pBase )
294 : SAL_THROW( (RuntimeException) )
295 : {
296 32 : checkInterface( rType );
297 32 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
298 :
299 : // shortcut XInterface to OWeakObject
300 32 : if (! isXInterface( pTDR->pTypeName ))
301 : {
302 32 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
303 32 : if (p)
304 : {
305 12 : return Any( &p, pTDR );
306 : }
307 : }
308 20 : return pBase->OWeakObject::queryInterface( rType );
309 : }
310 :
311 0 : Sequence< Type > SAL_CALL WeakImplHelper_getTypes(
312 : class_data * cd )
313 : SAL_THROW( (RuntimeException) )
314 : {
315 0 : sal_Int32 nTypes = cd->m_nTypes;
316 0 : Sequence< Type > types( nTypes +1 );
317 0 : Type * pTypes = types.getArray();
318 0 : __fillTypes( pTypes, cd );
319 0 : pTypes[ nTypes ] = ::getCppuType( (Reference< XWeak > const *)0 );
320 0 : return types;
321 : }
322 :
323 : // WeakAggImplHelper
324 :
325 0 : Any SAL_CALL WeakAggImplHelper_queryAgg(
326 : Type const & rType, class_data * cd, void * that, OWeakAggObject * pBase )
327 : SAL_THROW( (RuntimeException) )
328 : {
329 0 : checkInterface( rType );
330 0 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
331 :
332 : // shortcut XInterface to OWeakAggObject
333 0 : if (! isXInterface( pTDR->pTypeName ))
334 : {
335 0 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
336 0 : if (p)
337 : {
338 0 : return Any( &p, pTDR );
339 : }
340 : }
341 0 : return pBase->OWeakAggObject::queryAggregation( rType );
342 : }
343 :
344 0 : Sequence< Type > SAL_CALL WeakAggImplHelper_getTypes(
345 : class_data * cd )
346 : SAL_THROW( (RuntimeException) )
347 : {
348 0 : sal_Int32 nTypes = cd->m_nTypes;
349 0 : Sequence< Type > types( nTypes +2 );
350 0 : Type * pTypes = types.getArray();
351 0 : __fillTypes( pTypes, cd );
352 0 : pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
353 0 : pTypes[ nTypes ] = ::getCppuType( (const Reference< XAggregation > *)0 );
354 0 : return types;
355 : }
356 :
357 : // WeakComponentImplHelper
358 :
359 39 : Any SAL_CALL WeakComponentImplHelper_query(
360 : Type const & rType, class_data * cd, void * that, WeakComponentImplHelperBase * pBase )
361 : SAL_THROW( (RuntimeException) )
362 : {
363 39 : checkInterface( rType );
364 39 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
365 :
366 : // shortcut XInterface to WeakComponentImplHelperBase
367 39 : if (! isXInterface( pTDR->pTypeName ))
368 : {
369 37 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
370 37 : if (p)
371 : {
372 32 : return Any( &p, pTDR );
373 : }
374 : }
375 7 : return pBase->WeakComponentImplHelperBase::queryInterface( rType );
376 : }
377 :
378 0 : Sequence< Type > SAL_CALL WeakComponentImplHelper_getTypes(
379 : class_data * cd )
380 : SAL_THROW( (RuntimeException) )
381 : {
382 0 : sal_Int32 nTypes = cd->m_nTypes;
383 0 : Sequence< Type > types( nTypes +2 );
384 0 : Type * pTypes = types.getArray();
385 0 : __fillTypes( pTypes, cd );
386 0 : pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
387 0 : pTypes[ nTypes ] = ::getCppuType( (Reference< lang::XComponent > const *)0 );
388 0 : return types;
389 : }
390 :
391 : // WeakAggComponentImplHelper
392 :
393 0 : Any SAL_CALL WeakAggComponentImplHelper_queryAgg(
394 : Type const & rType, class_data * cd, void * that, WeakAggComponentImplHelperBase * pBase )
395 : SAL_THROW( (RuntimeException) )
396 : {
397 0 : checkInterface( rType );
398 0 : typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
399 :
400 : // shortcut XInterface to WeakAggComponentImplHelperBase
401 0 : if (! isXInterface( pTDR->pTypeName ))
402 : {
403 0 : void * p = __queryDeepNoXInterface( pTDR, cd, that );
404 0 : if (p)
405 : {
406 0 : return Any( &p, pTDR );
407 : }
408 : }
409 0 : return pBase->WeakAggComponentImplHelperBase::queryAggregation( rType );
410 : }
411 :
412 0 : Sequence< Type > SAL_CALL WeakAggComponentImplHelper_getTypes(
413 : class_data * cd )
414 : SAL_THROW( (RuntimeException) )
415 : {
416 0 : sal_Int32 nTypes = cd->m_nTypes;
417 0 : Sequence< Type > types( nTypes +3 );
418 0 : Type * pTypes = types.getArray();
419 0 : __fillTypes( pTypes, cd );
420 0 : pTypes[ nTypes++ ] = ::getCppuType( (Reference< XWeak > const *)0 );
421 0 : pTypes[ nTypes++ ] = ::getCppuType( (const Reference< XAggregation > *)0 );
422 0 : pTypes[ nTypes ] = ::getCppuType( (const Reference< lang::XComponent > *)0 );
423 0 : return types;
424 : }
425 :
426 : }
427 :
428 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|