cURL: follow redirects
[LibreOffice.git] / binaryurp / source / proxy.cxx
blob5f23b4647208c41c8479e021ac8acd5823fcda58
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 <vector>
26 #include "cppuhelper/exc_hlp.hxx"
27 #include "rtl/ref.hxx"
28 #include "rtl/ustring.hxx"
29 #include "sal/types.h"
30 #include "typelib/typedescription.h"
31 #include "typelib/typedescription.hxx"
32 #include "uno/any2.h"
33 #include "uno/dispatcher.h"
34 #include "uno/dispatcher.hxx"
36 #include "binaryany.hxx"
37 #include "bridge.hxx"
38 #include "proxy.hxx"
40 namespace binaryurp {
42 namespace {
44 extern "C" void SAL_CALL proxy_acquireInterface(uno_Interface * pInterface) {
45 assert(pInterface != nullptr);
46 static_cast< Proxy * >(pInterface)->do_acquire();
49 extern "C" void SAL_CALL proxy_releaseInterface(uno_Interface * pInterface) {
50 assert(pInterface != nullptr);
51 static_cast< Proxy * >(pInterface)->do_release();
54 extern "C" void SAL_CALL proxy_dispatchInterface(
55 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
56 void * pReturn, void ** pArgs, uno_Any ** ppException)
58 assert(pUnoI != nullptr);
59 static_cast< Proxy * >(pUnoI)->do_dispatch(
60 pMemberType, pReturn, pArgs, ppException);
65 Proxy::Proxy(
66 rtl::Reference< Bridge > const & bridge, OUString const & oid,
67 css::uno::TypeDescription const & type):
68 bridge_(bridge), oid_(oid), type_(type), references_(1)
70 assert(bridge.is());
71 acquire = &proxy_acquireInterface;
72 release = &proxy_releaseInterface;
73 pDispatcher = &proxy_dispatchInterface;
77 void Proxy::do_acquire() {
78 if (osl_atomic_increment(&references_) == 1) {
79 bridge_->resurrectProxy(*this);
83 void Proxy::do_release() {
84 if (osl_atomic_decrement(&references_) == 0) {
85 bridge_->revokeProxy(*this);
89 void Proxy::do_free() {
90 bridge_->freeProxy(*this);
91 delete this;
94 void Proxy::do_dispatch(
95 typelib_TypeDescription const * member, void * returnValue,
96 void ** arguments, uno_Any ** exception) const
98 try {
99 try {
100 do_dispatch_throw(member, returnValue, arguments, exception);
101 } catch (const std::exception & e) {
102 throw css::uno::RuntimeException(
103 "caught C++ exception: " +
104 OStringToOUString(
105 OString(e.what()), RTL_TEXTENCODING_ASCII_US));
106 // best-effort string conversion
108 } catch (const css::uno::RuntimeException &) {
109 css::uno::Any exc(cppu::getCaughtException());
110 uno_copyAndConvertData(
111 *exception, &exc,
112 (css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).
113 get()),
114 bridge_->getCppToBinaryMapping().get());
118 bool Proxy::isProxy(
119 rtl::Reference< Bridge > const & bridge,
120 css::uno::UnoInterfaceReference const & object, OUString * oid)
122 assert(object.is());
123 return object.m_pUnoI->acquire == &proxy_acquireInterface &&
124 static_cast< Proxy * >(object.m_pUnoI)->isProxy(bridge, oid);
127 Proxy::~Proxy() {}
129 void Proxy::do_dispatch_throw(
130 typelib_TypeDescription const * member, void * returnValue,
131 void ** arguments, uno_Any ** exception) const
133 //TODO: Optimize queryInterface:
134 assert(member != nullptr);
135 bool bSetter = false;
136 std::vector< BinaryAny > inArgs;
137 switch (member->eTypeClass) {
138 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
139 bSetter = returnValue == nullptr;
140 if (bSetter) {
141 inArgs.push_back(
142 BinaryAny(
143 css::uno::TypeDescription(
144 reinterpret_cast<
145 typelib_InterfaceAttributeTypeDescription const * >(
146 member)->
147 pAttributeTypeRef),
148 arguments[0]));
150 break;
151 case typelib_TypeClass_INTERFACE_METHOD:
153 typelib_InterfaceMethodTypeDescription const * mtd =
154 reinterpret_cast<
155 typelib_InterfaceMethodTypeDescription const * >(member);
156 for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
157 if (mtd->pParams[i].bIn) {
158 inArgs.push_back(
159 BinaryAny(
160 css::uno::TypeDescription(mtd->pParams[i].pTypeRef),
161 arguments[i]));
164 break;
166 default:
167 assert(false); // this cannot happen
168 break;
170 BinaryAny ret;
171 std::vector< BinaryAny > outArgs;
172 if (bridge_->makeCall(
173 oid_,
174 css::uno::TypeDescription(
175 const_cast< typelib_TypeDescription * >(member)),
176 bSetter, inArgs, &ret, &outArgs))
178 assert(ret.getType().get()->eTypeClass == typelib_TypeClass_EXCEPTION);
179 uno_any_construct(
180 *exception, ret.getValue(ret.getType()), ret.getType().get(), nullptr);
181 } else {
182 switch (member->eTypeClass) {
183 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
184 if (!bSetter) {
185 css::uno::TypeDescription t(
186 reinterpret_cast<
187 typelib_InterfaceAttributeTypeDescription const * >(
188 member)->
189 pAttributeTypeRef);
190 uno_copyData(returnValue, ret.getValue(t), t.get(), nullptr);
192 break;
193 case typelib_TypeClass_INTERFACE_METHOD:
195 typelib_InterfaceMethodTypeDescription const * mtd =
196 reinterpret_cast<
197 typelib_InterfaceMethodTypeDescription const * >(
198 member);
199 css::uno::TypeDescription t(mtd->pReturnTypeRef);
200 if (t.get()->eTypeClass != typelib_TypeClass_VOID) {
201 uno_copyData(returnValue, ret.getValue(t), t.get(), nullptr);
203 std::vector< BinaryAny >::iterator i(outArgs.begin());
204 for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
205 if (mtd->pParams[j].bOut) {
206 css::uno::TypeDescription pt(mtd->pParams[j].pTypeRef);
207 if (mtd->pParams[j].bIn) {
208 (void) uno_assignData(
209 arguments[j], pt.get(), i++->getValue(pt),
210 pt.get(), nullptr, nullptr, nullptr);
211 } else {
212 uno_copyData(
213 arguments[j], i++->getValue(pt), pt.get(), nullptr);
217 assert(i == outArgs.end());
218 break;
220 default:
221 assert(false); // this cannot happen
222 break;
224 *exception = nullptr;
228 bool Proxy::isProxy(
229 rtl::Reference< Bridge > const & bridge, OUString * oid) const
231 assert(oid != nullptr);
232 if (bridge == bridge_) {
233 *oid = oid_;
234 return true;
235 } else {
236 return false;
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */