/************************************************************************* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright 2000, 2010 Oracle and/or its affiliates. * * OpenOffice.org - a multi-platform office productivity suite * * This file is part of OpenOffice.org. * * OpenOffice.org is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * OpenOffice.org 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 version 3 for more details * (a copy is included in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see * * for a copy of the LGPLv3 License. * ************************************************************************/ #ifndef _UNOTOOLS_UNOTUNNELHLP_HXX #define _UNOTOOLS_UNOTUNNELHLP_HXX #include #include #include #include #include #include namespace utl { namespace staruno = ::com::sun::star::uno; namespace starlang = ::com::sun::star::lang; //----------------------------------------------------------------------------------------------------------- // to use the following, define // sal_Bool getTunneledImplementation(Classname*& pObjImpl, staruno::Reference const& xObj); template sal_Bool getImplementation(Classname*& pImpl, staruno::Reference const& xTunnel) throw(staruno::RuntimeException) { if (xTunnel.is()) return getTunneledImplementation(pImpl, xTunnel); pImpl = 0; return sal_False; } template sal_Bool getImplementation(Classname*& pImpl, staruno::Reference const& xObj) throw(staruno::RuntimeException) { staruno::Reference xTunnel(xObj,staruno::UNO_QUERY); if (xTunnel.is()) return getTunneledImplementation(pImpl, xTunnel); pImpl = 0; return sal_False; } template sal_Bool getImplementation(Classname*& pImpl, staruno::Any const& aObj) throw(staruno::RuntimeException) { staruno::Reference xTunnel; if (cppu::extractInterface(xTunnel, aObj)) getTunneledImplementation(pImpl, xTunnel); pImpl = 0; return sal_False; } template sal_Bool getImplementation(Classname*& pImpl, starlang::XUnoTunnel* pObj) throw(staruno::RuntimeException) { if (pObj) { staruno::Reference xTunnel(pObj); return getTunneledImplementation(pImpl, xTunnel); } pImpl = 0; return sal_False; } //----------------------------------------------------------------------------------------------------------- class UnoTunnelId { sal_Int8 tunnelId[16]; public: UnoTunnelId(sal_Bool bUseMAC = sal_True) throw() { rtl_createUuid(reinterpret_cast(tunnelId),0,bUseMAC); } staruno::Sequence getId() const throw(staruno::RuntimeException) { return staruno::Sequence(tunnelId, sizeof(tunnelId)); } sal_Bool equalTo(staruno::Sequence const& rIdentifier) throw() { return rIdentifier.getLength() == sizeof(tunnelId) && rtl_compareMemory(tunnelId, rIdentifier.getConstArray(), sizeof(tunnelId)) == 0; } sal_Int8 const (&getIdBytes() const)[16] { return tunnelId; } }; //----------------------------------------------------------------------------------------------------------- template class UnoTunnelImplBase { protected: Classname* ThisImplementation() throw() { return static_cast(this); } sal_Int64 makeUnoSomething() throw() { return reinterpret_cast(static_cast(ThisImplementation())); } static Classname* extractUnoSomething(sal_Int64 nSomething) throw() { if (nSomething != sal_Int64()) return static_cast(reinterpret_cast(nSomething)); return NULL; } #ifdef LINUX public: #endif static Classname* extractUnoSomething( staruno::Reference const& xObj, staruno::Sequence const& rMyTunnelId ) throw(staruno::RuntimeException) { return xObj.is() ? extractUnoSomething(xObj->getSomething(rMyTunnelId)) : NULL; } }; //----------------------------------------------------------------------------------------------------------- template class UnoTunnelHelper : public UnoTunnelImplBase { protected: static UnoTunnelId s_aTunnelId; sal_Int64 getSomething(staruno::Sequence const& rTunnelId) throw() { if (s_aTunnelId.equalTo(rTunnelId)) return this->makeUnoSomething(); else return sal_Int64(); } public: static staruno::Sequence getImplementationTunnelId() throw(staruno::RuntimeException) { return s_aTunnelId.getId(); } #ifndef LINUX friend sal_Bool getTunneledImplementation(Classname*& pImpl, staruno::Reference const& xObj) throw(staruno::RuntimeException) { pImpl = UnoTunnelHelper::UnoTunnelHelper::extractUnoSomething( xObj, UnoTunnelHelper::getImplementationTunnelId() ); return pImpl != 0; } #endif }; template UnoTunnelId UnoTunnelHelper::s_aTunnelId; //----------------------------------------------------------------------------------------------------------- } // namespace utl #endif // _UNOTOOLS_UNOTUNNELHLP_HXX