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