update dev300-m58
[ooovba.git] / cli_ure / source / mono_bridge / mono_proxy.cxx
blob0a5f130c7dbb6af0f7a45baf7ecb9609bb1595af
1 /*************************************************************************
3 * $RCSfile: $
5 * $Revision: $
7 * last change: $Author: $ $Date: $
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 * MA 02111-1307 USA
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Sun Microsystems, Inc.
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): _______________________________________
60 ************************************************************************/
62 #include "mono_bridge.h"
64 #include "rtl/ustring.h"
65 #include "uno/dispatcher.h"
66 #include "uno/environment.h"
67 #include "typelib/typedescription.h"
69 extern "C" {
70 #include "mono/metadata/threads.h"
73 using namespace mono_uno;
75 extern "C" {
77 static void SAL_CALL mono_proxy_free( uno_ExtEnvironment * env, void * proxy)
79 MonoProxy * monoProxy = reinterpret_cast< MonoProxy * >( proxy );
81 delete monoProxy;
84 uno_Interface * SAL_CALL cli_uno_environment_createMonoProxyAndRegister(
85 uno_ExtEnvironment *pUnoEnv, void *pMonoProxy, rtl_uString *pOid,
86 typelib_TypeDescription *pTD )
87 SAL_THROW_EXTERN_C()
89 uno_Interface * proxy = static_cast< uno_Interface * >(
90 new MonoProxy( pUnoEnv,
91 static_cast< guint32 >( reinterpret_cast< sal_IntPtr >( pMonoProxy ) ),
92 pOid, pTD ) );
94 pUnoEnv->registerProxyInterface(
95 pUnoEnv,
96 reinterpret_cast< void ** >( &proxy ),
97 mono_proxy_free,
98 pOid,
99 (typelib_InterfaceTypeDescription*) pTD );
101 return proxy;
104 static void SAL_CALL mono_proxy_acquire( uno_Interface * pUnoI )
106 MonoProxy const * that = static_cast< MonoProxy const * >( pUnoI );
107 that->acquire();
110 static void SAL_CALL mono_proxy_release( uno_Interface * pUnoI )
112 MonoProxy const * that = static_cast< MonoProxy const * >( pUnoI );
113 that->release();
116 static void SAL_CALL mono_proxy_dispatch(
117 uno_Interface * pUnoI, typelib_TypeDescription const * member_td,
118 void * uno_ret, void * uno_args [], uno_Any ** uno_exc )
119 SAL_THROW_EXTERN_C()
121 MonoProxy * that = static_cast< MonoProxy * >( pUnoI );
122 that->dispatch( member_td, uno_ret, uno_args, uno_exc );
125 } // extern "C"
127 namespace mono_uno
130 MonoProxy::MonoProxy(uno_ExtEnvironment * pUnoEnv, guint32 managedProxy,
131 rtl_uString *pOid, typelib_TypeDescription * pTD):
132 m_ref(1),
133 m_unoEnv(pUnoEnv),
134 m_managedProxy(managedProxy), // FIXME free this in the destructor?
135 m_Oid(pOid),
136 m_unoType(pTD)
138 uno_Interface::acquire = mono_proxy_acquire;
139 uno_Interface::release = mono_proxy_release;
140 uno_Interface::pDispatcher = mono_proxy_dispatch;
142 MonoObject * pObj = mono_gchandle_get_target( m_managedProxy );
143 MonoClass * pClass = mono_object_get_class( pObj );
144 MonoMethodDesc * pMethodDesc = mono_method_desc_new( "ManagedProxy:Dispatch", FALSE );
145 m_managedDispatch = mono_method_desc_search_in_class( pMethodDesc, pClass );
146 mono_method_desc_free( pMethodDesc );
147 OSL_ASSERT( 0 != m_managedDispatch );
150 inline void MonoProxy::acquire() const
152 if (1 == osl_incrementInterlockedCount( &m_ref ))
154 // rebirth of proxy zombie
155 void * that = const_cast< MonoProxy * >( this );
156 // register at uno env
157 (*m_unoEnv->registerProxyInterface)(
158 m_unoEnv, &that,
159 mono_proxy_free, m_Oid.pData,
160 (typelib_InterfaceTypeDescription *)m_unoType.get() );
161 #if OSL_DEBUG_LEVEL >= 2
162 OSL_ASSERT( this == (void const * const)that );
163 #endif
167 inline void MonoProxy::release() const
169 if (0 == osl_decrementInterlockedCount( &m_ref ))
171 // revoke from uno env on last release,
172 // The proxy can be resurrected if acquire is called before the uno
173 // environment calls mono_proxy_free. mono_proxy_free will
174 //delete the proxy. The environment does not acquire a registered
175 //proxy.
176 (*m_unoEnv->revokeInterface)(
177 m_unoEnv, const_cast< MonoProxy * >( this ) );
181 inline void MonoProxy::dispatch( typelib_TypeDescription const * member_td,
182 void * uno_ret, void * uno_args [], uno_Any ** uno_exc )
184 OSL_ASSERT( m_managedDispatch != 0 );
186 gpointer pMonoParams[4];
188 pMonoParams[0] = const_cast< typelib_TypeDescription * >(member_td);
189 pMonoParams[1] = uno_ret;
190 pMonoParams[2] = uno_args;
191 pMonoParams[3] = uno_exc;
193 MonoObject *obj = mono_gchandle_get_target( m_managedProxy );
195 mono_uno::runtime_invoke( m_managedDispatch,
196 obj, pMonoParams, NULL,
197 mono_object_get_domain( obj ) );