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 .
20 #include <sal/alloca.h>
23 #include <osl/diagnose.h>
24 #include <rtl/byteseq.hxx>
25 #include <rtl/ustrbuf.hxx>
27 #include <sal/log.hxx>
28 #include <cppuhelper/compbase_ex.hxx>
30 #include <com/sun/star/uno/RuntimeException.hpp>
32 using namespace ::cppu
;
33 using namespace ::osl
;
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
41 class theImplHelperInitMutex
: public rtl::Static
<Mutex
, theImplHelperInitMutex
>{};
47 /** Shared mutex for implementation helper initialization.
50 ::osl::Mutex
& getImplHelperInitMutex()
52 return theImplHelperInitMutex::get();
56 static inline void checkInterface( Type
const & rType
)
58 if (TypeClass_INTERFACE
!= rType
.getTypeClass())
60 OUString
msg( "querying for interface \"" + rType
.getTypeName() + "\": no interface type!" );
61 SAL_WARN( "cppuhelper", msg
);
62 throw RuntimeException( msg
);
66 static inline bool isXInterface( rtl_uString
* pStr
)
68 return OUString::unacquired(&pStr
) == "com.sun.star.uno.XInterface";
71 static inline void * makeInterface( sal_IntPtr nOffset
, void * that
)
73 return (static_cast<char *>(that
) + nOffset
);
76 static inline bool td_equals(
77 typelib_TypeDescriptionReference
const * pTDR1
,
78 typelib_TypeDescriptionReference
const * pTDR2
)
80 return ((pTDR1
== pTDR2
) ||
81 OUString::unacquired(&pTDR1
->pTypeName
) == OUString::unacquired(&pTDR2
->pTypeName
));
84 static inline type_entry
* getTypeEntries( class_data
* cd
)
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?
93 for ( sal_Int32 n
= cd
->m_nTypes
; n
--; )
95 type_entry
* pEntry
= &pEntries
[ n
];
96 Type
const & rType
= (*pEntry
->m_type
.getCppuType
)( nullptr );
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 OUString
msg( "type \"" + rType
.getTypeName() + "\" is no interface type!" );
102 SAL_WARN( "cppuhelper", msg
);
103 throw RuntimeException( msg
);
105 // ref is statically held by getCppuType()
106 pEntry
->m_type
.typeRef
= rType
.getTypeLibType();
108 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
109 cd
->m_storedTypeRefs
= true;
114 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
119 static inline void fillTypes( Type
* types
, class_data
* cd
)
121 type_entry
* pEntries
= getTypeEntries( cd
);
122 for ( sal_Int32 n
= cd
->m_nTypes
; n
--; )
124 types
[ n
] = pEntries
[ n
].m_type
.typeRef
;
130 bool recursivelyFindType(
131 typelib_TypeDescriptionReference
const * demandedType
,
132 typelib_InterfaceTypeDescription
const * type
, sal_IntPtr
* offset
)
134 // This code assumes that the vtables of a multiple-inheritance class (the
135 // offset amount by which to adjust the this pointer) follow one another in
136 // the object layout, and that they contain slots for the inherited classes
137 // in a specific order. In theory, that need not hold for any given
138 // platform; in practice, it seems to work well on all supported platforms:
140 for (sal_Int32 i
= 0; i
< type
->nBaseTypes
; ++i
) {
142 *offset
+= sizeof (void *);
144 typelib_InterfaceTypeDescription
const * base
= type
->ppBaseTypes
[i
];
145 // ignore XInterface:
146 if (base
->nBaseTypes
> 0) {
149 typelib_TypeDescriptionReference
const * >(base
),
154 // Profiling showed that it is important to speed up the common case
156 if (type
->nBaseTypes
== 1) {
160 if (recursivelyFindType(demandedType
, base
, offset
)) {
170 static inline void * queryDeepNoXInterface(
171 typelib_TypeDescriptionReference
const * pDemandedTDR
, class_data
* cd
, void * that
)
173 type_entry
* pEntries
= getTypeEntries( cd
);
174 sal_Int32 nTypes
= cd
->m_nTypes
;
177 // try top interfaces without getting td
178 for ( n
= 0; n
< nTypes
; ++n
)
180 if (td_equals( pEntries
[ n
].m_type
.typeRef
, pDemandedTDR
))
182 return makeInterface( pEntries
[ n
].m_offset
, that
);
185 // query deep getting td
186 for ( n
= 0; n
< nTypes
; ++n
)
188 typelib_TypeDescription
* pTD
= nullptr;
189 TYPELIB_DANGER_GET( &pTD
, pEntries
[ n
].m_type
.typeRef
);
192 // exclude top (already tested) and bottom (XInterface) interface
194 reinterpret_cast< typelib_InterfaceTypeDescription
* >(pTD
)->
196 "### want to implement XInterface:"
197 " template argument is XInterface?!?!?!" );
198 sal_IntPtr offset
= pEntries
[n
].m_offset
;
199 bool found
= recursivelyFindType(
201 reinterpret_cast< typelib_InterfaceTypeDescription
* >(pTD
),
203 TYPELIB_DANGER_RELEASE( pTD
);
205 return makeInterface( offset
, that
);
210 OUString
msg( "cannot get type description for type \"" + OUString(pEntries
[ n
].m_type
.typeRef
->pTypeName
) + "\"!" );
211 SAL_WARN( "cppuhelper", msg
);
212 throw RuntimeException( msg
);
220 Any SAL_CALL
ImplHelper_query(
221 Type
const & rType
, class_data
* cd
, void * that
)
223 checkInterface( rType
);
224 typelib_TypeDescriptionReference
* pTDR
= rType
.getTypeLibType();
227 // shortcut for XInterface
228 if (isXInterface( pTDR
->pTypeName
))
231 p
= makeInterface( cd
->m_typeEntries
[ 0 ].m_offset
, that
);
235 p
= queryDeepNoXInterface( pTDR
, cd
, that
);
241 return Any( &p
, pTDR
);
244 Any SAL_CALL
ImplHelper_queryNoXInterface(
245 Type
const & rType
, class_data
* cd
, void * that
)
247 checkInterface( rType
);
248 typelib_TypeDescriptionReference
* pTDR
= rType
.getTypeLibType();
250 void * p
= queryDeepNoXInterface( pTDR
, cd
, that
);
253 return Any( &p
, pTDR
);
258 css::uno::Sequence
<sal_Int8
> ImplHelper_getImplementationId(
259 SAL_UNUSED_PARAMETER class_data
*)
261 return css::uno::Sequence
<sal_Int8
>();
264 Sequence
< Type
> SAL_CALL
ImplHelper_getTypes(
267 Sequence
< Type
> types( cd
->m_nTypes
);
268 Type
* pTypes
= types
.getArray();
269 fillTypes( pTypes
, cd
);
273 Sequence
< Type
> SAL_CALL
ImplInhHelper_getTypes(
274 class_data
* cd
, Sequence
< Type
> const & rAddTypes
)
276 sal_Int32 nImplTypes
= cd
->m_nTypes
;
277 sal_Int32 nAddTypes
= rAddTypes
.getLength();
278 Sequence
< Type
> types( nImplTypes
+ nAddTypes
);
279 Type
* pTypes
= types
.getArray();
280 fillTypes( pTypes
, cd
);
282 Type
const * pAddTypes
= rAddTypes
.getConstArray();
285 pTypes
[ nImplTypes
+ nAddTypes
] = pAddTypes
[ nAddTypes
];
292 Any SAL_CALL
WeakImplHelper_query(
293 Type
const & rType
, class_data
* cd
, void * that
, OWeakObject
* pBase
)
295 checkInterface( rType
);
296 typelib_TypeDescriptionReference
* pTDR
= rType
.getTypeLibType();
298 // shortcut XInterface to OWeakObject
299 if (! isXInterface( pTDR
->pTypeName
))
301 void * p
= queryDeepNoXInterface( pTDR
, cd
, that
);
304 return Any( &p
, pTDR
);
307 return pBase
->OWeakObject::queryInterface( rType
);
310 Sequence
< Type
> SAL_CALL
WeakImplHelper_getTypes(
313 sal_Int32 nTypes
= cd
->m_nTypes
;
314 Sequence
< Type
> types( nTypes
+1 );
315 Type
* pTypes
= types
.getArray();
316 fillTypes( pTypes
, cd
);
317 pTypes
[ nTypes
] = cppu::UnoType
<XWeak
>::get();
323 Any SAL_CALL
WeakAggImplHelper_queryAgg(
324 Type
const & rType
, class_data
* cd
, void * that
, OWeakAggObject
* pBase
)
326 checkInterface( rType
);
327 typelib_TypeDescriptionReference
* pTDR
= rType
.getTypeLibType();
329 // shortcut XInterface to OWeakAggObject
330 if (! isXInterface( pTDR
->pTypeName
))
332 void * p
= queryDeepNoXInterface( pTDR
, cd
, that
);
335 return Any( &p
, pTDR
);
338 return pBase
->OWeakAggObject::queryAggregation( rType
);
341 Sequence
< Type
> SAL_CALL
WeakAggImplHelper_getTypes(
344 sal_Int32 nTypes
= cd
->m_nTypes
;
345 Sequence
< Type
> types( nTypes
+2 );
346 Type
* pTypes
= types
.getArray();
347 fillTypes( pTypes
, cd
);
348 pTypes
[ nTypes
++ ] = cppu::UnoType
<XWeak
>::get();
349 pTypes
[ nTypes
] = cppu::UnoType
<XAggregation
>::get();
353 // WeakComponentImplHelper
355 Any SAL_CALL
WeakComponentImplHelper_query(
356 Type
const & rType
, class_data
* cd
, void * that
, WeakComponentImplHelperBase
* pBase
)
358 checkInterface( rType
);
359 typelib_TypeDescriptionReference
* pTDR
= rType
.getTypeLibType();
361 // shortcut XInterface to WeakComponentImplHelperBase
362 if (! isXInterface( pTDR
->pTypeName
))
364 void * p
= queryDeepNoXInterface( pTDR
, cd
, that
);
367 return Any( &p
, pTDR
);
370 return pBase
->WeakComponentImplHelperBase::queryInterface( rType
);
373 Sequence
< Type
> SAL_CALL
WeakComponentImplHelper_getTypes(
376 sal_Int32 nTypes
= cd
->m_nTypes
;
377 Sequence
< Type
> types( nTypes
+2 );
378 Type
* pTypes
= types
.getArray();
379 fillTypes( pTypes
, cd
);
380 pTypes
[ nTypes
++ ] = cppu::UnoType
<XWeak
>::get();
381 pTypes
[ nTypes
] = cppu::UnoType
<lang::XComponent
>::get();
385 // WeakAggComponentImplHelper
387 Any SAL_CALL
WeakAggComponentImplHelper_queryAgg(
388 Type
const & rType
, class_data
* cd
, void * that
, WeakAggComponentImplHelperBase
* pBase
)
390 checkInterface( rType
);
391 typelib_TypeDescriptionReference
* pTDR
= rType
.getTypeLibType();
393 // shortcut XInterface to WeakAggComponentImplHelperBase
394 if (! isXInterface( pTDR
->pTypeName
))
396 void * p
= queryDeepNoXInterface( pTDR
, cd
, that
);
399 return Any( &p
, pTDR
);
402 return pBase
->WeakAggComponentImplHelperBase::queryAggregation( rType
);
405 Sequence
< Type
> SAL_CALL
WeakAggComponentImplHelper_getTypes(
408 sal_Int32 nTypes
= cd
->m_nTypes
;
409 Sequence
< Type
> types( nTypes
+3 );
410 Type
* pTypes
= types
.getArray();
411 fillTypes( pTypes
, cd
);
412 pTypes
[ nTypes
++ ] = cppu::UnoType
<XWeak
>::get();
413 pTypes
[ nTypes
++ ] = cppu::UnoType
<XAggregation
>::get();
414 pTypes
[ nTypes
] = cppu::UnoType
<lang::XComponent
>::get();
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */