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