merged tag ooo/DEV300_m102
[LibreOffice.git] / cppu / source / helper / purpenv / helper_purpenv_Environment.cxx
blob0fbed67399512e9001a466ab4cf05d814833a731
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppu.hxx"
31 #include "cppu/helper/purpenv/Environment.hxx"
33 #include "osl/diagnose.h"
34 #include "uno/lbnames.h"
36 #include "typelib/typedescription.h"
37 #include "osl/interlck.h"
39 #ifdef debug
40 # define LOG_LIFECYCLE_cppu_helper_purpenv_Base
41 #endif
43 #ifdef LOG_LIFECYCLE_cppu_helper_purpenv_Base
44 # include <iostream>
45 # define LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(x) x
47 #else
48 # define LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(x)
50 #endif
53 extern "C" {
54 typedef void SAL_CALL EnvFun_P (uno_Environment *);
55 typedef void SAL_CALL EnvFun_PP_P(uno_Environment ** ppHardEnv, uno_Environment *);
56 typedef void SAL_CALL ExtEnv_registerProxyInterface (uno_ExtEnvironment *,
57 void ** ppProxy,
58 uno_freeProxyFunc freeProxy,
59 rtl_uString * pOId,
60 typelib_InterfaceTypeDescription * pTypeDescr);
61 typedef void SAL_CALL ExtEnv_revokeInterface (uno_ExtEnvironment *,
62 void * pInterface);
63 typedef void SAL_CALL ExtEnv_getObjectIdentifier (uno_ExtEnvironment *,
64 rtl_uString **,
65 void *);
66 typedef void SAL_CALL ExtEnv_getRegisteredInterface (uno_ExtEnvironment *,
67 void **,
68 rtl_uString *,
69 typelib_InterfaceTypeDescription *);
70 typedef void SAL_CALL ExtEnv_getRegisteredInterfaces(uno_ExtEnvironment *,
71 void *** pppInterfaces,
72 sal_Int32 * pnLen,
73 uno_memAlloc memAlloc);
74 typedef void SAL_CALL ExtEnv_computeObjectIdentifier(uno_ExtEnvironment *,
75 rtl_uString ** ppOId,
76 void * pInterface);
77 typedef void SAL_CALL ExtEnv_acquireInterface (uno_ExtEnvironment *,
78 void * pInterface);
79 typedef void SAL_CALL ExtEnv_releaseInterface (uno_ExtEnvironment *,
80 void * pInterface);
83 class Base : public cppu::Enterable
85 public:
86 explicit Base(uno_Environment * pEnv, cppu::Enterable * pEnterable);
88 void acquireWeak(void);
89 void releaseWeak(void);
90 void harden (uno_Environment ** ppHardEnv);
91 void acquire (void);
92 void release (void);
94 void registerProxyInterface (void ** ppProxy,
95 uno_freeProxyFunc freeProxy,
96 rtl::OUString const & oid,
97 typelib_InterfaceTypeDescription * pTypeDescr);
98 void revokeInterface (void * pInterface);
99 void getObjectIdentifier (void * pInterface,
100 rtl::OUString * pOid);
101 void getRegisteredInterface (void **,
102 rtl::OUString const & oid,
103 typelib_InterfaceTypeDescription *);
104 void getRegisteredInterfaces(void ***,
105 sal_Int32 * pnLen,
106 uno_memAlloc memAlloc);
107 void computeObjectIdentifier(void * pInterface,
108 rtl::OUString * pOid);
109 void acquireInterface (void * pInterface);
110 void releaseInterface (void * pInterface);
112 virtual void v_enter (void);
113 virtual void v_leave (void);
114 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
115 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
116 virtual int v_isValid (rtl::OUString * pReason);
118 protected:
119 oslInterlockedCount m_nRef;
120 uno_Environment * m_pEnv;
121 cppu::Enterable * m_pEnterable;
123 EnvFun_P * m_env_acquire;
124 EnvFun_P * m_env_release;
125 EnvFun_PP_P * m_env_harden;
126 EnvFun_P * m_env_acquireWeak;
127 EnvFun_P * m_env_releaseWeak;
129 ExtEnv_registerProxyInterface * m_env_registerProxyInterface;
130 ExtEnv_revokeInterface * m_env_revokeInterface;
131 ExtEnv_getObjectIdentifier * m_env_getObjectIdentifier;
132 ExtEnv_getRegisteredInterface * m_env_getRegisteredInterface;
133 ExtEnv_getRegisteredInterfaces * m_env_getRegisteredInterfaces;
134 ExtEnv_computeObjectIdentifier * m_env_computeObjectIdentifier;
135 ExtEnv_acquireInterface * m_env_acquireInterface;
136 ExtEnv_releaseInterface * m_env_releaseInterface;
138 virtual ~Base();
141 extern "C" {
142 static void SAL_CALL s_acquire(uno_Environment * pEnv) //SAL_THROW_EXTERN_C()
144 Base * pBase = static_cast<Base *>(pEnv->pReserved);
145 pBase->acquire();
148 static void SAL_CALL s_release(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
150 Base * pBase = static_cast<Base *>(pEnv->pReserved);
151 pBase->release();
154 static void SAL_CALL s_harden(uno_Environment ** ppHardEnv, uno_Environment * pEnv) SAL_THROW_EXTERN_C()
156 Base * pBase = static_cast<Base *>(pEnv->pReserved);
157 pBase->harden(ppHardEnv);
160 static void SAL_CALL s_acquireWeak(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
162 Base * pBase = static_cast<Base *>(pEnv->pReserved);
163 pBase->acquireWeak();
166 static void SAL_CALL s_releaseWeak(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
168 Base * pBase = static_cast<Base *>(pEnv->pReserved);
169 pBase->releaseWeak();
173 static void SAL_CALL s_registerProxyInterface(uno_ExtEnvironment * pExtEnv,
174 void ** ppProxy,
175 uno_freeProxyFunc freeProxy,
176 rtl_uString * pOId,
177 typelib_InterfaceTypeDescription * pTypeDescr)
179 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
180 pBase->registerProxyInterface(ppProxy, freeProxy, pOId, pTypeDescr);
183 static void SAL_CALL s_revokeInterface(uno_ExtEnvironment * pExtEnv, void * pInterface)
185 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
186 pBase->revokeInterface(pInterface);
189 static void SAL_CALL s_getObjectIdentifier(uno_ExtEnvironment * pExtEnv,
190 rtl_uString ** ppOId,
191 void * pInterface)
193 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
194 pBase->getObjectIdentifier(pInterface, reinterpret_cast<rtl::OUString *>(ppOId));
197 static void SAL_CALL s_getRegisteredInterface(uno_ExtEnvironment * pExtEnv,
198 void ** ppInterface,
199 rtl_uString * pOId,
200 typelib_InterfaceTypeDescription * pTypeDescr)
202 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
203 pBase->getRegisteredInterface(ppInterface, pOId, pTypeDescr);
206 static void SAL_CALL s_getRegisteredInterfaces(uno_ExtEnvironment * pExtEnv,
207 void *** pppInterface,
208 sal_Int32 * pnLen,
209 uno_memAlloc memAlloc)
211 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
212 pBase->getRegisteredInterfaces(pppInterface, pnLen, memAlloc);
215 static void SAL_CALL s_computeObjectIdentifier(uno_ExtEnvironment * pExtEnv,
216 rtl_uString ** ppOId,
217 void * pInterface)
219 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
220 pBase->computeObjectIdentifier(pInterface, reinterpret_cast<rtl::OUString *>(ppOId));
223 static void SAL_CALL s_acquireInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
224 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
225 pBase->acquireInterface(pInterface);
228 static void SAL_CALL s_releaseInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
229 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
230 pBase->releaseInterface(pInterface);
235 Base::Base(uno_Environment * pEnv, cppu::Enterable * pEnterable)
236 :m_nRef(1),
237 m_pEnv(pEnv),
238 m_pEnterable (pEnterable),
239 m_env_acquire (pEnv->acquire),
240 m_env_release (pEnv->release),
241 m_env_harden (pEnv->harden),
242 m_env_acquireWeak(pEnv->acquireWeak),
243 m_env_releaseWeak(pEnv->releaseWeak),
244 m_env_registerProxyInterface (pEnv->pExtEnv->registerProxyInterface),
245 m_env_revokeInterface (pEnv->pExtEnv->revokeInterface),
246 m_env_getObjectIdentifier (pEnv->pExtEnv->getObjectIdentifier),
247 m_env_getRegisteredInterface (pEnv->pExtEnv->getRegisteredInterface),
248 m_env_getRegisteredInterfaces(pEnv->pExtEnv->getRegisteredInterfaces),
249 m_env_computeObjectIdentifier(pEnv->pExtEnv->computeObjectIdentifier),
250 m_env_acquireInterface (pEnv->pExtEnv->acquireInterface),
251 m_env_releaseInterface (pEnv->pExtEnv->releaseInterface)
253 LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(fprintf(stderr, "LIFE: %s -> %p\n", "cppu::helper::purpenv::Base::Base(uno_Environment * pEnv)", this));
254 OSL_ENSURE(
255 rtl_ustr_ascii_compare_WithLength(pEnv->pTypeName->buffer, rtl_str_getLength(UNO_LB_UNO), UNO_LB_UNO)
256 == 0,
257 "### wrong environment type!");
259 pEnv->acquire = s_acquire;
260 pEnv->release = s_release;
261 pEnv->harden = s_harden;
262 pEnv->acquireWeak = s_acquireWeak;
263 pEnv->releaseWeak = s_releaseWeak;
265 pEnv->pExtEnv->registerProxyInterface = s_registerProxyInterface;
266 pEnv->pExtEnv->revokeInterface = s_revokeInterface;
267 pEnv->pExtEnv->getObjectIdentifier = s_getObjectIdentifier;
268 pEnv->pExtEnv->getRegisteredInterface = s_getRegisteredInterface;
269 pEnv->pExtEnv->getRegisteredInterfaces = s_getRegisteredInterfaces;
270 pEnv->pExtEnv->computeObjectIdentifier = s_computeObjectIdentifier;
271 pEnv->pExtEnv->acquireInterface = s_acquireInterface;
272 pEnv->pExtEnv->releaseInterface = s_releaseInterface;
274 pEnv->pReserved = this;
277 Base::~Base()
279 LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(fprintf(stderr, "LIFE: %s -> %p\n", "cppu::helper::purpenv::Base::~Base()", this));
281 m_pEnv->acquire = m_env_acquire;
282 m_pEnv->release = m_env_release;
283 m_pEnv->harden = m_env_harden;
284 m_pEnv->acquireWeak = m_env_acquireWeak;
285 m_pEnv->releaseWeak = m_env_releaseWeak;
287 m_pEnv->pReserved = NULL;
289 delete m_pEnterable;
290 m_pEnv->release(m_pEnv);
293 void Base::acquire(void)
295 m_env_acquire(m_pEnv);
297 osl_incrementInterlockedCount(&m_nRef);
300 void Base::release(void)
302 if (osl_decrementInterlockedCount(&m_nRef) == 0)
303 delete this;
305 else
306 m_env_release(m_pEnv);
309 void Base::harden(uno_Environment ** ppHardEnv)
311 m_env_harden(ppHardEnv, m_pEnv);
312 osl_incrementInterlockedCount(&m_nRef);
315 void Base::acquireWeak(void)
317 m_env_acquireWeak(m_pEnv);
320 void Base::releaseWeak(void)
322 m_env_releaseWeak(m_pEnv);
326 extern "C" { static void s_registerProxyInterface_v(va_list * pParam)
328 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
329 void ** ppProxy = va_arg(*pParam, void **);
330 uno_freeProxyFunc freeProxy = va_arg(*pParam, uno_freeProxyFunc);
331 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
332 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
333 ExtEnv_registerProxyInterface * pRegisterProxyInterface
334 = va_arg(*pParam, ExtEnv_registerProxyInterface *);
336 pRegisterProxyInterface(pExtEnv, ppProxy, freeProxy, pOId, pTypeDescr);
339 void Base::registerProxyInterface(void ** ppProxy,
340 uno_freeProxyFunc freeProxy,
341 rtl::OUString const & oid,
342 typelib_InterfaceTypeDescription * pTypeDescr)
344 uno_Environment_invoke(m_pEnv,
345 s_registerProxyInterface_v,
346 m_pEnv->pExtEnv,
347 ppProxy,
348 freeProxy,
349 oid.pData,
350 pTypeDescr,
351 m_env_registerProxyInterface);
355 extern "C" { static void s_revokeInterface_v(va_list * pParam)
357 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
358 void * pInterface = va_arg(*pParam, void *);
359 ExtEnv_revokeInterface * pRevokeInterface = va_arg(*pParam, ExtEnv_revokeInterface *);
361 pRevokeInterface(pExtEnv, pInterface);
364 void Base::revokeInterface(void * pInterface)
366 uno_Environment_invoke(m_pEnv,
367 s_revokeInterface_v,
368 m_pEnv->pExtEnv,
369 pInterface,
370 m_env_revokeInterface);
374 extern "C" { static void s_getObjectIdentifier_v(va_list * pParam)
376 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
377 void * pInterface = va_arg(*pParam, void *);
378 rtl::OUString * pOId = va_arg(*pParam, rtl::OUString *);
379 ExtEnv_getObjectIdentifier * pGetObjectIdentifier
380 = va_arg(*pParam, ExtEnv_getObjectIdentifier *);
382 pGetObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
385 void Base::getObjectIdentifier(void * pInterface, rtl::OUString * pOid)
387 uno_Environment_invoke(m_pEnv,
388 s_getObjectIdentifier_v,
389 m_pEnv->pExtEnv,
390 pInterface,
391 pOid,
392 m_env_getObjectIdentifier);
396 extern "C" { static void s_getRegisteredInterface_v(va_list * pParam)
398 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
399 void ** ppInterface = va_arg(*pParam, void **);
400 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
401 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
402 ExtEnv_getRegisteredInterface * pGetRegisteredInterface
403 = va_arg(*pParam, ExtEnv_getRegisteredInterface *);
405 pGetRegisteredInterface(pExtEnv, ppInterface, pOId, pTypeDescr);
408 void Base::getRegisteredInterface(void ** ppInterface,
409 rtl::OUString const & oid,
410 typelib_InterfaceTypeDescription * pTypeDescr)
412 uno_Environment_invoke(m_pEnv,
413 s_getRegisteredInterface_v,
414 m_pEnv->pExtEnv,
415 ppInterface,
416 oid.pData,
417 pTypeDescr,
418 m_env_getRegisteredInterface);
422 extern "C" { static void s_getRegisteredInterfaces_v(va_list * pParam)
424 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
425 void *** pppInterface = va_arg(*pParam, void ***);
426 sal_Int32 * pnLen = va_arg(*pParam, sal_Int32 *);
427 uno_memAlloc memAlloc = va_arg(*pParam, uno_memAlloc);
428 ExtEnv_getRegisteredInterfaces * pGetRegisteredInterfaces
429 = va_arg(*pParam, ExtEnv_getRegisteredInterfaces *);
431 pGetRegisteredInterfaces(pExtEnv, pppInterface, pnLen, memAlloc);
434 void Base::getRegisteredInterfaces(void *** pppInterface,
435 sal_Int32 * pnLen,
436 uno_memAlloc memAlloc)
438 uno_Environment_invoke(m_pEnv,
439 s_getRegisteredInterfaces_v,
440 m_pEnv->pExtEnv,
441 pppInterface,
442 pnLen,
443 memAlloc,
444 m_env_getRegisteredInterfaces);
448 extern "C" { static void s_computeObjectIdentifier_v(va_list * pParam)
450 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
451 void * pInterface = va_arg(*pParam, void *);
452 rtl::OUString * pOId = va_arg(*pParam, rtl::OUString *);
453 ExtEnv_computeObjectIdentifier * pComputeObjectIdentifier
454 = va_arg(*pParam, ExtEnv_computeObjectIdentifier *);
456 pComputeObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
459 void Base::computeObjectIdentifier(void * pInterface, rtl::OUString * pOid)
461 uno_Environment_invoke(m_pEnv,
462 s_computeObjectIdentifier_v,
463 m_pEnv->pExtEnv,
464 pInterface,
465 pOid,
466 m_env_computeObjectIdentifier);
470 extern "C" { static void s_acquireInterface_v(va_list * pParam)
472 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
473 void * pInterface = va_arg(*pParam, void *);
474 ExtEnv_acquireInterface * pAcquireInterface
475 = va_arg(*pParam, ExtEnv_acquireInterface *);
477 pAcquireInterface(pExtEnv, pInterface);
480 void Base::acquireInterface(void * pInterface)
482 uno_Environment_invoke(m_pEnv, s_acquireInterface_v, m_pEnv->pExtEnv, pInterface, m_env_acquireInterface);
486 extern "C" { static void s_releaseInterface_v(va_list * pParam)
488 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
489 void * pInterface = va_arg(*pParam, void *);
490 ExtEnv_releaseInterface * pReleaseInterface
491 = va_arg(*pParam, ExtEnv_releaseInterface *);
493 pReleaseInterface(pExtEnv, pInterface);
496 void Base::releaseInterface(void * pInterface)
498 uno_Environment_invoke(m_pEnv,
499 s_releaseInterface_v,
500 m_pEnv->pExtEnv,
501 pInterface,
502 m_env_releaseInterface);
505 void Base::v_enter(void)
507 m_pEnterable->enter();
510 void Base::v_leave(void)
512 m_pEnterable->leave();
515 void Base::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
517 m_pEnterable->callInto_v(pCallee, pParam);
520 void Base::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
522 m_pEnterable->callOut_v(pCallee, pParam);
525 int Base::v_isValid(rtl::OUString * pReason)
527 return m_pEnterable->isValid(pReason);
530 namespace cppu { namespace helper { namespace purpenv {
532 void Environment_initWithEnterable(uno_Environment * pEnvironment, cppu::Enterable * pEnterable)
534 new Base(pEnvironment, pEnterable);