1*2123d757SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2123d757SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2123d757SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2123d757SAndrew Rist  * distributed with this work for additional information
6*2123d757SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2123d757SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2123d757SAndrew Rist  * "License"); you may not use this file except in compliance
9*2123d757SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2123d757SAndrew Rist  *
11*2123d757SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2123d757SAndrew Rist  *
13*2123d757SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2123d757SAndrew Rist  * software distributed under the License is distributed on an
15*2123d757SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2123d757SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2123d757SAndrew Rist  * specific language governing permissions and limitations
18*2123d757SAndrew Rist  * under the License.
19*2123d757SAndrew Rist  *
20*2123d757SAndrew Rist  *************************************************************/
21*2123d757SAndrew Rist 
22*2123d757SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #using <mscorlib.dll>
25cdf0e10cSrcweir #using "cli_ure.dll"
26cdf0e10cSrcweir #using "cli_uretypes.dll"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "rtl/ustring.hxx"
29cdf0e10cSrcweir #include "uno/mapping.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <vcclr.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace uno
37cdf0e10cSrcweir {
38cdf0e10cSrcweir namespace util
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //------------------------------------------------------------------------------
ustring_to_String(::rtl::OUString const & ustr)42cdf0e10cSrcweir inline ::System::String * ustring_to_String( ::rtl::OUString const & ustr )
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     return new ::System::String( ustr.getStr(), 0, ustr.getLength() );
45cdf0e10cSrcweir }
46cdf0e10cSrcweir //------------------------------------------------------------------------------
String_to_ustring(::System::String * str)47cdf0e10cSrcweir inline ::rtl::OUString String_to_ustring( ::System::String * str )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     OSL_ASSERT( sizeof (wchar_t) == sizeof (sal_Unicode) );
50cdf0e10cSrcweir     wchar_t const __pin * chars = PtrToStringChars( str );
51cdf0e10cSrcweir     return ::rtl::OUString( chars, str->get_Length() );
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir template< typename T >
to_cli(::com::sun::star::uno::Reference<T> const & x)55cdf0e10cSrcweir inline ::System::Object * to_cli(
56cdf0e10cSrcweir     ::com::sun::star::uno::Reference< T > const & x )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     ::com::sun::star::uno::Mapping mapping(
59cdf0e10cSrcweir         OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME), OUSTR(UNO_LB_CLI) );
60cdf0e10cSrcweir     OSL_ASSERT( mapping.is() );
61cdf0e10cSrcweir     if (! mapping.is())
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         throw ::com::sun::star::uno::RuntimeException(
64cdf0e10cSrcweir             OUSTR("cannot get mapping from C++ to CLI!"),
65cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
66cdf0e10cSrcweir               ::com::sun::star::uno::XInterface >() );
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     intptr_t intptr =
70cdf0e10cSrcweir         reinterpret_cast< intptr_t >(
71cdf0e10cSrcweir             mapping.mapInterface( x.get(), ::getCppuType( &x ) ) );
72cdf0e10cSrcweir     ::System::Runtime::InteropServices::GCHandle handle(
73cdf0e10cSrcweir         ::System::Runtime::InteropServices::GCHandle::op_Explicit( intptr ) );
74cdf0e10cSrcweir     ::System::Object * ret = handle.get_Target();
75cdf0e10cSrcweir     handle.Free();
76cdf0e10cSrcweir     return ret;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir template< typename T >
to_uno(::com::sun::star::uno::Reference<T> * pRet,::System::Object * x)80cdf0e10cSrcweir inline void to_uno(
81cdf0e10cSrcweir     ::com::sun::star::uno::Reference< T > * pRet, ::System::Object * x )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     ::com::sun::star::uno::Mapping mapping(
84cdf0e10cSrcweir         OUSTR(UNO_LB_CLI), OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
85cdf0e10cSrcweir     OSL_ASSERT( mapping.is() );
86cdf0e10cSrcweir     if (! mapping.is())
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         throw ::com::sun::star::uno::RuntimeException(
89cdf0e10cSrcweir             OUSTR("cannot get mapping from CLI to C++!"),
90cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
91cdf0e10cSrcweir               ::com::sun::star::uno::XInterface >() );
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     ::System::Runtime::InteropServices::GCHandle handle(
95cdf0e10cSrcweir         ::System::Runtime::InteropServices::GCHandle::Alloc( x ) );
96cdf0e10cSrcweir     T * ret = 0;
97cdf0e10cSrcweir     mapping.mapInterface(
98cdf0e10cSrcweir         reinterpret_cast< void ** >( &ret ),
99cdf0e10cSrcweir         reinterpret_cast< void * >(
100cdf0e10cSrcweir             ::System::Runtime::InteropServices::GCHandle::op_Explicit( handle )
101cdf0e10cSrcweir #if defined _WIN32
102cdf0e10cSrcweir             .ToInt32()
103cdf0e10cSrcweir #elif defined _WIN64
104cdf0e10cSrcweir             .ToInt64()
105cdf0e10cSrcweir #else
106cdf0e10cSrcweir #error ERROR: either _WIN64 or _WIN32 must be defined
107cdf0e10cSrcweir             ERROR: either _WIN64 or _WIN32 must be defined
108cdf0e10cSrcweir #endif
109cdf0e10cSrcweir             ),
110cdf0e10cSrcweir         ::getCppuType( pRet ) );
111cdf0e10cSrcweir     handle.Free();
112cdf0e10cSrcweir     pRet->set( ret, SAL_NO_ACQUIRE /* takeover ownership */ );
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir }
116cdf0e10cSrcweir }
117