1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #using <mscorlib.dll>
29*cdf0e10cSrcweir #using "cli_ure.dll"
30*cdf0e10cSrcweir #using "cli_uretypes.dll"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "rtl/ustring.hxx"
33*cdf0e10cSrcweir #include "uno/mapping.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <vcclr.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace uno
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir namespace util
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //------------------------------------------------------------------------------
46*cdf0e10cSrcweir inline ::System::String * ustring_to_String( ::rtl::OUString const & ustr )
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir     return new ::System::String( ustr.getStr(), 0, ustr.getLength() );
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir //------------------------------------------------------------------------------
51*cdf0e10cSrcweir inline ::rtl::OUString String_to_ustring( ::System::String * str )
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     OSL_ASSERT( sizeof (wchar_t) == sizeof (sal_Unicode) );
54*cdf0e10cSrcweir     wchar_t const __pin * chars = PtrToStringChars( str );
55*cdf0e10cSrcweir     return ::rtl::OUString( chars, str->get_Length() );
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir template< typename T >
59*cdf0e10cSrcweir inline ::System::Object * to_cli(
60*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< T > const & x )
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     ::com::sun::star::uno::Mapping mapping(
63*cdf0e10cSrcweir         OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME), OUSTR(UNO_LB_CLI) );
64*cdf0e10cSrcweir     OSL_ASSERT( mapping.is() );
65*cdf0e10cSrcweir     if (! mapping.is())
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir         throw ::com::sun::star::uno::RuntimeException(
68*cdf0e10cSrcweir             OUSTR("cannot get mapping from C++ to CLI!"),
69*cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
70*cdf0e10cSrcweir               ::com::sun::star::uno::XInterface >() );
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     intptr_t intptr =
74*cdf0e10cSrcweir         reinterpret_cast< intptr_t >(
75*cdf0e10cSrcweir             mapping.mapInterface( x.get(), ::getCppuType( &x ) ) );
76*cdf0e10cSrcweir     ::System::Runtime::InteropServices::GCHandle handle(
77*cdf0e10cSrcweir         ::System::Runtime::InteropServices::GCHandle::op_Explicit( intptr ) );
78*cdf0e10cSrcweir     ::System::Object * ret = handle.get_Target();
79*cdf0e10cSrcweir     handle.Free();
80*cdf0e10cSrcweir     return ret;
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir template< typename T >
84*cdf0e10cSrcweir inline void to_uno(
85*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< T > * pRet, ::System::Object * x )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     ::com::sun::star::uno::Mapping mapping(
88*cdf0e10cSrcweir         OUSTR(UNO_LB_CLI), OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
89*cdf0e10cSrcweir     OSL_ASSERT( mapping.is() );
90*cdf0e10cSrcweir     if (! mapping.is())
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         throw ::com::sun::star::uno::RuntimeException(
93*cdf0e10cSrcweir             OUSTR("cannot get mapping from CLI to C++!"),
94*cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
95*cdf0e10cSrcweir               ::com::sun::star::uno::XInterface >() );
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     ::System::Runtime::InteropServices::GCHandle handle(
99*cdf0e10cSrcweir         ::System::Runtime::InteropServices::GCHandle::Alloc( x ) );
100*cdf0e10cSrcweir     T * ret = 0;
101*cdf0e10cSrcweir     mapping.mapInterface(
102*cdf0e10cSrcweir         reinterpret_cast< void ** >( &ret ),
103*cdf0e10cSrcweir         reinterpret_cast< void * >(
104*cdf0e10cSrcweir             ::System::Runtime::InteropServices::GCHandle::op_Explicit( handle )
105*cdf0e10cSrcweir #if defined _WIN32
106*cdf0e10cSrcweir             .ToInt32()
107*cdf0e10cSrcweir #elif defined _WIN64
108*cdf0e10cSrcweir             .ToInt64()
109*cdf0e10cSrcweir #else
110*cdf0e10cSrcweir #error ERROR: either _WIN64 or _WIN32 must be defined
111*cdf0e10cSrcweir             ERROR: either _WIN64 or _WIN32 must be defined
112*cdf0e10cSrcweir #endif
113*cdf0e10cSrcweir             ),
114*cdf0e10cSrcweir         ::getCppuType( pRet ) );
115*cdf0e10cSrcweir     handle.Free();
116*cdf0e10cSrcweir     pRet->set( ret, SAL_NO_ACQUIRE /* takeover ownership */ );
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir }
121