Update git submodules
[LibreOffice.git] / cppu / source / helper / purpenv / helper_purpenv_Mapping.cxx
blob1fc4436f4f2f2fb1588f14ed678a9a6776092797
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 .
21 #include <cppu/helper/purpenv/Mapping.hxx>
23 #include "Proxy.hxx"
25 #include <osl/interlck.h>
26 #include <sal/log.hxx>
27 #include <uno/environment.hxx>
28 #include <uno/dispatcher.h>
30 using namespace com::sun::star;
32 namespace {
34 class Mapping : public uno_Mapping
36 uno::Environment m_from;
37 uno::Environment m_to;
39 oslInterlockedCount m_nCount;
41 cppu::helper::purpenv::ProbeFun * m_probeFun;
42 void * m_pContext;
44 public:
45 explicit Mapping(uno_Environment * pFrom,
46 uno_Environment * pTo,
47 cppu::helper::purpenv::ProbeFun * probeFun,
48 void * pProbeContext);
49 virtual ~Mapping();
51 void mapInterface(
52 uno_Interface ** ppOut,
53 uno_Interface * pUnoI,
54 typelib_InterfaceTypeDescription * pTypeDescr);
56 void acquire();
57 void release();
62 static void s_mapInterface(
63 uno_Mapping * puno_Mapping,
64 void ** ppOut,
65 void * pUnoI,
66 typelib_InterfaceTypeDescription * pTypeDescr ) noexcept
68 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
69 pMapping->mapInterface(
70 reinterpret_cast<uno_Interface **>(ppOut),
71 static_cast<uno_Interface *>(pUnoI), pTypeDescr);
74 extern "C" {
75 static void s_acquire(uno_Mapping * puno_Mapping) noexcept
77 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
78 pMapping->acquire();
81 static void s_release(uno_Mapping * puno_Mapping) noexcept
83 Mapping * pMapping = static_cast<Mapping * >(puno_Mapping);
84 pMapping->release();
88 static void s_getIdentifier_v(va_list * pParam)
90 uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *);
91 rtl_uString ** ppOid = va_arg(*pParam, rtl_uString **);
92 uno_Interface * pUnoI = va_arg(*pParam, uno_Interface *);
94 pEnv->getObjectIdentifier(pEnv, ppOid, pUnoI);
97 static void s_free(uno_Mapping * puno_Mapping) noexcept
99 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
100 delete pMapping;
104 Mapping::Mapping(uno_Environment * pFrom,
105 uno_Environment * pTo,
106 cppu::helper::purpenv::ProbeFun * probeFun,
107 void * pProbeContext
109 : m_from (pFrom),
110 m_to (pTo),
111 m_nCount (1),
112 m_probeFun(probeFun),
113 m_pContext(pProbeContext)
115 SAL_INFO("cppu.purpenv", "LIFE: Mapping::Mapping(uno_Environment * pFrom, uno_Environment * pTo -> " << this);
117 uno_Mapping::acquire = s_acquire;
118 uno_Mapping::release = s_release;
119 uno_Mapping::mapInterface = s_mapInterface;
122 Mapping::~Mapping()
124 SAL_INFO("cppu.purpenv", "LIFE: Mapping:~Mapping() -> " << this);
127 void Mapping::mapInterface(
128 uno_Interface ** ppOut,
129 uno_Interface * pUnoI,
130 typelib_InterfaceTypeDescription * pTypeDescr)
132 assert(ppOut && pTypeDescr);
133 if (*ppOut)
135 (*ppOut)->release(*ppOut);
136 *ppOut = nullptr;
139 if (!pUnoI)
140 return;
142 // get object id of uno interface to be wrapped
143 // need to enter environment because of potential "queryInterface" call
144 rtl_uString * pOId = nullptr;
145 uno_Environment_invoke(m_from.get(), s_getIdentifier_v, m_from.get(), &pOId, pUnoI);
146 OSL_ASSERT(pOId);
148 // try to get any known interface from target environment
149 m_to.get()->pExtEnv->getRegisteredInterface(m_to.get()->pExtEnv, reinterpret_cast<void **>(ppOut), pOId, pTypeDescr);
151 if (!*ppOut) // not yet there, register new proxy interface
153 // try to publish a new proxy (ref count initially 1)
154 uno_Interface * pProxy = new Proxy(this,
155 m_from.get(),
156 m_to.get(),
157 pUnoI,
158 pTypeDescr,
159 pOId,
160 m_probeFun,
161 m_pContext);
163 // proxy may be exchanged during registration
164 m_to.get()->pExtEnv->registerProxyInterface(m_to.get()->pExtEnv,
165 reinterpret_cast<void **>(&pProxy),
166 Proxy_free,
167 pOId,
168 pTypeDescr);
170 *ppOut = pProxy;
173 rtl_uString_release(pOId);
176 void Mapping::acquire()
178 if (osl_atomic_increment(&m_nCount) == 1)
180 uno_Mapping * pMapping = this;
182 ::uno_registerMapping(&pMapping, s_free, m_from.get(), m_to.get(), nullptr);
186 void Mapping::release()
188 if (osl_atomic_decrement(&m_nCount) == 0)
189 ::uno_revokeMapping(this);
193 namespace cppu::helper::purpenv {
195 void createMapping(uno_Mapping ** ppMapping,
196 uno_Environment * pFrom,
197 uno_Environment * pTo,
198 ProbeFun * probeFun,
199 void * pContext
202 *ppMapping = new Mapping(pFrom, pTo, probeFun, pContext);
204 ::uno_registerMapping(ppMapping, s_free, pFrom, pTo, nullptr);
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */