LibreOffice Module binaryurp (master) 1
proxy.cxx
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#include <sal/config.h>
21
22#include <cassert>
23#include <exception>
24#include <utility>
25#include <vector>
26
29#include <rtl/ref.hxx>
30#include <rtl/ustring.hxx>
31#include <sal/types.h>
32#include <typelib/typedescription.h>
33#include <typelib/typedescription.hxx>
34#include <uno/any2.h>
35#include <uno/dispatcher.h>
36#include <uno/dispatcher.hxx>
37
38#include "binaryany.hxx"
39#include "bridge.hxx"
40#include "proxy.hxx"
41
42namespace binaryurp {
43
44namespace {
45
46extern "C" void proxy_acquireInterface(uno_Interface * pInterface) {
47 assert(pInterface != nullptr);
48 static_cast< Proxy * >(pInterface)->do_acquire();
49}
50
51extern "C" void proxy_releaseInterface(uno_Interface * pInterface) {
52 assert(pInterface != nullptr);
53 static_cast< Proxy * >(pInterface)->do_release();
54}
55
56extern "C" void proxy_dispatchInterface(
57 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
58 void * pReturn, void ** pArgs, uno_Any ** ppException)
59{
60 assert(pUnoI != nullptr);
61 static_cast< Proxy * >(pUnoI)->do_dispatch(
62 pMemberType, pReturn, pArgs, ppException);
63}
64
65}
66
68 rtl::Reference< Bridge > const & bridge, OUString oid,
69 css::uno::TypeDescription type):
70 bridge_(bridge), oid_(std::move(oid)), type_(std::move(type)), references_(1)
71{
72 assert(bridge.is());
73 acquire = &proxy_acquireInterface;
74 release = &proxy_releaseInterface;
75 pDispatcher = &proxy_dispatchInterface;
76}
77
78
79void Proxy::do_acquire() {
80 if (++references_ == 1) {
81 bridge_->resurrectProxy(*this);
82 }
83}
84
85void Proxy::do_release() {
86 if (--references_ == 0) {
87 bridge_->revokeProxy(*this);
88 }
89}
90
91void Proxy::do_free() {
92 bridge_->freeProxy(*this);
93 delete this;
94}
95
96void Proxy::do_dispatch(
97 typelib_TypeDescription const * member, void * returnValue,
98 void ** arguments, uno_Any ** exception) const
99{
100 try {
101 try {
102 do_dispatch_throw(member, returnValue, arguments, exception);
103 } catch (const std::exception & e) {
104 throw css::uno::RuntimeException(
105 "caught C++ exception: " + o3tl::runtimeToOUString(e.what()));
106 }
107 } catch (const css::uno::RuntimeException &) {
108 css::uno::Any exc(cppu::getCaughtException());
110 *exception, &exc,
111 (css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).
112 get()),
113 bridge_->getCppToBinaryMapping().get());
114 }
115}
116
117bool Proxy::isProxy(
118 rtl::Reference< Bridge > const & bridge,
119 css::uno::UnoInterfaceReference const & object, OUString * oid)
120{
121 assert(object.is());
122 return object.m_pUnoI->acquire == &proxy_acquireInterface &&
123 static_cast< Proxy * >(object.m_pUnoI)->isProxy(bridge, oid);
124}
125
127
128void Proxy::do_dispatch_throw(
129 typelib_TypeDescription const * member, void * returnValue,
130 void ** arguments, uno_Any ** exception) const
131{
132 //TODO: Optimize queryInterface:
133 assert(member != nullptr);
134 bool bSetter = false;
135 std::vector< BinaryAny > inArgs;
136 switch (member->eTypeClass) {
137 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
138 bSetter = returnValue == nullptr;
139 if (bSetter) {
140 inArgs.emplace_back(
141 css::uno::TypeDescription(
142 reinterpret_cast<
143 typelib_InterfaceAttributeTypeDescription const * >(
144 member)->
145 pAttributeTypeRef),
146 arguments[0]);
147 }
148 break;
149 case typelib_TypeClass_INTERFACE_METHOD:
150 {
151 typelib_InterfaceMethodTypeDescription const * mtd =
152 reinterpret_cast<
153 typelib_InterfaceMethodTypeDescription const * >(member);
154 for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
155 if (mtd->pParams[i].bIn) {
156 inArgs.emplace_back(
157 css::uno::TypeDescription(mtd->pParams[i].pTypeRef),
158 arguments[i]);
159 }
160 }
161 break;
162 }
163 default:
164 assert(false); // this cannot happen
165 break;
166 }
167 BinaryAny ret;
168 std::vector< BinaryAny > outArgs;
169 if (bridge_->makeCall(
170 oid_,
171 css::uno::TypeDescription(
172 const_cast< typelib_TypeDescription * >(member)),
173 bSetter, std::move(inArgs), &ret, &outArgs))
174 {
175 assert(ret.getType().get()->eTypeClass == typelib_TypeClass_EXCEPTION);
177 *exception, ret.getValue(ret.getType()), ret.getType().get(), nullptr);
178 } else {
179 switch (member->eTypeClass) {
180 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
181 if (!bSetter) {
182 css::uno::TypeDescription t(
183 reinterpret_cast<
184 typelib_InterfaceAttributeTypeDescription const * >(
185 member)->
186 pAttributeTypeRef);
187 uno_copyData(returnValue, ret.getValue(t), t.get(), nullptr);
188 }
189 break;
190 case typelib_TypeClass_INTERFACE_METHOD:
191 {
192 typelib_InterfaceMethodTypeDescription const * mtd =
193 reinterpret_cast<
194 typelib_InterfaceMethodTypeDescription const * >(
195 member);
196 css::uno::TypeDescription t(mtd->pReturnTypeRef);
197 if (t.get()->eTypeClass != typelib_TypeClass_VOID) {
198 uno_copyData(returnValue, ret.getValue(t), t.get(), nullptr);
199 }
200 std::vector< BinaryAny >::iterator i(outArgs.begin());
201 for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
202 if (mtd->pParams[j].bOut) {
203 css::uno::TypeDescription pt(mtd->pParams[j].pTypeRef);
204 if (mtd->pParams[j].bIn) {
205 (void) uno_assignData(
206 arguments[j], pt.get(), i++->getValue(pt),
207 pt.get(), nullptr, nullptr, nullptr);
208 } else {
210 arguments[j], i++->getValue(pt), pt.get(), nullptr);
211 }
212 }
213 }
214 assert(i == outArgs.end());
215 break;
216 }
217 default:
218 assert(false); // this cannot happen
219 break;
220 }
221 *exception = nullptr;
222 }
223}
224
225bool Proxy::isProxy(
226 rtl::Reference< Bridge > const & bridge, OUString * oid) const
227{
228 assert(oid != nullptr);
229 if (bridge == bridge_) {
230 *oid = oid_;
231 return true;
232 } else {
233 return false;
234 }
235}
236
237}
238
239/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
void SAL_CALL uno_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
com::sun::star::uno::TypeDescription getType() const noexcept
Definition: binaryany.cxx:97
void * getValue(com::sun::star::uno::TypeDescription const &type) const noexcept
Definition: binaryany.cxx:101
Proxy(rtl::Reference< Bridge > const &bridge, OUString oid, com::sun::star::uno::TypeDescription type)
sal_Bool SAL_CALL uno_assignData(void *pDest, typelib_TypeDescription *pDestTD, void *pSource, typelib_TypeDescription *pSourceTD, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
void SAL_CALL uno_copyData(void *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
void SAL_CALL uno_copyAndConvertData(void *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
GVariantType * type_
struct _typelib_TypeDescription typelib_TypeDescription
struct _uno_Any uno_Any
css::uno::UnoInterfaceReference get()
Any SAL_CALL getCaughtException()
int i
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
OUString runtimeToOUString(char const *runtimeString)
ResultType type