tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / cppu / source / helper / purpenv / helper_purpenv_Environment.cxx
blob50e6eba1c3c45fa0d33c3d241b91a35c707f36c3
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/Environment.hxx>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
25 #include <uno/lbnames.h>
26 #include <cppu/Enterable.hxx>
28 #include <typelib/typedescription.h>
29 #include <osl/interlck.h>
30 #include <memory>
32 extern "C" {
33 typedef void EnvFun_P (uno_Environment *);
34 typedef void EnvFun_PP_P(uno_Environment ** ppHardEnv, uno_Environment *);
35 typedef void ExtEnv_registerProxyInterface (uno_ExtEnvironment *,
36 void ** ppProxy,
37 uno_freeProxyFunc freeProxy,
38 rtl_uString * pOId,
39 typelib_InterfaceTypeDescription * pTypeDescr);
40 typedef void ExtEnv_revokeInterface (uno_ExtEnvironment *,
41 void * pInterface);
42 typedef void ExtEnv_getObjectIdentifier (uno_ExtEnvironment *,
43 rtl_uString **,
44 void *);
45 typedef void ExtEnv_getRegisteredInterface (uno_ExtEnvironment *,
46 void **,
47 rtl_uString *,
48 typelib_InterfaceTypeDescription *);
49 typedef void ExtEnv_getRegisteredInterfaces(uno_ExtEnvironment *,
50 void *** pppInterfaces,
51 sal_Int32 * pnLen,
52 uno_memAlloc memAlloc);
53 typedef void ExtEnv_computeObjectIdentifier(uno_ExtEnvironment *,
54 rtl_uString ** ppOId,
55 void * pInterface);
56 typedef void ExtEnv_acquireInterface (uno_ExtEnvironment *,
57 void * pInterface);
58 typedef void ExtEnv_releaseInterface (uno_ExtEnvironment *,
59 void * pInterface);
62 namespace {
64 class Base : public cppu::Enterable
66 public:
67 explicit Base(uno_Environment * pEnv, cppu::Enterable * pEnterable);
69 void acquireWeak();
70 void releaseWeak();
71 void harden (uno_Environment ** ppHardEnv);
72 void acquire();
73 void release();
75 void registerProxyInterface (void ** ppProxy,
76 uno_freeProxyFunc freeProxy,
77 OUString const & oid,
78 typelib_InterfaceTypeDescription * pTypeDescr);
79 void revokeInterface (void * pInterface);
80 void getObjectIdentifier (void * pInterface,
81 OUString * pOid);
82 void getRegisteredInterface (void **,
83 OUString const & oid,
84 typelib_InterfaceTypeDescription *);
85 void getRegisteredInterfaces(void ***,
86 sal_Int32 * pnLen,
87 uno_memAlloc memAlloc);
88 void computeObjectIdentifier(void * pInterface,
89 OUString * pOid);
90 void acquireInterface (void * pInterface);
91 void releaseInterface (void * pInterface);
93 virtual void v_enter() override;
94 virtual void v_leave() override;
95 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam) override;
96 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam) override;
97 virtual bool v_isValid (OUString * pReason) override;
99 protected:
100 oslInterlockedCount m_nRef;
101 uno_Environment * m_pEnv;
102 std::unique_ptr<cppu::Enterable> m_pEnterable;
104 EnvFun_P * m_env_acquire;
105 EnvFun_P * m_env_release;
106 EnvFun_PP_P * m_env_harden;
107 EnvFun_P * m_env_acquireWeak;
108 EnvFun_P * m_env_releaseWeak;
110 ExtEnv_registerProxyInterface * m_env_registerProxyInterface;
111 ExtEnv_revokeInterface * m_env_revokeInterface;
112 ExtEnv_getObjectIdentifier * m_env_getObjectIdentifier;
113 ExtEnv_getRegisteredInterface * m_env_getRegisteredInterface;
114 ExtEnv_getRegisteredInterfaces * m_env_getRegisteredInterfaces;
115 ExtEnv_computeObjectIdentifier * m_env_computeObjectIdentifier;
116 ExtEnv_acquireInterface * m_env_acquireInterface;
117 ExtEnv_releaseInterface * m_env_releaseInterface;
119 virtual ~Base() override;
124 extern "C" {
125 static void s_acquire(uno_Environment * pEnv) //SAL_THROW_EXTERN_C()
127 Base * pBase = static_cast<Base *>(pEnv->pReserved);
128 pBase->acquire();
131 static void s_release(uno_Environment * pEnv) noexcept
133 Base * pBase = static_cast<Base *>(pEnv->pReserved);
134 pBase->release();
137 static void s_harden(uno_Environment ** ppHardEnv, uno_Environment * pEnv) noexcept
139 Base * pBase = static_cast<Base *>(pEnv->pReserved);
140 pBase->harden(ppHardEnv);
143 static void s_acquireWeak(uno_Environment * pEnv) noexcept
145 Base * pBase = static_cast<Base *>(pEnv->pReserved);
146 pBase->acquireWeak();
149 static void s_releaseWeak(uno_Environment * pEnv) noexcept
151 Base * pBase = static_cast<Base *>(pEnv->pReserved);
152 pBase->releaseWeak();
156 static void s_registerProxyInterface(uno_ExtEnvironment * pExtEnv,
157 void ** ppProxy,
158 uno_freeProxyFunc freeProxy,
159 rtl_uString * pOId,
160 typelib_InterfaceTypeDescription * pTypeDescr)
162 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
163 pBase->registerProxyInterface(ppProxy, freeProxy, pOId, pTypeDescr);
166 static void s_revokeInterface(uno_ExtEnvironment * pExtEnv, void * pInterface)
168 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
169 pBase->revokeInterface(pInterface);
172 static void s_getObjectIdentifier(uno_ExtEnvironment * pExtEnv,
173 rtl_uString ** ppOId,
174 void * pInterface)
176 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
177 pBase->getObjectIdentifier(pInterface, reinterpret_cast<OUString *>(ppOId));
180 static void s_getRegisteredInterface(uno_ExtEnvironment * pExtEnv,
181 void ** ppInterface,
182 rtl_uString * pOId,
183 typelib_InterfaceTypeDescription * pTypeDescr)
185 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
186 pBase->getRegisteredInterface(ppInterface, pOId, pTypeDescr);
189 static void s_getRegisteredInterfaces(uno_ExtEnvironment * pExtEnv,
190 void *** pppInterface,
191 sal_Int32 * pnLen,
192 uno_memAlloc memAlloc)
194 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
195 pBase->getRegisteredInterfaces(pppInterface, pnLen, memAlloc);
198 static void s_computeObjectIdentifier(uno_ExtEnvironment * pExtEnv,
199 rtl_uString ** ppOId,
200 void * pInterface)
202 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
203 pBase->computeObjectIdentifier(pInterface, reinterpret_cast<OUString *>(ppOId));
206 static void s_acquireInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
207 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
208 pBase->acquireInterface(pInterface);
211 static void s_releaseInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
212 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
213 pBase->releaseInterface(pInterface);
218 Base::Base(uno_Environment * pEnv, cppu::Enterable * pEnterable)
219 :m_nRef(1),
220 m_pEnv(pEnv),
221 m_pEnterable (pEnterable),
222 m_env_acquire (pEnv->acquire),
223 m_env_release (pEnv->release),
224 m_env_harden (pEnv->harden),
225 m_env_acquireWeak(pEnv->acquireWeak),
226 m_env_releaseWeak(pEnv->releaseWeak),
227 m_env_registerProxyInterface (pEnv->pExtEnv->registerProxyInterface),
228 m_env_revokeInterface (pEnv->pExtEnv->revokeInterface),
229 m_env_getObjectIdentifier (pEnv->pExtEnv->getObjectIdentifier),
230 m_env_getRegisteredInterface (pEnv->pExtEnv->getRegisteredInterface),
231 m_env_getRegisteredInterfaces(pEnv->pExtEnv->getRegisteredInterfaces),
232 m_env_computeObjectIdentifier(pEnv->pExtEnv->computeObjectIdentifier),
233 m_env_acquireInterface (pEnv->pExtEnv->acquireInterface),
234 m_env_releaseInterface (pEnv->pExtEnv->releaseInterface)
236 SAL_INFO("cppu.purpenv", "LIFE: cppu::helper::purpenv::Base::Base(uno_Environment * pEnv) -> " << this);
237 OSL_ENSURE(
238 rtl_ustr_ascii_compare_WithLength(pEnv->pTypeName->buffer, rtl_str_getLength(UNO_LB_UNO), UNO_LB_UNO)
239 == 0,
240 "### wrong environment type!");
242 pEnv->acquire = s_acquire;
243 pEnv->release = s_release;
244 pEnv->harden = s_harden;
245 pEnv->acquireWeak = s_acquireWeak;
246 pEnv->releaseWeak = s_releaseWeak;
248 pEnv->pExtEnv->registerProxyInterface = s_registerProxyInterface;
249 pEnv->pExtEnv->revokeInterface = s_revokeInterface;
250 pEnv->pExtEnv->getObjectIdentifier = s_getObjectIdentifier;
251 pEnv->pExtEnv->getRegisteredInterface = s_getRegisteredInterface;
252 pEnv->pExtEnv->getRegisteredInterfaces = s_getRegisteredInterfaces;
253 pEnv->pExtEnv->computeObjectIdentifier = s_computeObjectIdentifier;
254 pEnv->pExtEnv->acquireInterface = s_acquireInterface;
255 pEnv->pExtEnv->releaseInterface = s_releaseInterface;
257 pEnv->pReserved = this;
260 Base::~Base()
262 SAL_INFO("cppu.purpenv", "LIFE: cppu::helper::purpenv::Base::~Base() -> " << this);
264 m_pEnv->acquire = m_env_acquire;
265 m_pEnv->release = m_env_release;
266 m_pEnv->harden = m_env_harden;
267 m_pEnv->acquireWeak = m_env_acquireWeak;
268 m_pEnv->releaseWeak = m_env_releaseWeak;
270 m_pEnv->pReserved = nullptr;
272 m_pEnterable.reset();
273 m_pEnv->release(m_pEnv);
276 void Base::acquire()
278 m_env_acquire(m_pEnv);
280 osl_atomic_increment(&m_nRef);
283 void Base::release()
285 if (osl_atomic_decrement(&m_nRef) == 0)
286 delete this;
288 else
289 m_env_release(m_pEnv);
292 void Base::harden(uno_Environment ** ppHardEnv)
294 m_env_harden(ppHardEnv, m_pEnv);
295 osl_atomic_increment(&m_nRef);
298 void Base::acquireWeak()
300 m_env_acquireWeak(m_pEnv);
303 void Base::releaseWeak()
305 m_env_releaseWeak(m_pEnv);
309 extern "C" { static void s_registerProxyInterface_v(va_list * pParam)
311 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
312 void ** ppProxy = va_arg(*pParam, void **);
313 uno_freeProxyFunc freeProxy = va_arg(*pParam, uno_freeProxyFunc);
314 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
315 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
316 ExtEnv_registerProxyInterface * pRegisterProxyInterface
317 = va_arg(*pParam, ExtEnv_registerProxyInterface *);
319 pRegisterProxyInterface(pExtEnv, ppProxy, freeProxy, pOId, pTypeDescr);
322 void Base::registerProxyInterface(void ** ppProxy,
323 uno_freeProxyFunc freeProxy,
324 OUString const & oid,
325 typelib_InterfaceTypeDescription * pTypeDescr)
327 uno_Environment_invoke(m_pEnv,
328 s_registerProxyInterface_v,
329 m_pEnv->pExtEnv,
330 ppProxy,
331 freeProxy,
332 oid.pData,
333 pTypeDescr,
334 m_env_registerProxyInterface);
338 extern "C" { static void s_revokeInterface_v(va_list * pParam)
340 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
341 void * pInterface = va_arg(*pParam, void *);
342 ExtEnv_revokeInterface * pRevokeInterface = va_arg(*pParam, ExtEnv_revokeInterface *);
344 pRevokeInterface(pExtEnv, pInterface);
347 void Base::revokeInterface(void * pInterface)
349 uno_Environment_invoke(m_pEnv,
350 s_revokeInterface_v,
351 m_pEnv->pExtEnv,
352 pInterface,
353 m_env_revokeInterface);
357 extern "C" { static void s_getObjectIdentifier_v(va_list * pParam)
359 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
360 void * pInterface = va_arg(*pParam, void *);
361 OUString * pOId = va_arg(*pParam, OUString *);
362 ExtEnv_getObjectIdentifier * pGetObjectIdentifier
363 = va_arg(*pParam, ExtEnv_getObjectIdentifier *);
365 pGetObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
368 void Base::getObjectIdentifier(void * pInterface, OUString * pOid)
370 uno_Environment_invoke(m_pEnv,
371 s_getObjectIdentifier_v,
372 m_pEnv->pExtEnv,
373 pInterface,
374 pOid,
375 m_env_getObjectIdentifier);
379 extern "C" { static void s_getRegisteredInterface_v(va_list * pParam)
381 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
382 void ** ppInterface = va_arg(*pParam, void **);
383 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
384 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
385 ExtEnv_getRegisteredInterface * pGetRegisteredInterface
386 = va_arg(*pParam, ExtEnv_getRegisteredInterface *);
388 pGetRegisteredInterface(pExtEnv, ppInterface, pOId, pTypeDescr);
391 void Base::getRegisteredInterface(void ** ppInterface,
392 OUString const & oid,
393 typelib_InterfaceTypeDescription * pTypeDescr)
395 uno_Environment_invoke(m_pEnv,
396 s_getRegisteredInterface_v,
397 m_pEnv->pExtEnv,
398 ppInterface,
399 oid.pData,
400 pTypeDescr,
401 m_env_getRegisteredInterface);
405 extern "C" { static void s_getRegisteredInterfaces_v(va_list * pParam)
407 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
408 void *** pppInterface = va_arg(*pParam, void ***);
409 sal_Int32 * pnLen = va_arg(*pParam, sal_Int32 *);
410 uno_memAlloc memAlloc = va_arg(*pParam, uno_memAlloc);
411 ExtEnv_getRegisteredInterfaces * pGetRegisteredInterfaces
412 = va_arg(*pParam, ExtEnv_getRegisteredInterfaces *);
414 pGetRegisteredInterfaces(pExtEnv, pppInterface, pnLen, memAlloc);
417 void Base::getRegisteredInterfaces(void *** pppInterface,
418 sal_Int32 * pnLen,
419 uno_memAlloc memAlloc)
421 uno_Environment_invoke(m_pEnv,
422 s_getRegisteredInterfaces_v,
423 m_pEnv->pExtEnv,
424 pppInterface,
425 pnLen,
426 memAlloc,
427 m_env_getRegisteredInterfaces);
431 extern "C" { static void s_computeObjectIdentifier_v(va_list * pParam)
433 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
434 void * pInterface = va_arg(*pParam, void *);
435 OUString * pOId = va_arg(*pParam, OUString *);
436 ExtEnv_computeObjectIdentifier * pComputeObjectIdentifier
437 = va_arg(*pParam, ExtEnv_computeObjectIdentifier *);
439 pComputeObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
442 void Base::computeObjectIdentifier(void * pInterface, OUString * pOid)
444 uno_Environment_invoke(m_pEnv,
445 s_computeObjectIdentifier_v,
446 m_pEnv->pExtEnv,
447 pInterface,
448 pOid,
449 m_env_computeObjectIdentifier);
453 extern "C" { static void s_acquireInterface_v(va_list * pParam)
455 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
456 void * pInterface = va_arg(*pParam, void *);
457 ExtEnv_acquireInterface * pAcquireInterface
458 = va_arg(*pParam, ExtEnv_acquireInterface *);
460 pAcquireInterface(pExtEnv, pInterface);
463 void Base::acquireInterface(void * pInterface)
465 uno_Environment_invoke(m_pEnv, s_acquireInterface_v, m_pEnv->pExtEnv, pInterface, m_env_acquireInterface);
469 extern "C" { static void s_releaseInterface_v(va_list * pParam)
471 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
472 void * pInterface = va_arg(*pParam, void *);
473 ExtEnv_releaseInterface * pReleaseInterface
474 = va_arg(*pParam, ExtEnv_releaseInterface *);
476 pReleaseInterface(pExtEnv, pInterface);
479 void Base::releaseInterface(void * pInterface)
481 uno_Environment_invoke(m_pEnv,
482 s_releaseInterface_v,
483 m_pEnv->pExtEnv,
484 pInterface,
485 m_env_releaseInterface);
488 void Base::v_enter()
490 m_pEnterable->enter();
493 void Base::v_leave()
495 m_pEnterable->leave();
498 void Base::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
500 m_pEnterable->callInto_v(pCallee, pParam);
503 void Base::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
505 m_pEnterable->callOut_v(pCallee, pParam);
508 bool Base::v_isValid(OUString * pReason)
510 return m_pEnterable->isValid(pReason);
513 namespace cppu::helper::purpenv {
515 void Environment_initWithEnterable(uno_Environment * pEnvironment, cppu::Enterable * pEnterable)
517 new Base(pEnvironment, pEnterable);
522 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */