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 //------------------------------------------------------------------------------ 42 inline ::System::String ^ ustring_to_String( ::rtl::OUString const & ustr ) 43 { 44 return gcnew ::System::String( ustr.getStr(), 0, ustr.getLength() ); 45 } 46 //------------------------------------------------------------------------------ 47 inline ::rtl::OUString String_to_ustring( ::System::String ^ str ) 48 { 49 OSL_ASSERT( sizeof (wchar_t) == sizeof (sal_Unicode) ); 50 pin_ptr< wchar_t const > chars = PtrToStringChars( str ); 51 return ::rtl::OUString( chars, str->Length ); 52 } 53 54 template< typename T > 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::FromIntPtr( 74 ::System::IntPtr( intptr ) ) ); 75 ::System::Object ^ ret = handle.Target; 76 handle.Free(); 77 return ret; 78 } 79 80 template< typename T > 81 inline void to_uno( 82 ::com::sun::star::uno::Reference< T > * pRet, ::System::Object ^ x ) 83 { 84 ::com::sun::star::uno::Mapping mapping( 85 OUSTR(UNO_LB_CLI), OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME) ); 86 OSL_ASSERT( mapping.is() ); 87 if (! mapping.is()) 88 { 89 throw ::com::sun::star::uno::RuntimeException( 90 OUSTR("cannot get mapping from CLI to C++!"), 91 ::com::sun::star::uno::Reference< 92 ::com::sun::star::uno::XInterface >() ); 93 } 94 95 ::System::Runtime::InteropServices::GCHandle handle( 96 ::System::Runtime::InteropServices::GCHandle::Alloc( x ) ); 97 T * ret = 0; 98 mapping.mapInterface( 99 reinterpret_cast< void ** >( &ret ), 100 reinterpret_cast< void * >( 101 ::System::Runtime::InteropServices::GCHandle::op_Explicit( handle ) 102 #if defined _WIN32 103 .ToInt32() 104 #elif defined _WIN64 105 .ToInt64() 106 #else 107 #error ERROR: either _WIN64 or _WIN32 must be defined 108 ERROR: either _WIN64 or _WIN32 must be defined 109 #endif 110 ), 111 ::getCppuType( pRet ) ); 112 handle.Free(); 113 pRet->set( ret, SAL_NO_ACQUIRE /* takeover ownership */ ); 114 } 115 116 } 117 } 118