1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 
31 #include "bridges/cpp_uno/shared/vtables.hxx"
32 
33 #include "osl/diagnose.h"
34 #include "sal/types.h"
35 #include "typelib/typedescription.h"
36 
37 #include <algorithm>
38 
39 namespace
40 {
41 
42 /**
43  *  Calculates the number of vtables associated with an interface type.
44  *
45  * <p>Multiple-inheritance C++ classes have more than one vtable.</p>
46  *
47  * @param type a non-null pointer to an interface type description
48  * @return the number of vtables associated with the given interface type
49  */
50 sal_Int32 getVtableCount(typelib_InterfaceTypeDescription const * type) {
51     sal_Int32 n = 0;
52     for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
53         n += getVtableCount(type->ppBaseTypes[i]);
54     }
55     return std::max< sal_Int32 >(n, 1);
56 }
57 
58 /**
59  * Maps a local member index to a local function index.
60  *
61  * <p><em>Local</em> members/functions are those not inherited from any base
62  * types.  The number of <em>functions</em> is potentially larger than the
63  * number of <em>members</em>, as each read&ndash;write attribute member counts
64  * as two functions.</p>
65  *
66  * @param type a non-null pointer to an interface type description
67  * @param localMember a local member index, relative to the given interface type
68  * @return the local function index corresponding to the given local member
69  *     index, relative to the given interface type
70  */
71 sal_Int32 mapLocalMemberToLocalFunction(
72     typelib_InterfaceTypeDescription * type, sal_Int32 localMember)
73 {
74     typelib_typedescription_complete(
75         reinterpret_cast< typelib_TypeDescription ** >(&type));
76     sal_Int32 localMemberOffset = type->nAllMembers - type->nMembers;
77     sal_Int32 localFunctionOffset = type->nMapFunctionIndexToMemberIndex
78         - bridges::cpp_uno::shared::getLocalFunctions(type);
79     return type->pMapMemberIndexToFunctionIndex[localMemberOffset + localMember]
80         - localFunctionOffset;
81 }
82 
83 // Since on Solaris we compile with --instances=static, getVtableSlot cannot be
84 // a template function, with explicit instantiates for
85 // T = typelib_InterfaceAttributeTypeDescription and
86 // T = typelib_InterfaceMethodTypeDescription in this file; hence, there are two
87 // overloaded versions of getVtableSlot that both delegate to this template
88 // function:
89 template< typename T > bridges::cpp_uno::shared::VtableSlot doGetVtableSlot(
90     T const * ifcMember)
91 {
92     bridges::cpp_uno::shared::VtableSlot slot;
93     slot.offset = 0;
94     T * member = const_cast< T * >(ifcMember);
95     while (member->pBaseRef != 0) {
96         OSL_ASSERT(member->nIndex < member->pInterface->nBaseTypes);
97         for (sal_Int32 i = 0; i < member->nIndex; ++i) {
98             slot.offset += getVtableCount(member->pInterface->ppBaseTypes[i]);
99         }
100         typelib_TypeDescription * desc = 0;
101         typelib_typedescriptionreference_getDescription(
102             &desc, member->pBaseRef);
103         OSL_ASSERT(
104             desc != 0 && desc->eTypeClass == member->aBase.aBase.eTypeClass);
105         if (member != ifcMember) {
106             typelib_typedescription_release(&member->aBase.aBase);
107         }
108         member = reinterpret_cast< T * >(desc);
109     }
110     slot.index
111         = bridges::cpp_uno::shared::getPrimaryFunctions(
112             member->pInterface->pBaseTypeDescription)
113         + mapLocalMemberToLocalFunction(member->pInterface, member->nIndex);
114     if (member != ifcMember) {
115         typelib_typedescription_release(&member->aBase.aBase);
116     }
117     return slot;
118 }
119 
120 }
121 
122 namespace bridges { namespace cpp_uno { namespace shared {
123 
124 sal_Int32 getLocalFunctions(typelib_InterfaceTypeDescription const * type) {
125     return type->nMembers == 0
126         ? 0
127         : (type->nMapFunctionIndexToMemberIndex
128            - type->pMapMemberIndexToFunctionIndex[
129                type->nAllMembers - type->nMembers]);
130 }
131 
132 sal_Int32 getPrimaryFunctions(typelib_InterfaceTypeDescription * type) {
133     sal_Int32 n = 0;
134     for (; type != 0; type = type->pBaseTypeDescription) {
135         typelib_typedescription_complete(
136             reinterpret_cast< typelib_TypeDescription ** >(&type));
137         n += getLocalFunctions(type);
138     }
139     return n;
140 }
141 
142 VtableSlot getVtableSlot(
143     typelib_InterfaceAttributeTypeDescription const * ifcMember)
144 {
145     return doGetVtableSlot(ifcMember);
146 }
147 
148 VtableSlot getVtableSlot(
149     typelib_InterfaceMethodTypeDescription const * ifcMember)
150 {
151     return doGetVtableSlot(ifcMember);
152 }
153 
154 } } }
155