Update git submodules
[LibreOffice.git] / binaryurp / source / proxy.cxx
blob49705e06aa89d263e9093c546c4cd95834723b36
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 #include <sal/config.h>
22 #include <cassert>
23 #include <exception>
24 #include <utility>
25 #include <vector>
27 #include <cppuhelper/exc_hlp.hxx>
28 #include <o3tl/runtimetooustring.hxx>
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>
38 #include "binaryany.hxx"
39 #include "bridge.hxx"
40 #include "proxy.hxx"
42 namespace binaryurp {
44 namespace {
46 extern "C" void proxy_acquireInterface(uno_Interface * pInterface) {
47 assert(pInterface != nullptr);
48 static_cast< Proxy * >(pInterface)->do_acquire();
51 extern "C" void proxy_releaseInterface(uno_Interface * pInterface) {
52 assert(pInterface != nullptr);
53 static_cast< Proxy * >(pInterface)->do_release();
56 extern "C" void proxy_dispatchInterface(
57 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
58 void * pReturn, void ** pArgs, uno_Any ** ppException)
60 assert(pUnoI != nullptr);
61 static_cast< Proxy * >(pUnoI)->do_dispatch(
62 pMemberType, pReturn, pArgs, ppException);
67 Proxy::Proxy(
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)
72 assert(bridge.is());
73 acquire = &proxy_acquireInterface;
74 release = &proxy_releaseInterface;
75 pDispatcher = &proxy_dispatchInterface;
79 void Proxy::do_acquire() {
80 if (++references_ == 1) {
81 bridge_->resurrectProxy(*this);
85 void Proxy::do_release() {
86 if (--references_ == 0) {
87 bridge_->revokeProxy(*this);
91 void Proxy::do_free() {
92 bridge_->freeProxy(*this);
93 delete this;
96 void Proxy::do_dispatch(
97 typelib_TypeDescription const * member, void * returnValue,
98 void ** arguments, uno_Any ** exception) const
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()));
107 } catch (const css::uno::RuntimeException &) {
108 css::uno::Any exc(cppu::getCaughtException());
109 uno_copyAndConvertData(
110 *exception, &exc,
111 (css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).
112 get()),
113 bridge_->getCppToBinaryMapping().get());
117 bool Proxy::isProxy(
118 rtl::Reference< Bridge > const & bridge,
119 css::uno::UnoInterfaceReference const & object, OUString * oid)
121 assert(object.is());
122 return object.m_pUnoI->acquire == &proxy_acquireInterface &&
123 static_cast< Proxy * >(object.m_pUnoI)->isProxy(bridge, oid);
126 Proxy::~Proxy() {}
128 void Proxy::do_dispatch_throw(
129 typelib_TypeDescription const * member, void * returnValue,
130 void ** arguments, uno_Any ** exception) const
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]);
148 break;
149 case typelib_TypeClass_INTERFACE_METHOD:
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]);
161 break;
163 default:
164 assert(false); // this cannot happen
165 break;
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))
175 assert(ret.getType().get()->eTypeClass == typelib_TypeClass_EXCEPTION);
176 uno_any_construct(
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);
189 break;
190 case typelib_TypeClass_INTERFACE_METHOD:
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);
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 {
209 uno_copyData(
210 arguments[j], i++->getValue(pt), pt.get(), nullptr);
214 assert(i == outArgs.end());
215 break;
217 default:
218 assert(false); // this cannot happen
219 break;
221 *exception = nullptr;
225 bool Proxy::isProxy(
226 rtl::Reference< Bridge > const & bridge, OUString * oid) const
228 assert(oid != nullptr);
229 if (bridge == bridge_) {
230 *oid = oid_;
231 return true;
232 } else {
233 return false;
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */