update dev300-m58
[ooovba.git] / cppu / test / surrogate.hxx
blob3ea172ed27a6514f8af555ab11aad2a1906bf719
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: surrogate.hxx,v $
10 * $Revision: 1.4 $
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 #include <uno/dispatcher.h>
32 #include <uno/mapping.hxx>
33 #include <uno/environment.h>
34 #include <uno/data.h>
35 #include <osl/diagnose.h>
36 #include <osl/interlck.h>
38 #include <com/sun/star/uno/Reference.hxx>
41 //==================================================================================================
42 struct UnoMediator : public uno_Interface
44 oslInterlockedCount nRef;
45 uno_Interface * pDest;
47 UnoMediator( uno_Interface * pDest );
48 ~UnoMediator();
51 //--------------------------------------------------------------------------------------------------
52 inline static void SAL_CALL UnoMediator_acquire( uno_Interface * pUnoI )
54 osl_incrementInterlockedCount( &((UnoMediator *)pUnoI)->nRef );
56 //--------------------------------------------------------------------------------------------------
57 inline static void SAL_CALL UnoMediator_release( uno_Interface * pUnoI )
59 if (! osl_decrementInterlockedCount( &((UnoMediator *)pUnoI)->nRef ))
60 delete (UnoMediator *)pUnoI;
62 //--------------------------------------------------------------------------------------------------
63 inline static void SAL_CALL UnoMediator_dispatch(
64 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberType,
65 void * pReturn, void * pArgs[], uno_Any ** ppException )
67 (*((UnoMediator *)pUnoI)->pDest->pDispatcher)(
68 ((UnoMediator *)pUnoI)->pDest, pMemberType, pReturn, pArgs, ppException );
71 //__________________________________________________________________________________________________
72 UnoMediator::UnoMediator( uno_Interface * pDest_ )
73 : nRef( 0 )
74 , pDest( pDest_ )
76 (*pDest->acquire)( pDest );
77 uno_Interface::acquire = UnoMediator_acquire;
78 uno_Interface::release = UnoMediator_release;
79 uno_Interface::pDispatcher = UnoMediator_dispatch;
81 //__________________________________________________________________________________________________
82 UnoMediator::~UnoMediator()
84 (*pDest->release)( pDest );
88 //##################################################################################################
90 template< class T >
91 inline sal_Bool makeSurrogate( com::sun::star::uno::Reference< T > & rOut,
92 const com::sun::star::uno::Reference< T > & rOriginal )
94 rOut.clear();
96 typelib_TypeDescription * pTD = 0;
97 const com::sun::star::uno::Type & rType = ::getCppuType( &rOriginal );
98 TYPELIB_DANGER_GET( &pTD, rType.getTypeLibType() );
99 OSL_ENSURE( pTD, "### cannot get typedescription!" );
100 if (pTD)
102 uno_Environment * pCppEnv1 = 0;
103 uno_Environment * pCppEnv2 = 0;
105 ::rtl::OUString aCppEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
106 uno_getEnvironment( &pCppEnv1, aCppEnvTypeName.pData, 0 );
107 uno_createEnvironment( &pCppEnv2, aCppEnvTypeName.pData, 0 ); // anonymous
109 ::com::sun::star::uno::Mapping aMapping( pCppEnv1, pCppEnv2, ::rtl::OUString::createFromAscii("prot") );
110 T * p = (T *)aMapping.mapInterface( rOriginal.get(), (typelib_InterfaceTypeDescription *)pTD );
111 if (p)
113 rOut = p;
114 p->release();
117 (*pCppEnv2->release)( pCppEnv2 );
118 (*pCppEnv1->release)( pCppEnv1 );
120 TYPELIB_DANGER_RELEASE( pTD );
123 ::com::sun::star::uno::Mapping aCpp2Uno( CPPU_CURRENT_LANGUAGE_BINDING_NAME, UNO_LB_UNO );
124 ::com::sun::star::uno::Mapping aUno2Cpp( UNO_LB_UNO, CPPU_CURRENT_LANGUAGE_BINDING_NAME );
125 OSL_ENSURE( aCpp2Uno.is() && aUno2Cpp.is(), "### cannot get mappings!" );
126 if (aCpp2Uno.is() && aUno2Cpp.is())
128 typelib_TypeDescription * pTD = 0;
129 const com::sun::star::uno::Type & rType = ::getCppuType( &rOriginal );
130 TYPELIB_DANGER_GET( &pTD, rType.getTypeLibType() );
131 OSL_ENSURE( pTD, "### cannot get typedescription!" );
132 if (pTD)
134 uno_Interface * pUno = (uno_Interface *)aCpp2Uno.mapInterface(
135 rOriginal.get(), (typelib_InterfaceTypeDescription *)pTD );
136 if (pUno)
138 UnoMediator * pPseudo = new UnoMediator( pUno );
139 (*pPseudo->acquire)( pPseudo );
140 OSL_ENSURE( uno_equals( &pUno, &pPseudo, pTD, 0 ), "### interfaces don't belong to same object, but they do!?" );
141 (*pUno->release)( pUno );
143 T * pCpp = (T *)aUno2Cpp.mapInterface(
144 pPseudo, (typelib_InterfaceTypeDescription *)pTD );
145 (*pPseudo->release)( pPseudo );
147 if (pCpp)
149 rOut = pCpp;
150 pCpp->release();
153 TYPELIB_DANGER_RELEASE( pTD );
157 return rOut.is();