Branch libreoffice-5-0-4
[LibreOffice.git] / testtools / source / performance / pseudo.cxx
bloba23ea010c9ab4ed1d3e909dbf44b7a5d3011dfa6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/diagnose.h>
21 #include <osl/interlck.h>
22 #include <rtl/ustring.hxx>
23 #include <typelib/typedescription.h>
24 #include <uno/dispatcher.h>
25 #include <uno/environment.h>
26 #include <uno/mapping.h>
27 #include <uno/lbnames.h>
30 namespace pseudo_uno
34 struct pseudo_Mapping : public uno_Mapping
36 oslInterlockedCount nRef;
38 uno_ExtEnvironment * pFrom;
39 uno_ExtEnvironment * pTo;
41 pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ );
42 ~pseudo_Mapping();
45 //==== a uno pseudo proxy =============================================================================
46 struct pseudo_unoInterfaceProxy : public uno_Interface
48 oslInterlockedCount nRef;
49 pseudo_Mapping * pPseudoMapping;
51 // mapping information
52 uno_Interface * pUnoI; // wrapped interface
53 typelib_InterfaceTypeDescription * pTypeDescr;
54 OUString oid;
56 // ctor
57 inline pseudo_unoInterfaceProxy( pseudo_Mapping * pPseudoMapping_,
58 uno_Interface * pUnoI_,
59 typelib_InterfaceTypeDescription * pTypeDescr_,
60 const OUString & rOId_ );
63 static void SAL_CALL pseudo_unoInterfaceProxy_dispatch(
64 uno_Interface * pUnoI,
65 const typelib_TypeDescription * pMemberType,
66 void * pReturn,
67 void * pArgs[],
68 uno_Any ** ppException )
70 pseudo_unoInterfaceProxy * pThis = static_cast< pseudo_unoInterfaceProxy * >( pUnoI );
71 (*pThis->pUnoI->pDispatcher)( pThis->pUnoI, pMemberType, pReturn, pArgs, ppException );
75 static void SAL_CALL pseudo_unoInterfaceProxy_free( uno_ExtEnvironment * pEnv, void * pProxy )
77 pseudo_unoInterfaceProxy * pThis =
78 static_cast< pseudo_unoInterfaceProxy * >(
79 reinterpret_cast< uno_Interface * >( pProxy ) );
80 OSL_ASSERT( pEnv == pThis->pPseudoMapping->pTo );
82 (*pThis->pPseudoMapping->pFrom->revokeInterface)( pThis->pPseudoMapping->pFrom, pThis->pUnoI );
83 (*pThis->pUnoI->release)( pThis->pUnoI );
84 typelib_typedescription_release( (typelib_TypeDescription *)pThis->pTypeDescr );
85 (*pThis->pPseudoMapping->release)( pThis->pPseudoMapping );
87 #if OSL_DEBUG_LEVEL > 1
88 *(int *)pProxy = 0xdeadbabe;
89 #endif
90 delete pThis;
93 static void SAL_CALL pseudo_unoInterfaceProxy_acquire( uno_Interface * pUnoI )
95 if (1 == osl_atomic_increment( &static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
97 // rebirth of proxy zombie
98 // register at uno env
99 void * pThis = static_cast< uno_Interface * >( pUnoI );
100 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->registerProxyInterface)(
101 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo,
102 &pThis, pseudo_unoInterfaceProxy_free,
103 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->oid.pData,
104 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pTypeDescr );
105 OSL_ASSERT( pThis == static_cast< uno_Interface * >( pUnoI ) );
109 static void SAL_CALL pseudo_unoInterfaceProxy_release( uno_Interface * pUnoI )
111 if (! osl_atomic_decrement( & static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
113 // revoke from uno env on last release
114 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->revokeInterface)(
115 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo, pUnoI );
119 inline pseudo_unoInterfaceProxy::pseudo_unoInterfaceProxy(
120 pseudo_Mapping * pPseudoMapping_, uno_Interface * pUnoI_,
121 typelib_InterfaceTypeDescription * pTypeDescr_, const OUString & rOId_ )
122 : nRef( 1 )
123 , pPseudoMapping( pPseudoMapping_ )
124 , pUnoI( pUnoI_ )
125 , pTypeDescr( pTypeDescr_ )
126 , oid( rOId_ )
128 (*pPseudoMapping->acquire)( pPseudoMapping );
129 typelib_typedescription_acquire( (typelib_TypeDescription *)pTypeDescr );
130 (*pPseudoMapping->pFrom->registerInterface)(
131 pPseudoMapping->pFrom, reinterpret_cast< void ** >( &pUnoI ), oid.pData, pTypeDescr );
132 (*pUnoI->acquire)( pUnoI );
134 // uno_Interface
135 uno_Interface::acquire = pseudo_unoInterfaceProxy_acquire;
136 uno_Interface::release = pseudo_unoInterfaceProxy_release;
137 uno_Interface::pDispatcher = pseudo_unoInterfaceProxy_dispatch;
141 static void SAL_CALL pseudo_Mapping_mapInterface(
142 uno_Mapping * pMapping, void ** ppOut,
143 void * pUnoI, typelib_InterfaceTypeDescription * pTypeDescr )
145 OSL_ASSERT( ppOut && pTypeDescr );
146 if (*ppOut)
148 (*reinterpret_cast< uno_Interface * >( *ppOut )->release)(
149 reinterpret_cast< uno_Interface * >( *ppOut ) );
150 *ppOut = 0;
152 if (pUnoI && pTypeDescr)
154 // get object id of uno interface to be wrapped
155 rtl_uString * pOId = 0;
156 (*static_cast< pseudo_Mapping * >( pMapping )->pFrom->getObjectIdentifier)(
157 static_cast< pseudo_Mapping * >( pMapping )->pFrom, &pOId, pUnoI );
158 OSL_ASSERT( pOId );
160 if (pOId)
162 // try to get any known interface from target environment
163 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->getRegisteredInterface)(
164 static_cast< pseudo_Mapping * >( pMapping )->pTo, ppOut, pOId, pTypeDescr );
165 if (! *ppOut) // no existing interface, register new proxy interface
167 // try to publish a new proxy (ref count initially 1)
168 void * pProxy = new pseudo_unoInterfaceProxy(
169 static_cast< pseudo_Mapping * >( pMapping ),
170 reinterpret_cast< uno_Interface * >( pUnoI ), pTypeDescr, pOId );
172 // proxy may be exchanged during registration
173 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->registerProxyInterface)(
174 static_cast< pseudo_Mapping * >( pMapping )->pTo,
175 &pProxy, pseudo_unoInterfaceProxy_free, pOId, pTypeDescr );
177 *ppOut = pProxy;
179 rtl_uString_release( pOId );
184 static void SAL_CALL pseudo_Mapping_free( uno_Mapping * pMapping )
186 delete static_cast< pseudo_Mapping * >( pMapping );
189 static void SAL_CALL pseudo_Mapping_acquire( uno_Mapping * pMapping )
191 if (1 == osl_atomic_increment( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
193 OUString aMappingPurpose("pseudo");
194 uno_registerMapping( &pMapping,
195 pseudo_Mapping_free,
196 (uno_Environment *)((pseudo_Mapping *)pMapping)->pFrom,
197 (uno_Environment *)((pseudo_Mapping *)pMapping)->pTo,
198 aMappingPurpose.pData );
202 static void SAL_CALL pseudo_Mapping_release( uno_Mapping * pMapping )
204 if (! osl_atomic_decrement( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
206 uno_revokeMapping( pMapping );
211 pseudo_Mapping::pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ )
212 : nRef( 1 )
213 , pFrom( pFrom_ )
214 , pTo( pTo_ )
216 (*((uno_Environment *)pFrom)->acquire)( (uno_Environment *)pFrom );
217 (*((uno_Environment *)pTo)->acquire)( (uno_Environment *)pTo );
219 uno_Mapping::acquire = pseudo_Mapping_acquire;
220 uno_Mapping::release = pseudo_Mapping_release;
221 uno_Mapping::mapInterface = pseudo_Mapping_mapInterface;
224 pseudo_Mapping::~pseudo_Mapping()
226 (*((uno_Environment *)pTo)->release)( (uno_Environment *)pTo );
227 (*((uno_Environment *)pFrom)->release)( (uno_Environment *)pFrom );
233 extern "C" void SAL_CALL uno_initEnvironment( uno_Environment * pUnoEnv )
235 OSL_FAIL( "### no impl: unexpected call!" );
238 extern "C" void SAL_CALL uno_ext_getMapping(
239 uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo )
241 OSL_ASSERT( ppMapping && pFrom && pTo );
242 if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
244 uno_Mapping * pMapping = 0;
246 if (0 == rtl_ustr_ascii_compare( pFrom->pTypeName->buffer, UNO_LB_UNO ) &&
247 0 == rtl_ustr_ascii_compare( pTo->pTypeName->buffer, UNO_LB_UNO ))
249 OUString aMappingPurpose("pseudo");
250 // ref count is initially 1
251 pMapping = new pseudo_uno::pseudo_Mapping( pFrom->pExtEnv, pTo->pExtEnv );
252 uno_registerMapping( &pMapping, pseudo_uno::pseudo_Mapping_free,
253 (uno_Environment *)pFrom->pExtEnv,
254 (uno_Environment *)pTo->pExtEnv,
255 aMappingPurpose.pData );
258 if (*ppMapping)
259 (*(*ppMapping)->release)( *ppMapping );
260 *ppMapping = pMapping;
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */