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