tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / bridges / source / cpp_uno / shared / cppinterfaceproxy.cxx
blob324b328e8fc2c8d8573168e5bb61b698d063ac34
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 <cppinterfaceproxy.hxx>
22 #include <bridge.hxx>
23 #include <utility>
24 #include <vtablefactory.hxx>
26 #include <com/sun/star/uno/XInterface.hpp>
27 #include <typelib/typedescription.h>
29 #include <cstddef>
30 #include <memory>
31 #include <new>
33 namespace bridges::cpp_uno::shared {
35 void freeCppInterfaceProxy(uno_ExtEnvironment * pEnv, void * pInterface)
37 CppInterfaceProxy * pThis = CppInterfaceProxy::castInterfaceToProxy(
38 pInterface);
39 if (pEnv != pThis->pBridge->getCppEnv()) {
40 assert(false);
43 (*pThis->pBridge->getUnoEnv()->revokeInterface)(
44 pThis->pBridge->getUnoEnv(), pThis->pUnoI );
45 (*pThis->pUnoI->release)( pThis->pUnoI );
46 ::typelib_typedescription_release(
47 &pThis->pTypeDescr->aBase );
48 pThis->pBridge->release();
50 #if OSL_DEBUG_LEVEL > 1
51 *(int *)pInterface = 0xdeadbabe;
52 #endif
53 pThis->~CppInterfaceProxy();
54 delete[] reinterpret_cast< char * >(pThis);
57 css::uno::XInterface * CppInterfaceProxy::create(
58 bridges::cpp_uno::shared::Bridge * pBridge, uno_Interface * pUnoI,
59 typelib_InterfaceTypeDescription * pTypeDescr, OUString const & rOId)
61 typelib_typedescription_complete(
62 reinterpret_cast< typelib_TypeDescription ** >(&pTypeDescr));
63 static bridges::cpp_uno::shared::VtableFactory factory;
64 const bridges::cpp_uno::shared::VtableFactory::Vtables& rVtables(
65 factory.getVtables(pTypeDescr));
66 std::unique_ptr< char[] > pMemory(
67 new char[
68 sizeof (CppInterfaceProxy)
69 + (rVtables.count - 1) * sizeof (void **)]);
70 new(pMemory.get()) CppInterfaceProxy(pBridge, pUnoI, pTypeDescr, rOId);
71 CppInterfaceProxy * pProxy = reinterpret_cast< CppInterfaceProxy * >(
72 pMemory.release());
73 for (sal_Int32 i = 0; i < rVtables.count; ++i) {
74 pProxy->vtables[i] = VtableFactory::mapBlockToVtable(
75 rVtables.blocks[i].start);
77 // coverity[leaked_storage : FALSE] - see freeCppInterfaceProxy
78 return castProxyToInterface(pProxy);
81 void CppInterfaceProxy::acquireProxy()
83 if (++nRef == 1)
85 // rebirth of proxy zombie
86 // register at cpp env
87 void * pThis = castProxyToInterface( this );
88 (*pBridge->getCppEnv()->registerProxyInterface)(
89 pBridge->getCppEnv(), &pThis, freeCppInterfaceProxy, oid.pData,
90 pTypeDescr );
91 assert(pThis == castProxyToInterface(this));
95 void CppInterfaceProxy::releaseProxy()
97 if (! --nRef ) // last release
99 // revoke from cpp env
100 (*pBridge->getCppEnv()->revokeInterface)(
101 pBridge->getCppEnv(), castProxyToInterface( this ) );
105 CppInterfaceProxy::CppInterfaceProxy(
106 bridges::cpp_uno::shared::Bridge * pBridge_, uno_Interface * pUnoI_,
107 typelib_InterfaceTypeDescription * pTypeDescr_, OUString aOId_)
108 : nRef( 1 )
109 , pBridge( pBridge_ )
110 , pUnoI( pUnoI_ )
111 , pTypeDescr( pTypeDescr_ )
112 , oid(std::move( aOId_ ))
114 pBridge->acquire();
115 ::typelib_typedescription_acquire( &pTypeDescr->aBase );
116 (*pUnoI->acquire)( pUnoI );
117 (*pBridge->getUnoEnv()->registerInterface)(
118 pBridge->getUnoEnv(), reinterpret_cast< void ** >( &pUnoI ), oid.pData,
119 pTypeDescr );
122 CppInterfaceProxy::~CppInterfaceProxy()
125 css::uno::XInterface * CppInterfaceProxy::castProxyToInterface(
126 CppInterfaceProxy * pProxy)
128 return reinterpret_cast< css::uno::XInterface * >(
129 &pProxy->vtables);
132 CppInterfaceProxy * CppInterfaceProxy::castInterfaceToProxy(void * pInterface)
134 // pInterface == &pProxy->vtables (this emulated offsetof is not truly
135 // portable):
136 char const * const base = reinterpret_cast< char const * >(16);
137 std::ptrdiff_t const offset = reinterpret_cast< char const * >(
138 &reinterpret_cast< CppInterfaceProxy const * >(base)->vtables) - base;
139 return reinterpret_cast< CppInterfaceProxy * >(
140 static_cast< char * >(pInterface) - offset);
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */