summaryrefslogtreecommitdiff
path: root/cppuhelper/source/implbase_ex.cxx
blob: 3f88feb97dd1f92115a3b23d1a7f9d5d44fd14a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <osl/diagnose.h>
#include <sal/log.hxx>
#include <cppuhelper/compbase_ex.hxx>
#include <cppuhelper/implbase_ex.hxx>

#include <com/sun/star/uno/RuntimeException.hpp>

#include <mutex>

using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

namespace cppu
{

static void checkInterface( Type const & rType )
{
    if (TypeClass_INTERFACE != rType.getTypeClass())
    {
        OUString msg( "querying for interface \"" + rType.getTypeName() + "\": no interface type!" );
        SAL_WARN( "cppuhelper", msg );
        throw RuntimeException( msg );
    }
}

static bool isXInterface( rtl_uString * pStr )
{
    return OUString::unacquired(&pStr) == "com.sun.star.uno.XInterface";
}

static void * makeInterface( sal_IntPtr nOffset, void * that )
{
    return (static_cast<char *>(that) + nOffset);
}

static bool td_equals(
    typelib_TypeDescriptionReference const * pTDR1,
    typelib_TypeDescriptionReference const * pTDR2 )
{
    return ((pTDR1 == pTDR2) ||
            OUString::unacquired(&pTDR1->pTypeName) == OUString::unacquired(&pTDR2->pTypeName));
}

static type_entry * getTypeEntries( class_data * cd )
{
    type_entry * pEntries = cd->m_typeEntries;
    if (! cd->m_storedTypeRefs) // not inited?
    {
        static std::mutex aMutex;
        std::scoped_lock guard( aMutex );
        if (! cd->m_storedTypeRefs) // not inited?
        {
            // get all types
            for ( sal_Int32 n = cd->m_nTypes; n--; )
            {
                type_entry * pEntry = &pEntries[ n ];
                Type const & rType = (*pEntry->m_type.getCppuType)( nullptr );
                OSL_ENSURE( rType.getTypeClass() == TypeClass_INTERFACE, "### wrong helper init: expected interface!" );
                OSL_ENSURE( ! isXInterface( rType.getTypeLibType()->pTypeName ), "### want to implement XInterface: template argument is XInterface?!?!?!" );
                if (rType.getTypeClass() != TypeClass_INTERFACE)
                {
                    OUString msg( "type \"" + rType.getTypeName() + "\" is no interface type!" );
                    SAL_WARN( "cppuhelper", msg );
                    throw RuntimeException( msg );
                }
                // ref is statically held by getCppuType()
                pEntry->m_type.typeRef = rType.getTypeLibType();
            }
            OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
            cd->m_storedTypeRefs = true;
        }
    }
    else
    {
        OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
    }
    return pEntries;
}

static void fillTypes( Type * types, class_data * cd )
{
    type_entry * pEntries = getTypeEntries( cd );
    for ( sal_Int32 n = cd->m_nTypes; n--; )
    {
        types[ n ] = pEntries[ n ].m_type.typeRef;
    }
}

namespace {

bool recursivelyFindType(
    typelib_TypeDescriptionReference const * demandedType,
    typelib_InterfaceTypeDescription const * type, sal_IntPtr * offset)
{
    // This code assumes that the vtables of a multiple-inheritance class (the
    // offset amount by which to adjust the this pointer) follow one another in
    // the object layout, and that they contain slots for the inherited classes
    // in a specific order.  In theory, that need not hold for any given
    // platform; in practice, it seems to work well on all supported platforms:
 next:
    for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
        if (i > 0) {
            *offset += sizeof (void *);
        }
        typelib_InterfaceTypeDescription const * base = type->ppBaseTypes[i];
        // ignore XInterface:
        if (base->nBaseTypes > 0) {
            if (td_equals(
                    reinterpret_cast<
                        typelib_TypeDescriptionReference const * >(base),
                    demandedType))
            {
                return true;
            }
            // Profiling showed that it is important to speed up the common case
            // of only one base:
            if (type->nBaseTypes == 1) {
                type = base;
                goto next;
            }
            if (recursivelyFindType(demandedType, base, offset)) {
                return true;
            }
        }
    }
    return false;
}

}

static void * queryDeepNoXInterface(
    typelib_TypeDescriptionReference const * pDemandedTDR, class_data * cd, void * that )
{
    type_entry * pEntries = getTypeEntries( cd );
    sal_Int32 nTypes = cd->m_nTypes;
    sal_Int32 n;

    // try top interfaces without getting td
    for ( n = 0; n < nTypes; ++n )
    {
        if (td_equals( pEntries[ n ].m_type.typeRef, pDemandedTDR ))
        {
            return makeInterface( pEntries[ n ].m_offset, that );
        }
    }
    // query deep getting td
    for ( n = 0; n < nTypes; ++n )
    {
        typelib_TypeDescription * pTD = nullptr;
        TYPELIB_DANGER_GET( &pTD, pEntries[ n ].m_type.typeRef );
        if (pTD)
        {
            // exclude top (already tested) and bottom (XInterface) interface
            OSL_ENSURE(
                reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD)->
                    nBaseTypes > 0,
                "### want to implement XInterface:"
                    " template argument is XInterface?!?!?!" );
            sal_IntPtr offset = pEntries[n].m_offset;
            bool found = recursivelyFindType(
                pDemandedTDR,
                reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD),
                &offset);
            TYPELIB_DANGER_RELEASE( pTD );
            if (found) {
                return makeInterface( offset, that );
            }
        }
        else
        {
            OUString msg( "cannot get type description for type \"" + OUString::unacquired(&pEntries[ n ].m_type.typeRef->pTypeName) + "\"!" );
            SAL_WARN( "cppuhelper", msg );
            throw RuntimeException( msg );
        }
    }
    return nullptr;
}

// ImplHelper

Any SAL_CALL ImplHelper_query(
    Type const & rType, class_data * cd, void * that )
{
    checkInterface( rType );
    typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();

    void * p;
    // shortcut for XInterface
    if (isXInterface( pTDR->pTypeName ))
    {
        // take first one
        p = makeInterface( cd->m_typeEntries[ 0 ].m_offset, that );
    }
    else
    {
        p = queryDeepNoXInterface( pTDR, cd, that );
        if (! p)
        {
            return Any();
        }
    }
    return Any( &p, pTDR );
}

Any SAL_CALL ImplHelper_queryNoXInterface(
    Type const & rType, class_data * cd, void * that )
{
    checkInterface( rType );
    typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();

    void * p = queryDeepNoXInterface( pTDR, cd, that );
    if (p)
    {
        return Any( &p, pTDR );
    }
    return Any();
}

css::uno::Sequence<sal_Int8> ImplHelper_getImplementationId(
    SAL_UNUSED_PARAMETER class_data *)
{
    return css::uno::Sequence<sal_Int8>();
}

Sequence< Type > SAL_CALL ImplHelper_getTypes(
    class_data * cd )
{
    Sequence< Type > types( cd->m_nTypes );
    Type * pTypes = types.getArray();
    fillTypes( pTypes, cd );
    return types;
}

Sequence< Type >  SAL_CALL ImplInhHelper_getTypes(
    class_data * cd, Sequence< Type > const & rAddTypes )
{
    sal_Int32 nImplTypes = cd->m_nTypes;
    Sequence<Type> types(nImplTypes + rAddTypes.getLength());
    Type * pTypes = types.getArray();
    fillTypes( pTypes, cd );
    // append base types
    std::copy(rAddTypes.begin(), rAddTypes.end(), pTypes + nImplTypes);
    return types;
}

// WeakImplHelper

Any SAL_CALL WeakImplHelper_query(
    Type const & rType, class_data * cd, void * that, OWeakObject * pBase )
{
    checkInterface( rType );
    typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();

    // shortcut XInterface to OWeakObject
    if (! isXInterface( pTDR->pTypeName ))
    {
        void * p = queryDeepNoXInterface( pTDR, cd, that );
        if (p)
        {
            return Any( &p, pTDR );
        }
    }
    return pBase->OWeakObject::queryInterface( rType );
}

