LibreOffice Module cli_ure (master) 1
native_share.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#using "cli_ure.dll"
21#using "cli_uretypes.dll"
22
23#include "rtl/ustring.hxx"
24#include <uno/lbnames.h>
25#include "uno/mapping.hxx"
26
27#include <vcclr.h>
28
29
30namespace uno
31{
32namespace util
33{
34
35
36inline ::System::String ^ ustring_to_String( OUString const & ustr )
37{
38 return gcnew ::System::String(
39 reinterpret_cast<wchar_t const *>(ustr.getStr()), 0, ustr.getLength());
40}
41
42inline OUString String_to_ustring( ::System::String ^ str )
43{
44 OSL_ASSERT( sizeof (wchar_t) == sizeof (sal_Unicode) );
45 pin_ptr<wchar_t const> chars = PtrToStringChars( str );
46 return OUString(reinterpret_cast<sal_Unicode const *>(chars), str->Length);
47}
48
49template< typename T >
50inline ::System::Object ^ to_cli(
51 css::uno::Reference< T > const & x )
52{
53 css::uno::Mapping mapping(
54 CPPU_CURRENT_LANGUAGE_BINDING_NAME, UNO_LB_CLI );
55 OSL_ASSERT( mapping.is() );
56 if (! mapping.is())
57 {
58 throw css::uno::RuntimeException(
59 "cannot get mapping from C++ to CLI!",
60 css::uno::Reference<
61 css::uno::XInterface >() );
62 }
63
64 intptr_t intptr =
65 reinterpret_cast< intptr_t >(
66 mapping.mapInterface( x.get(), cppu::UnoType<decltype(x)>::get() ) );
67 ::System::Runtime::InteropServices::GCHandle ^ handle = ::System::Runtime::InteropServices::GCHandle::FromIntPtr(::System::IntPtr(intptr));
68 ::System::Object ^ ret = handle->Target;
69 handle->Free();
70 return ret;
71}
72
73template< typename T >
74inline void to_uno(
75 css::uno::Reference< T > * pRet, ::System::Object ^ x )
76{
77 css::uno::Mapping mapping(
78 UNO_LB_CLI, CPPU_CURRENT_LANGUAGE_BINDING_NAME );
79 OSL_ASSERT( mapping.is() );
80 if (! mapping.is())
81 {
82 throw css::uno::RuntimeException(
83 "cannot get mapping from CLI to C++!",
84 css::uno::Reference<
85 css::uno::XInterface >() );
86 }
87
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 _WIN64
96 .ToInt64()
97#elif defined _WIN32
98 .ToInt32()
99#else
100#error ERROR: either _WIN64 or _WIN32 must be defined
101 ERROR: either _WIN64 or _WIN32 must be defined
102#endif
103 ),
105 handle.Free();
106 pRet->set( ret, SAL_NO_ACQUIRE /* takeover ownership */ );
107}
108
109}
110}
111
112/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
float x
ERROR
void to_uno(css::uno::Reference< T > *pRet, ::System::Object ^ x)
Definition: native_share.h:74
OUString String_to_ustring(::System::String ^ str)
Definition: native_share.h:42
inline ::System::String ustring_to_String(OUString const &ustr)
Definition: native_share.h:36
inline ::System::Object to_cli(css::uno::Reference< T > const &x)
Definition: native_share.h:50
sal_uInt16 sal_Unicode