1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
*,
37 uno_freeProxyFunc freeProxy
,
39 typelib_InterfaceTypeDescription
* pTypeDescr
);
40 typedef void ExtEnv_revokeInterface (uno_ExtEnvironment
*,
42 typedef void ExtEnv_getObjectIdentifier (uno_ExtEnvironment
*,
45 typedef void ExtEnv_getRegisteredInterface (uno_ExtEnvironment
*,
48 typelib_InterfaceTypeDescription
*);
49 typedef void ExtEnv_getRegisteredInterfaces(uno_ExtEnvironment
*,
50 void *** pppInterfaces
,
52 uno_memAlloc memAlloc
);
53 typedef void ExtEnv_computeObjectIdentifier(uno_ExtEnvironment
*,
56 typedef void ExtEnv_acquireInterface (uno_ExtEnvironment
*,
58 typedef void ExtEnv_releaseInterface (uno_ExtEnvironment
*,
64 class Base
: public cppu::Enterable
67 explicit Base(uno_Environment
* pEnv
, cppu::Enterable
* pEnterable
);
71 void harden (uno_Environment
** ppHardEnv
);
75 void registerProxyInterface (void ** ppProxy
,
76 uno_freeProxyFunc freeProxy
,
78 typelib_InterfaceTypeDescription
* pTypeDescr
);
79 void revokeInterface (void * pInterface
);
80 void getObjectIdentifier (void * pInterface
,
82 void getRegisteredInterface (void **,
84 typelib_InterfaceTypeDescription
*);
85 void getRegisteredInterfaces(void ***,
87 uno_memAlloc memAlloc
);
88 void computeObjectIdentifier(void * pInterface
,
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
;
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
;
125 static void s_acquire(uno_Environment
* pEnv
) //SAL_THROW_EXTERN_C()
127 Base
* pBase
= static_cast<Base
*>(pEnv
->pReserved
);
131 static void s_release(uno_Environment
* pEnv
) noexcept
133 Base
* pBase
= static_cast<Base
*>(pEnv
->pReserved
);
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
,
158 uno_freeProxyFunc freeProxy
,
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
,
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
,
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
,
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
,
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
)
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);
238 rtl_ustr_ascii_compare_WithLength(pEnv
->pTypeName
->buffer
, rtl_str_getLength(UNO_LB_UNO
), UNO_LB_UNO
)
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;
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
);
278 m_env_acquire(m_pEnv
);
280 osl_atomic_increment(&m_nRef
);
285 if (osl_atomic_decrement(&m_nRef
) == 0)
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
,
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
,
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
,
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
,
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
,
419 uno_memAlloc memAlloc
)
421 uno_Environment_invoke(m_pEnv
,
422 s_getRegisteredInterfaces_v
,
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
,
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
,
485 m_env_releaseInterface
);
490 m_pEnterable
->enter();
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: */