merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / shared / bridge.cxx
blob6825c3aafd53b12e208d5655fad7ab04e7c08b84
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: bridge.cxx,v $
10 * $Revision: 1.6 $
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/bridge.hxx"
36 #include "component.hxx"
38 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
39 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
41 #include "com/sun/star/uno/XInterface.hpp"
42 #include "osl/diagnose.h"
43 #include "osl/interlck.h"
44 #include "rtl/ustring.h"
45 #include "sal/types.h"
46 #include "typelib/typedescription.h"
47 #include "uno/dispatcher.h"
48 #include "uno/environment.h"
49 #include "uno/mapping.h"
51 namespace bridges { namespace cpp_uno { namespace shared {
53 void freeMapping(uno_Mapping * pMapping)
55 delete static_cast< Bridge::Mapping * >( pMapping )->pBridge;
58 void acquireMapping(uno_Mapping * pMapping)
60 static_cast< Bridge::Mapping * >( pMapping )->pBridge->acquire();
63 void releaseMapping(uno_Mapping * pMapping)
65 static_cast< Bridge::Mapping * >( pMapping )->pBridge->release();
68 void cpp2unoMapping(
69 uno_Mapping * pMapping, void ** ppUnoI, void * pCppI,
70 typelib_InterfaceTypeDescription * pTypeDescr)
72 OSL_ENSURE( ppUnoI && pTypeDescr, "### null ptr!" );
73 if (*ppUnoI)
75 (*reinterpret_cast< uno_Interface * >( *ppUnoI )->release)(
76 reinterpret_cast< uno_Interface * >( *ppUnoI ) );
77 *ppUnoI = 0;
79 if (pCppI)
81 Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
83 // get object id of interface to be wrapped
84 rtl_uString * pOId = 0;
85 (*pBridge->pCppEnv->getObjectIdentifier)(
86 pBridge->pCppEnv, &pOId, pCppI );
87 OSL_ASSERT( pOId );
89 // try to get any known interface from target environment
90 (*pBridge->pUnoEnv->getRegisteredInterface)(
91 pBridge->pUnoEnv, ppUnoI, pOId, pTypeDescr );
93 if (! *ppUnoI) // no existing interface, register new proxy interface
95 // try to publish a new proxy (refcount initially 1)
96 uno_Interface * pSurrogate
97 = bridges::cpp_uno::shared::UnoInterfaceProxy::create(
98 pBridge,
99 static_cast< ::com::sun::star::uno::XInterface * >( pCppI ),
100 pTypeDescr, pOId );
102 // proxy may be exchanged during registration
103 (*pBridge->pUnoEnv->registerProxyInterface)(
104 pBridge->pUnoEnv, reinterpret_cast< void ** >( &pSurrogate ),
105 freeUnoInterfaceProxy, pOId,
106 pTypeDescr );
108 *ppUnoI = pSurrogate;
110 ::rtl_uString_release( pOId );
114 void uno2cppMapping(
115 uno_Mapping * pMapping, void ** ppCppI, void * pUnoI,
116 typelib_InterfaceTypeDescription * pTypeDescr)
118 OSL_ASSERT( ppCppI && pTypeDescr );
119 if (*ppCppI)
121 static_cast< ::com::sun::star::uno::XInterface * >( *ppCppI )->
122 release();
123 *ppCppI = 0;
125 if (pUnoI)
127 Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
129 // get object id of uno interface to be wrapped
130 rtl_uString * pOId = 0;
131 (*pBridge->pUnoEnv->getObjectIdentifier)(
132 pBridge->pUnoEnv, &pOId, pUnoI );
133 OSL_ASSERT( pOId );
135 // try to get any known interface from target environment
136 (*pBridge->pCppEnv->getRegisteredInterface)(
137 pBridge->pCppEnv, ppCppI, pOId, pTypeDescr );
139 if (! *ppCppI) // no existing interface, register new proxy interface
141 // try to publish a new proxy (ref count initially 1)
142 com::sun::star::uno::XInterface * pProxy
143 = bridges::cpp_uno::shared::CppInterfaceProxy::create(
144 pBridge, static_cast< uno_Interface * >( pUnoI ),
145 pTypeDescr, pOId );
147 // proxy may be exchanged during registration
148 (*pBridge->pCppEnv->registerProxyInterface)(
149 pBridge->pCppEnv, reinterpret_cast< void ** >( &pProxy ),
150 freeCppInterfaceProxy, pOId,
151 pTypeDescr );
153 *ppCppI = pProxy;
155 ::rtl_uString_release( pOId );
159 uno_Mapping * Bridge::createMapping(
160 uno_ExtEnvironment * pCppEnv, uno_ExtEnvironment * pUnoEnv,
161 bool bExportCpp2Uno) SAL_THROW(())
163 Bridge * bridge = new Bridge(pCppEnv, pUnoEnv, bExportCpp2Uno);
164 return bExportCpp2Uno ? &bridge->aCpp2Uno : &bridge->aUno2Cpp;
167 void Bridge::acquire() SAL_THROW(())
169 if (1 == osl_incrementInterlockedCount( &nRef ))
171 if (bExportCpp2Uno)
173 uno_Mapping * pMapping = &aCpp2Uno;
174 ::uno_registerMapping(
175 &pMapping, freeMapping, (uno_Environment *)pCppEnv,
176 (uno_Environment *)pUnoEnv, 0 );
178 else
180 uno_Mapping * pMapping = &aUno2Cpp;
181 ::uno_registerMapping(
182 &pMapping, freeMapping, (uno_Environment *)pUnoEnv,
183 (uno_Environment *)pCppEnv, 0 );
188 void Bridge::release() SAL_THROW(())
190 if (! osl_decrementInterlockedCount( &nRef ))
192 ::uno_revokeMapping( bExportCpp2Uno ? &aCpp2Uno : &aUno2Cpp );
196 Bridge::Bridge(
197 uno_ExtEnvironment * pCppEnv_, uno_ExtEnvironment * pUnoEnv_,
198 bool bExportCpp2Uno_) SAL_THROW(())
199 : nRef( 1 )
200 , pCppEnv( pCppEnv_ )
201 , pUnoEnv( pUnoEnv_ )
202 , bExportCpp2Uno( bExportCpp2Uno_ )
204 bridges::cpp_uno::shared::g_moduleCount.modCnt.acquire(
205 &bridges::cpp_uno::shared::g_moduleCount.modCnt );
207 aCpp2Uno.pBridge = this;
208 aCpp2Uno.acquire = acquireMapping;
209 aCpp2Uno.release = releaseMapping;
210 aCpp2Uno.mapInterface = cpp2unoMapping;
212 aUno2Cpp.pBridge = this;
213 aUno2Cpp.acquire = acquireMapping;
214 aUno2Cpp.release = releaseMapping;
215 aUno2Cpp.mapInterface = uno2cppMapping;
217 (*((uno_Environment *)pCppEnv)->acquire)( (uno_Environment *)pCppEnv );
218 (*((uno_Environment *)pUnoEnv)->acquire)( (uno_Environment *)pUnoEnv );
221 Bridge::~Bridge() SAL_THROW(())
223 (*((uno_Environment *)pUnoEnv)->release)( (uno_Environment *)pUnoEnv );
224 (*((uno_Environment *)pCppEnv)->release)( (uno_Environment *)pCppEnv );
225 bridges::cpp_uno::shared::g_moduleCount.modCnt.release(
226 &bridges::cpp_uno::shared::g_moduleCount.modCnt );
229 } } }