Sequence< Type > SAL_CALL WeakImplHelper_getTypes(
    class_data * cd )
{
    sal_Int32 nTypes = cd->m_nTypes;
    Sequence< Type > types( nTypes +1 );
    Type * pTypes = types.getArray();
    fillTypes( pTypes, cd );
    pTypes[ nTypes ] = cppu::UnoType<XWeak>::get();
    return types;
}

// WeakAggImplHelper

Any SAL_CALL WeakAggImplHelper_queryAgg(
    Type const & rType, class_data * cd, void * that, OWeakAggObject * pBase )
{
    checkInterface( rType );
    typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();

    // shortcut XInterface to OWeakAggObject
    if (! isXInterface( pTDR->pTypeName ))
    {
        void * p = queryDeepNoXInterface( pTDR, cd, that );
        if (p)
        {
            return Any( &p, pTDR );
        }
    }
    return pBase->OWeakAggObject::queryAggregation( rType );
}

Sequence< Type > SAL_CALL WeakAggImplHelper_getTypes(
    class_data * cd )
{
    sal_Int32 nTypes = cd->m_nTypes;
    Sequence< Type > types( nTypes +2 );
    Type * pTypes = types.getArray();
    fillTypes( pTypes, cd );
    pTypes[ nTypes++ ] = cppu::UnoType<XWeak>::get();
    pTypes[ nTypes ] = cppu::UnoType<XAggregation>::get();
    return types;
}

// WeakComponentImplHelper

Any SAL_CALL WeakComponentImplHelper_query(
    Type const & rType, class_data * cd, void * that, WeakComponentImplHelperBase * pBase )
{
    checkInterface( rType );
    typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();

    // shortcut XInterface to WeakComponentImplHelperBase
    if (! isXInterface( pTDR->pTypeName ))
    {
        void * p = queryDeepNoXInterface( pTDR, cd, that );
        if (p)
        {
            return Any( &p, pTDR );
        }
    }
    return pBase->WeakComponentImplHelperBase::queryInterface( rType );
}

Sequence< Type > SAL_CALL WeakComponentImplHelper_getTypes(
    class_data * cd )
{
    sal_Int32 nTypes = cd->m_nTypes;
    Sequence< Type > types( nTypes +2 );
    Type * pTypes = types.getArray();
    fillTypes( pTypes, cd );
    pTypes[ nTypes++ ] = cppu::UnoType<XWeak>::get();
    pTypes[ nTypes ] = cppu::UnoType<lang::XComponent>::get();
    return types;
}

// WeakAggComponentImplHelper

Any SAL_CALL WeakAggComponentImplHelper_queryAgg(
    Type const & rType, class_data * cd, void * that, WeakAggComponentImplHelperBase * pBase )
{
    checkInterface( rType );
    typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();

    // shortcut XInterface to WeakAggComponentImplHelperBase
    if (! isXInterface( pTDR->pTypeName ))
    {
        void * p = queryDeepNoXInterface( pTDR, cd, that );
        if (p)
        {
            return Any( &p, pTDR );
        }
    }
    return pBase->WeakAggComponentImplHelperBase::queryAggregation( rType );
}

Sequence< Type > SAL_CALL WeakAggComponentImplHelper_getTypes(
    class_data * cd )
{
    sal_Int32 nTypes = cd->m_nTypes;
    Sequence< Type > types( nTypes +3 );
    Type * pTypes = types.getArray();
    fillTypes( pTypes, cd );
    pTypes[ nTypes++ ] = cppu::UnoType<XWeak>::get();
    pTypes[ nTypes++ ] = cppu::UnoType<XAggregation>::get();
    pTypes[ nTypes ] = cppu::UnoType<lang::XComponent>::get();
    return types;
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */