diff options
author | Jens-Heiner Rechtien <hr@openoffice.org> | 2004-02-03 11:53:10 +0000 |
---|---|---|
committer | Jens-Heiner Rechtien <hr@openoffice.org> | 2004-02-03 11:53:10 +0000 |
commit | b3632fcd59bddf61b9c0894d1dd30608aa46c83a (patch) | |
tree | b3ca6bb861dbc7a19a41befdfd957858dc604e01 /bridges | |
parent | 728bd887e3885799528bc9dd407f398773389bf7 (diff) |
INTEGRATION: CWS sb10 (1.1.2); FILE ADDED
2004/01/08 13:43:41 sb 1.1.2.1: #114000# Factored out more shared code.
Diffstat (limited to 'bridges')
-rw-r--r-- | bridges/source/cpp_uno/shared/vtablefactory.cxx | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/bridges/source/cpp_uno/shared/vtablefactory.cxx b/bridges/source/cpp_uno/shared/vtablefactory.cxx new file mode 100644 index 000000000000..bcf0ae5d8749 --- /dev/null +++ b/bridges/source/cpp_uno/shared/vtablefactory.cxx @@ -0,0 +1,201 @@ +/************************************************************************* + * + * $RCSfile: vtablefactory.cxx,v $ + * + * $Revision: 1.2 $ + * + * last change: $Author: hr $ $Date: 2004-02-03 12:53:10 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (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.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include "bridges/cpp_uno/shared/vtablefactory.hxx" + +#include "guardedarray.hxx" + +#include "bridges/cpp_uno/shared/vtables.hxx" + +#include "osl/diagnose.h" +#include "osl/mutex.hxx" +#include "rtl/ustring.hxx" +#include "sal/types.h" +#include "typelib/typedescription.hxx" + +#include <hash_map> +#include <vector> + +using bridges::cpp_uno::shared::VtableFactory; + +class VtableFactory::GuardedBlocks: public std::vector< char * > { +public: + GuardedBlocks(): m_guarded(true) {} + + ~GuardedBlocks(); + + void unguard() { m_guarded = false; } + +private: + GuardedBlocks(GuardedBlocks &); // not implemented + void operator =(GuardedBlocks); // not implemented + + bool m_guarded; +}; + +VtableFactory::GuardedBlocks::~GuardedBlocks() { + if (m_guarded) { + for (iterator i(begin()); i != end(); ++i) { + delete[] *i; + } + } +} + +class VtableFactory::BaseOffset { +public: + BaseOffset(typelib_InterfaceTypeDescription * type) { calculate(type, 0); } + + sal_Int32 getFunctionOffset(rtl::OUString const & name) const + { return m_map.find(name)->second; } + +private: + sal_Int32 calculate( + typelib_InterfaceTypeDescription * type, sal_Int32 offset); + + typedef std::hash_map< rtl::OUString, sal_Int32, rtl::OUStringHash > Map; + + Map m_map; +}; + +sal_Int32 VtableFactory::BaseOffset::calculate( + typelib_InterfaceTypeDescription * type, sal_Int32 offset) +{ + rtl::OUString name(type->aBase.pTypeName); + if (m_map.find(name) == m_map.end()) { + for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) { + offset = calculate(type->ppBaseTypes[i], offset); + } + m_map.insert(Map::value_type(name, offset)); + typelib_typedescription_complete( + reinterpret_cast< typelib_TypeDescription ** >(&type)); + offset += bridges::cpp_uno::shared::getLocalFunctions(type); + } + return offset; +} + +VtableFactory::VtableFactory() {} + +VtableFactory::~VtableFactory() { + osl::MutexGuard guard(m_mutex); + for (Map::iterator i(m_map.begin()); i != m_map.end(); ++i) { + for (sal_Int32 j = 0; j < i->second.count; ++j) { + delete[] i->second.blocks[j]; + } + delete[] i->second.blocks; + } +} + +VtableFactory::Vtables VtableFactory::getVtables( + typelib_InterfaceTypeDescription * type) +{ + rtl::OUString name(type->aBase.pTypeName); + osl::MutexGuard guard(m_mutex); + Map::iterator i(m_map.find(name)); + if (i == m_map.end()) { + GuardedBlocks blocks; + createVtables(blocks, BaseOffset(type), type, true); + Vtables vtables; + OSL_ASSERT(blocks.size() <= SAL_MAX_INT32); + vtables.count = static_cast< sal_Int32 >(blocks.size()); + bridges::cpp_uno::shared::GuardedArray< char * > guardedBlocks( + new char *[vtables.count]); + vtables.blocks = guardedBlocks.get(); + for (sal_Int32 j = 0; j < vtables.count; ++j) { + vtables.blocks[j] = blocks[j]; + } + i = m_map.insert(Map::value_type(name, vtables)).first; + guardedBlocks.release(); + blocks.unguard(); + } + return i->second; +} + +void VtableFactory::createVtables( + GuardedBlocks & blocks, BaseOffset const & baseOffset, + typelib_InterfaceTypeDescription * type, bool includePrimary) +{ + if (includePrimary) { + sal_Int32 slotCount + = bridges::cpp_uno::shared::getPrimaryFunctions(type); + void ** slots; + bridges::cpp_uno::shared::GuardedArray< char > block( + createBlock(slotCount, &slots)); + slots += slotCount; + unsigned char * code = reinterpret_cast< unsigned char * >(slots); + sal_Int32 vtableOffset = blocks.size() * sizeof (void **); + for (typelib_InterfaceTypeDescription const * type2 = type; type2 != 0; + type2 = type2->pBaseTypeDescription) + { + sal_Int32 functionCount + = bridges::cpp_uno::shared::getLocalFunctions(type2); + slots -= functionCount; + code = addLocalFunctions( + slots, code, type2, + baseOffset.getFunctionOffset(type2->aBase.pTypeName), + functionCount, vtableOffset); + } + blocks.push_back(block.get()); + block.release(); + } + for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) { + createVtables(blocks, baseOffset, type->ppBaseTypes[i], i != 0); + } +} |