Bump version to 6.4-15
[LibreOffice.git] / cppu / source / helper / purpenv / helper_purpenv_Environment.cxx
blobceeee6eb0deb4490437917ed7f20fd94df40ea3d
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 class Base : public cppu::Enterable
64 public:
65 explicit Base(uno_Environment * pEnv, cppu::Enterable * pEnterable);
67 void acquireWeak();
68 void releaseWeak();
69 void harden (uno_Environment ** ppHardEnv);
70 void acquire();
71 void release();
73 void registerProxyInterface (void ** ppProxy,
74 uno_freeProxyFunc freeProxy,
75 OUString const & oid,
76 typelib_InterfaceTypeDescription * pTypeDescr);
77 void revokeInterface (void * pInterface);
78 void getObjectIdentifier (void * pInterface,
79 OUString * pOid);
80 void getRegisteredInterface (void **,
81 OUString const & oid,
82 typelib_InterfaceTypeDescription *);
83 void getRegisteredInterfaces(void ***,
84 sal_Int32 * pnLen,
85 uno_memAlloc memAlloc);
86 void computeObjectIdentifier(void * pInterface,
87 OUString * pOid);
88 void acquireInterface (void * pInterface);
89 void releaseInterface (void * pInterface);
91 virtual void v_enter() override;
92 virtual void v_leave() override;
93 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam) override;
94 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam) override;
95 virtual bool v_isValid (OUString * pReason) override;
97 protected:
98 oslInterlockedCount m_nRef;
99 uno_Environment * m_pEnv;
100 std::unique_ptr<cppu::Enterable> m_pEnterable;
102 EnvFun_P * m_env_acquire;
103 EnvFun_P * m_env_release;
104 EnvFun_PP_P * m_env_harden;
105 EnvFun_P * m_env_acquireWeak;
106 EnvFun_P * m_env_releaseWeak;
108 ExtEnv_registerProxyInterface * m_env_registerProxyInterface;
109 ExtEnv_revokeInterface * m_env_revokeInterface;
110 ExtEnv_getObjectIdentifier * m_env_getObjectIdentifier;
111 ExtEnv_getRegisteredInterface * m_env_getRegisteredInterface;
112 ExtEnv_getRegisteredInterfaces * m_env_getRegisteredInterfaces;
113 ExtEnv_computeObjectIdentifier * m_env_computeObjectIdentifier;
114 ExtEnv_acquireInterface * m_env_acquireInterface;
115 ExtEnv_releaseInterface * m_env_releaseInterface;
117 virtual ~Base() override;
120 extern "C" {
121 static void s_acquire(uno_Environment * pEnv) //SAL_THROW_EXTERN_C()
123 Base * pBase = static_cast<Base *>(pEnv->pReserved);
124 pBase->acquire();
127 static void s_release(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
129 Base * pBase = static_cast<Base *>(pEnv->pReserved);
130 pBase->release();
133 static void s_harden(uno_Environment ** ppHardEnv, uno_Environment * pEnv) SAL_THROW_EXTERN_C()
135 Base * pBase = static_cast<Base *>(pEnv->pReserved);
136 pBase->harden(ppHardEnv);
139 static void s_acquireWeak(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
141 Base * pBase = static_cast<Base *>(pEnv->pReserved);
142 pBase->acquireWeak();
145 static void s_releaseWeak(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
147 Base * pBase = static_cast<Base *>(pEnv->pReserved);
148 pBase->releaseWeak();
152 static void s_registerProxyInterface(uno_ExtEnvironment * pExtEnv,
153 void ** ppProxy,
154 uno_freeProxyFunc freeProxy,
155 rtl_uString * pOId,
156 typelib_InterfaceTypeDescription * pTypeDescr)
158 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
159 pBase->registerProxyInterface(ppProxy, freeProxy, pOId, pTypeDescr);
162 static void s_revokeInterface(uno_ExtEnvironment * pExtEnv, void * pInterface)
164 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
165 pBase->revokeInterface(pInterface);
168 static void s_getObjectIdentifier(uno_ExtEnvironment * pExtEnv,
169 rtl_uString ** ppOId,
170 void * pInterface)
172 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
173 pBase->getObjectIdentifier(pInterface, reinterpret_cast<OUString *>(ppOId));
176 static void s_getRegisteredInterface(uno_ExtEnvironment * pExtEnv,
177 void ** ppInterface,
178 rtl_uString * pOId,
179 typelib_InterfaceTypeDescription * pTypeDescr)
181 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
182 pBase->getRegisteredInterface(ppInterface, pOId, pTypeDescr);
185 static void s_getRegisteredInterfaces(uno_ExtEnvironment * pExtEnv,
186 void *** pppInterface,
187 sal_Int32 * pnLen,
188 uno_memAlloc memAlloc)
190 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
191 pBase->getRegisteredInterfaces(pppInterface, pnLen, memAlloc);
194 static void s_computeObjectIdentifier(uno_ExtEnvironment * pExtEnv,
195 rtl_uString ** ppOId,
196 void * pInterface)
198 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
199 pBase->computeObjectIdentifier(pInterface, reinterpret_cast<OUString *>(ppOId));
202 static void s_acquireInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
203 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
204 pBase->acquireInterface(pInterface);
207 static void s_releaseInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
208 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
209 pBase->releaseInterface(pInterface);
214 Base::Base(uno_Environment * pEnv, cppu::Enterable * pEnterable)
215 :m_nRef(1),
216 m_pEnv(pEnv),
217 m_pEnterable (pEnterable),
218 m_env_acquire (pEnv->acquire),
219 m_env_release (pEnv->release),
220 m_env_harden (pEnv->harden),
221 m_env_acquireWeak(pEnv->acquireWeak),
222 m_env_releaseWeak(pEnv->releaseWeak),
223 m_env_registerProxyInterface (pEnv->pExtEnv->registerProxyInterface),
224 m_env_revokeInterface (pEnv->pExtEnv->revokeInterface),
225 m_env_getObjectIdentifier (pEnv->pExtEnv->getObjectIdentifier),
226 m_env_getRegisteredInterface (pEnv->pExtEnv->getRegisteredInterface),
227 m_env_getRegisteredInterfaces(pEnv->pExtEnv->getRegisteredInterfaces),
228 m_env_computeObjectIdentifier(pEnv->pExtEnv->computeObjectIdentifier),
229 m_env_acquireInterface (pEnv->pExtEnv->acquireInterface),
230 m_env_releaseInterface (pEnv->pExtEnv->releaseInterface)
232 SAL_INFO("cppu.purpenv", "LIFE: cppu::helper::purpenv::Base::Base(uno_Environment * pEnv) -> " << this);
233 OSL_ENSURE(
234 rtl_ustr_ascii_compare_WithLength(pEnv->pTypeName->buffer, rtl_str_getLength(UNO_LB_UNO), UNO_LB_UNO)
235 == 0,
236 "### wrong environment type!");
238 pEnv->acquire = s_acquire;
239 pEnv->release = s_release;
240 pEnv->harden = s_harden;
241 pEnv->acquireWeak = s_acquireWeak;
242 pEnv->releaseWeak = s_releaseWeak;
244 pEnv->pExtEnv->registerProxyInterface = s_registerProxyInterface;
245 pEnv->pExtEnv->revokeInterface = s_revokeInterface;
246 pEnv->pExtEnv->getObjectIdentifier = s_getObjectIdentifier;
247 pEnv->pExtEnv->getRegisteredInterface = s_getRegisteredInterface;
248 pEnv->pExtEnv->getRegisteredInterfaces = s_getRegisteredInterfaces;
249 pEnv->pExtEnv->computeObjectIdentifier = s_computeObjectIdentifier;
250 pEnv->pExtEnv->acquireInterface = s_acquireInterface;
251 pEnv->pExtEnv->releaseInterface = s_releaseInterface;
253 pEnv->pReserved = this;
256 Base::~Base()
258 SAL_INFO("cppu.purpenv", "LIFE: cppu::helper::purpenv::Base::~Base() -> " << this);
260 m_pEnv->acquire = m_env_acquire;
261 m_pEnv->release = m_env_release;
262 m_pEnv->harden = m_env_harden;
263 m_pEnv->acquireWeak = m_env_acquireWeak;
264 m_pEnv->releaseWeak = m_env_releaseWeak;
266 m_pEnv->pReserved = nullptr;
268 m_pEnterable.reset();
269 m_pEnv->release(m_pEnv);
272 void Base::acquire()
274 m_env_acquire(m_pEnv);
276 osl_atomic_increment(&m_nRef);
279 void Base::release()
281 if (osl_atomic_decrement(&m_nRef) == 0)
282 delete this;
284 else
285 m_env_release(m_pEnv);
288 void Base::harden(uno_Environment ** ppHardEnv)
290 m_env_harden(ppHardEnv, m_pEnv);
291 osl_atomic_increment(&m_nRef);
294 void Base::acquireWeak()
296 m_env_acquireWeak(m_pEnv);
299 void Base::releaseWeak()
301 m_env_releaseWeak(m_pEnv);
305 extern "C" { static void s_registerProxyInterface_v(va_list * pParam)
307 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
308 void ** ppProxy = va_arg(*pParam, void **);
309 uno_freeProxyFunc freeProxy = va_arg(*pParam, uno_freeProxyFunc);
310 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
311 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
312 ExtEnv_registerProxyInterface * pRegisterProxyInterface
313 = va_arg(*pParam, ExtEnv_registerProxyInterface *);
315 pRegisterProxyInterface(pExtEnv, ppProxy, freeProxy, pOId, pTypeDescr);
318 void Base::registerProxyInterface(void ** ppProxy,
319 uno_freeProxyFunc freeProxy,
320 OUString const & oid,
321 typelib_InterfaceTypeDescription * pTypeDescr)
323 uno_Environment_invoke(m_pEnv,
324 s_registerProxyInterface_v,
325 m_pEnv->pExtEnv,
326 ppProxy,
327 freeProxy,
328 oid.pData,
329 pTypeDescr,
330 m_env_registerProxyInterface);
334 extern "C" { static void s_revokeInterface_v(va_list * pParam)
336 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
337 void * pInterface = va_arg(*pParam, void *);
338 ExtEnv_revokeInterface * pRevokeInterface = va_arg(*pParam, ExtEnv_revokeInterface *);
340 pRevokeInterface(pExtEnv, pInterface);
343 void Base::revokeInterface(void * pInterface)
345 uno_Environment_invoke(m_pEnv,
346 s_revokeInterface_v,
347 m_pEnv->pExtEnv,
348 pInterface,
349 m_env_revokeInterface);
353 extern "C" { static void s_getObjectIdentifier_v(va_list * pParam)
355 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
356 void * pInterface = va_arg(*pParam, void *);
357 OUString * pOId = va_arg(*pParam, OUString *);
358 ExtEnv_getObjectIdentifier * pGetObjectIdentifier
359 = va_arg(*pParam, ExtEnv_getObjectIdentifier *);
361 pGetObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
364 void Base::getObjectIdentifier(void * pInterface, OUString * pOid)
366 uno_Environment_invoke(m_pEnv,
367 s_getObjectIdentifier_v,
368 m_pEnv->pExtEnv,
369 pInterface,
370 pOid,
371 m_env_getObjectIdentifier);
375 extern "C" { static void s_getRegisteredInterface_v(va_list * pParam)
377 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
378 void ** ppInterface = va_arg(*pParam, void **);
379 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
380 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
381 ExtEnv_getRegisteredInterface * pGetRegisteredInterface
382 = va_arg(*pParam, ExtEnv_getRegisteredInterface *);
384 pGetRegisteredInterface(pExtEnv, ppInterface, pOId, pTypeDescr);
387 void Base::getRegisteredInterface(void ** ppInterface,
388 OUString const & oid,
389 typelib_InterfaceTypeDescription * pTypeDescr)
391 uno_Environment_invoke(m_pEnv,
392 s_getRegisteredInterface_v,
393 m_pEnv->pExtEnv,
394 ppInterface,
395 oid.pData,
396 pTypeDescr,
397 m_env_getRegisteredInterface);
401 extern "C" { static void s_getRegisteredInterfaces_v(va_list * pParam)
403 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
404 void *** pppInterface = va_arg(*pParam, void ***);
405 sal_Int32 * pnLen = va_arg(*pParam, sal_Int32 *);
406 uno_memAlloc memAlloc = va_arg(*pParam, uno_memAlloc);
407 ExtEnv_getRegisteredInterfaces * pGetRegisteredInterfaces
408 = va_arg(*pParam, ExtEnv_getRegisteredInterfaces *);
410 pGetRegisteredInterfaces(pExtEnv, pppInterface, pnLen, memAlloc);
413 void Base::getRegisteredInterfaces(void *** pppInterface,
414 sal_Int32 * pnLen,
415 uno_memAlloc memAlloc)
417 uno_Environment_invoke(m_pEnv,
418 s_getRegisteredInterfaces_v,
419 m_pEnv->pExtEnv,
420 pppInterface,
421 pnLen,
422 memAlloc,
423 m_env_getRegisteredInterfaces);
427 extern "C" { static void s_computeObjectIdentifier_v(va_list * pParam)
429 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
430 void * pInterface = va_arg(*pParam, void *);
431 OUString * pOId = va_arg(*pParam, OUString *);
432 ExtEnv_computeObjectIdentifier * pComputeObjectIdentifier
433 = va_arg(*pParam, ExtEnv_computeObjectIdentifier *);
435 pComputeObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
438 void Base::computeObjectIdentifier(void * pInterface, OUString * pOid)
440 uno_Environment_invoke(m_pEnv,
441 s_computeObjectIdentifier_v,
442 m_pEnv->pExtEnv,
443 pInterface,
444 pOid,
445 m_env_computeObjectIdentifier);
449 extern "C" { static void s_acquireInterface_v(va_list * pParam)
451 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
452 void * pInterface = va_arg(*pParam, void *);
453 ExtEnv_acquireInterface * pAcquireInterface
454 = va_arg(*pParam, ExtEnv_acquireInterface *);
456 pAcquireInterface(pExtEnv, pInterface);
459 void Base::acquireInterface(void * pInterface)
461 uno_Environment_invoke(m_pEnv, s_acquireInterface_v, m_pEnv->pExtEnv, pInterface, m_env_acquireInterface);
465 extern "C" { static void s_releaseInterface_v(va_list * pParam)
467 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
468 void * pInterface = va_arg(*pParam, void *);
469 ExtEnv_releaseInterface * pReleaseInterface
470 = va_arg(*pParam, ExtEnv_releaseInterface *);
472 pReleaseInterface(pExtEnv, pInterface);
475 void Base::releaseInterface(void * pInterface)
477 uno_Environment_invoke(m_pEnv,
478 s_releaseInterface_v,
479 m_pEnv->pExtEnv,
480 pInterface,
481 m_env_releaseInterface);
484 void Base::v_enter()
486 m_pEnterable->enter();
489 void Base::v_leave()
491 m_pEnterable->leave();
494 void Base::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
496 m_pEnterable->callInto_v(pCallee, pParam);
499 void Base::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
501 m_pEnterable->callOut_v(pCallee, pParam);
504 bool Base::v_isValid(OUString * pReason)
506 return m_pEnterable->isValid(pReason);
509 namespace cppu { namespace helper { namespace purpenv {
511 void Environment_initWithEnterable(uno_Environment * pEnvironment, cppu::Enterable * pEnterable)
513 new Base(pEnvironment, pEnterable);
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */