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