Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / cli_ure / source / native / native_share.h
blob23977dfb0f006c90233a8474f7bf3c229dd92301
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #using "cli_ure.dll"
21 #using "cli_uretypes.dll"
23 #include "rtl/ustring.hxx"
24 #include "uno/mapping.hxx"
26 #include <vcclr.h>
28 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
31 namespace uno
33 namespace util
36 //------------------------------------------------------------------------------
37 inline ::System::String ^ ustring_to_String( ::rtl::OUString const & ustr )
39 return gcnew ::System::String( ustr.getStr(), 0, ustr.getLength() );
41 //------------------------------------------------------------------------------
42 inline ::rtl::OUString String_to_ustring( ::System::String ^ str )
44 OSL_ASSERT( sizeof (wchar_t) == sizeof (sal_Unicode) );
45 pin_ptr<wchar_t const> chars = PtrToStringChars( str );
46 return ::rtl::OUString( chars, str->Length );
49 template< typename T >
50 inline ::System::Object ^ to_cli(
51 ::com::sun::star::uno::Reference< T > const & x )
53 ::com::sun::star::uno::Mapping mapping(
54 OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME), OUSTR(UNO_LB_CLI) );
55 OSL_ASSERT( mapping.is() );
56 if (! mapping.is())
58 throw ::com::sun::star::uno::RuntimeException(
59 OUSTR("cannot get mapping from C++ to CLI!"),
60 ::com::sun::star::uno::Reference<
61 ::com::sun::star::uno::XInterface >() );
64 intptr_t intptr =
65 reinterpret_cast< intptr_t >(
66 mapping.mapInterface( x.get(), ::getCppuType( &x ) ) );
67 ::System::Runtime::InteropServices::GCHandle ^ handle = (::System::Runtime::InteropServices::GCHandle ^)(gcnew ::System::IntPtr(intptr));
68 ::System::Object ^ ret = handle->Target;
69 handle->Free();
70 return ret;
73 template< typename T >
74 inline void to_uno(
75 ::com::sun::star::uno::Reference< T > * pRet, ::System::Object ^ x )
77 ::com::sun::star::uno::Mapping mapping(
78 OUSTR(UNO_LB_CLI), OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
79 OSL_ASSERT( mapping.is() );
80 if (! mapping.is())
82 throw ::com::sun::star::uno::RuntimeException(
83 OUSTR("cannot get mapping from CLI to C++!"),
84 ::com::sun::star::uno::Reference<
85 ::com::sun::star::uno::XInterface >() );
88 ::System::Runtime::InteropServices::GCHandle handle(
89 ::System::Runtime::InteropServices::GCHandle::Alloc( x ) );
90 T * ret = 0;
91 mapping.mapInterface(
92 reinterpret_cast< void ** >( &ret ),
93 reinterpret_cast< void * >(
94 ::System::Runtime::InteropServices::GCHandle::op_Explicit( handle )
95 #if defined _WIN32
96 .ToInt32()
97 #elif defined _WIN64
98 .ToInt64()
99 #else
100 #error ERROR: either _WIN64 or _WIN32 must be defined
101 ERROR: either _WIN64 or _WIN32 must be defined
102 #endif
104 ::getCppuType( pRet ) );
105 handle.Free();
106 pRet->set( ret, SAL_NO_ACQUIRE /* takeover ownership */ );
112 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */