Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / cppu / source / helper / purpenv / helper_purpenv_Environment.cxx
blob15946d7558deac4eebb690b0adc3827873297781
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 "uno/lbnames.h"
26 #include "typelib/typedescription.h"
27 #include "osl/interlck.h"
29 #ifdef debug
30 # define LOG_LIFECYCLE_cppu_helper_purpenv_Base
31 #endif
33 #ifdef LOG_LIFECYCLE_cppu_helper_purpenv_Base
34 # include <iostream>
35 # define LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(x) x
37 #else
38 # define LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(x)
40 #endif
43 extern "C" {
44 typedef void SAL_CALL EnvFun_P (uno_Environment *);
45 typedef void SAL_CALL EnvFun_PP_P(uno_Environment ** ppHardEnv, uno_Environment *);
46 typedef void SAL_CALL ExtEnv_registerProxyInterface (uno_ExtEnvironment *,
47 void ** ppProxy,
48 uno_freeProxyFunc freeProxy,
49 rtl_uString * pOId,
50 typelib_InterfaceTypeDescription * pTypeDescr);
51 typedef void SAL_CALL ExtEnv_revokeInterface (uno_ExtEnvironment *,
52 void * pInterface);
53 typedef void SAL_CALL ExtEnv_getObjectIdentifier (uno_ExtEnvironment *,
54 rtl_uString **,
55 void *);
56 typedef void SAL_CALL ExtEnv_getRegisteredInterface (uno_ExtEnvironment *,
57 void **,
58 rtl_uString *,
59 typelib_InterfaceTypeDescription *);
60 typedef void SAL_CALL ExtEnv_getRegisteredInterfaces(uno_ExtEnvironment *,
61 void *** pppInterfaces,
62 sal_Int32 * pnLen,
63 uno_memAlloc memAlloc);
64 typedef void SAL_CALL ExtEnv_computeObjectIdentifier(uno_ExtEnvironment *,
65 rtl_uString ** ppOId,
66 void * pInterface);
67 typedef void SAL_CALL ExtEnv_acquireInterface (uno_ExtEnvironment *,
68 void * pInterface);
69 typedef void SAL_CALL ExtEnv_releaseInterface (uno_ExtEnvironment *,
70 void * pInterface);
73 class Base : public cppu::Enterable
75 public:
76 explicit Base(uno_Environment * pEnv, cppu::Enterable * pEnterable);
78 void acquireWeak(void);
79 void releaseWeak(void);
80 void harden (uno_Environment ** ppHardEnv);
81 void acquire (void);
82 void release (void);
84 void registerProxyInterface (void ** ppProxy,
85 uno_freeProxyFunc freeProxy,
86 rtl::OUString const & oid,
87 typelib_InterfaceTypeDescription * pTypeDescr);
88 void revokeInterface (void * pInterface);
89 void getObjectIdentifier (void * pInterface,
90 rtl::OUString * pOid);
91 void getRegisteredInterface (void **,
92 rtl::OUString const & oid,
93 typelib_InterfaceTypeDescription *);
94 void getRegisteredInterfaces(void ***,
95 sal_Int32 * pnLen,
96 uno_memAlloc memAlloc);
97 void computeObjectIdentifier(void * pInterface,
98 rtl::OUString * pOid);
99 void acquireInterface (void * pInterface);
100 void releaseInterface (void * pInterface);
102 virtual void v_enter (void);
103 virtual void v_leave (void);
104 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
105 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
106 virtual int v_isValid (rtl::OUString * pReason);
108 protected:
109 oslInterlockedCount m_nRef;
110 uno_Environment * m_pEnv;
111 cppu::Enterable * m_pEnterable;
113 EnvFun_P * m_env_acquire;
114 EnvFun_P * m_env_release;
115 EnvFun_PP_P * m_env_harden;
116 EnvFun_P * m_env_acquireWeak;
117 EnvFun_P * m_env_releaseWeak;
119 ExtEnv_registerProxyInterface * m_env_registerProxyInterface;
120 ExtEnv_revokeInterface * m_env_revokeInterface;
121 ExtEnv_getObjectIdentifier * m_env_getObjectIdentifier;
122 ExtEnv_getRegisteredInterface * m_env_getRegisteredInterface;
123 ExtEnv_getRegisteredInterfaces * m_env_getRegisteredInterfaces;
124 ExtEnv_computeObjectIdentifier * m_env_computeObjectIdentifier;
125 ExtEnv_acquireInterface * m_env_acquireInterface;
126 ExtEnv_releaseInterface * m_env_releaseInterface;
128 virtual ~Base();
131 extern "C" {
132 static void SAL_CALL s_acquire(uno_Environment * pEnv) //SAL_THROW_EXTERN_C()
134 Base * pBase = static_cast<Base *>(pEnv->pReserved);
135 pBase->acquire();
138 static void SAL_CALL s_release(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
140 Base * pBase = static_cast<Base *>(pEnv->pReserved);
141 pBase->release();
144 static void SAL_CALL s_harden(uno_Environment ** ppHardEnv, uno_Environment * pEnv) SAL_THROW_EXTERN_C()
146 Base * pBase = static_cast<Base *>(pEnv->pReserved);
147 pBase->harden(ppHardEnv);
150 static void SAL_CALL s_acquireWeak(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
152 Base * pBase = static_cast<Base *>(pEnv->pReserved);
153 pBase->acquireWeak();
156 static void SAL_CALL s_releaseWeak(uno_Environment * pEnv) SAL_THROW_EXTERN_C()
158 Base * pBase = static_cast<Base *>(pEnv->pReserved);
159 pBase->releaseWeak();
163 static void SAL_CALL s_registerProxyInterface(uno_ExtEnvironment * pExtEnv,
164 void ** ppProxy,
165 uno_freeProxyFunc freeProxy,
166 rtl_uString * pOId,
167 typelib_InterfaceTypeDescription * pTypeDescr)
169 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
170 pBase->registerProxyInterface(ppProxy, freeProxy, pOId, pTypeDescr);
173 static void SAL_CALL s_revokeInterface(uno_ExtEnvironment * pExtEnv, void * pInterface)
175 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
176 pBase->revokeInterface(pInterface);
179 static void SAL_CALL s_getObjectIdentifier(uno_ExtEnvironment * pExtEnv,
180 rtl_uString ** ppOId,
181 void * pInterface)
183 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
184 pBase->getObjectIdentifier(pInterface, reinterpret_cast<rtl::OUString *>(ppOId));
187 static void SAL_CALL s_getRegisteredInterface(uno_ExtEnvironment * pExtEnv,
188 void ** ppInterface,
189 rtl_uString * pOId,
190 typelib_InterfaceTypeDescription * pTypeDescr)
192 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
193 pBase->getRegisteredInterface(ppInterface, pOId, pTypeDescr);
196 static void SAL_CALL s_getRegisteredInterfaces(uno_ExtEnvironment * pExtEnv,
197 void *** pppInterface,
198 sal_Int32 * pnLen,
199 uno_memAlloc memAlloc)
201 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
202 pBase->getRegisteredInterfaces(pppInterface, pnLen, memAlloc);
205 static void SAL_CALL s_computeObjectIdentifier(uno_ExtEnvironment * pExtEnv,
206 rtl_uString ** ppOId,
207 void * pInterface)
209 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
210 pBase->computeObjectIdentifier(pInterface, reinterpret_cast<rtl::OUString *>(ppOId));
213 static void SAL_CALL s_acquireInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
214 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
215 pBase->acquireInterface(pInterface);
218 static void SAL_CALL s_releaseInterface(uno_ExtEnvironment * pExtEnv, void * pInterface) {
219 Base * pBase = static_cast<Base *>(pExtEnv->aBase.pReserved);
220 pBase->releaseInterface(pInterface);
225 Base::Base(uno_Environment * pEnv, cppu::Enterable * pEnterable)
226 :m_nRef(1),
227 m_pEnv(pEnv),
228 m_pEnterable (pEnterable),
229 m_env_acquire (pEnv->acquire),
230 m_env_release (pEnv->release),
231 m_env_harden (pEnv->harden),
232 m_env_acquireWeak(pEnv->acquireWeak),
233 m_env_releaseWeak(pEnv->releaseWeak),
234 m_env_registerProxyInterface (pEnv->pExtEnv->registerProxyInterface),
235 m_env_revokeInterface (pEnv->pExtEnv->revokeInterface),
236 m_env_getObjectIdentifier (pEnv->pExtEnv->getObjectIdentifier),
237 m_env_getRegisteredInterface (pEnv->pExtEnv->getRegisteredInterface),
238 m_env_getRegisteredInterfaces(pEnv->pExtEnv->getRegisteredInterfaces),
239 m_env_computeObjectIdentifier(pEnv->pExtEnv->computeObjectIdentifier),
240 m_env_acquireInterface (pEnv->pExtEnv->acquireInterface),
241 m_env_releaseInterface (pEnv->pExtEnv->releaseInterface)
243 LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(fprintf(stderr, "LIFE: %s -> %p\n", "cppu::helper::purpenv::Base::Base(uno_Environment * pEnv)", this));
244 OSL_ENSURE(
245 rtl_ustr_ascii_compare_WithLength(pEnv->pTypeName->buffer, rtl_str_getLength(UNO_LB_UNO), UNO_LB_UNO)
246 == 0,
247 "### wrong environment type!");
249 pEnv->acquire = s_acquire;
250 pEnv->release = s_release;
251 pEnv->harden = s_harden;
252 pEnv->acquireWeak = s_acquireWeak;
253 pEnv->releaseWeak = s_releaseWeak;
255 pEnv->pExtEnv->registerProxyInterface = s_registerProxyInterface;
256 pEnv->pExtEnv->revokeInterface = s_revokeInterface;
257 pEnv->pExtEnv->getObjectIdentifier = s_getObjectIdentifier;
258 pEnv->pExtEnv->getRegisteredInterface = s_getRegisteredInterface;
259 pEnv->pExtEnv->getRegisteredInterfaces = s_getRegisteredInterfaces;
260 pEnv->pExtEnv->computeObjectIdentifier = s_computeObjectIdentifier;
261 pEnv->pExtEnv->acquireInterface = s_acquireInterface;
262 pEnv->pExtEnv->releaseInterface = s_releaseInterface;
264 pEnv->pReserved = this;
267 Base::~Base()
269 LOG_LIFECYCLE_cppu_helper_purpenv_Base_emit(fprintf(stderr, "LIFE: %s -> %p\n", "cppu::helper::purpenv::Base::~Base()", this));
271 m_pEnv->acquire = m_env_acquire;
272 m_pEnv->release = m_env_release;
273 m_pEnv->harden = m_env_harden;
274 m_pEnv->acquireWeak = m_env_acquireWeak;
275 m_pEnv->releaseWeak = m_env_releaseWeak;
277 m_pEnv->pReserved = NULL;
279 delete m_pEnterable;
280 m_pEnv->release(m_pEnv);
283 void Base::acquire(void)
285 m_env_acquire(m_pEnv);
287 osl_atomic_increment(&m_nRef);
290 void Base::release(void)
292 if (osl_atomic_decrement(&m_nRef) == 0)
293 delete this;
295 else
296 m_env_release(m_pEnv);
299 void Base::harden(uno_Environment ** ppHardEnv)
301 m_env_harden(ppHardEnv, m_pEnv);
302 osl_atomic_increment(&m_nRef);
305 void Base::acquireWeak(void)
307 m_env_acquireWeak(m_pEnv);
310 void Base::releaseWeak(void)
312 m_env_releaseWeak(m_pEnv);
316 extern "C" { static void s_registerProxyInterface_v(va_list * pParam)
318 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
319 void ** ppProxy = va_arg(*pParam, void **);
320 uno_freeProxyFunc freeProxy = va_arg(*pParam, uno_freeProxyFunc);
321 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
322 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
323 ExtEnv_registerProxyInterface * pRegisterProxyInterface
324 = va_arg(*pParam, ExtEnv_registerProxyInterface *);
326 pRegisterProxyInterface(pExtEnv, ppProxy, freeProxy, pOId, pTypeDescr);
329 void Base::registerProxyInterface(void ** ppProxy,
330 uno_freeProxyFunc freeProxy,
331 rtl::OUString const & oid,
332 typelib_InterfaceTypeDescription * pTypeDescr)
334 uno_Environment_invoke(m_pEnv,
335 s_registerProxyInterface_v,
336 m_pEnv->pExtEnv,
337 ppProxy,
338 freeProxy,
339 oid.pData,
340 pTypeDescr,
341 m_env_registerProxyInterface);
345 extern "C" { static void s_revokeInterface_v(va_list * pParam)
347 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
348 void * pInterface = va_arg(*pParam, void *);
349 ExtEnv_revokeInterface * pRevokeInterface = va_arg(*pParam, ExtEnv_revokeInterface *);
351 pRevokeInterface(pExtEnv, pInterface);
354 void Base::revokeInterface(void * pInterface)
356 uno_Environment_invoke(m_pEnv,
357 s_revokeInterface_v,
358 m_pEnv->pExtEnv,
359 pInterface,
360 m_env_revokeInterface);
364 extern "C" { static void s_getObjectIdentifier_v(va_list * pParam)
366 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
367 void * pInterface = va_arg(*pParam, void *);
368 rtl::OUString * pOId = va_arg(*pParam, rtl::OUString *);
369 ExtEnv_getObjectIdentifier * pGetObjectIdentifier
370 = va_arg(*pParam, ExtEnv_getObjectIdentifier *);
372 pGetObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
375 void Base::getObjectIdentifier(void * pInterface, rtl::OUString * pOid)
377 uno_Environment_invoke(m_pEnv,
378 s_getObjectIdentifier_v,
379 m_pEnv->pExtEnv,
380 pInterface,
381 pOid,
382 m_env_getObjectIdentifier);
386 extern "C" { static void s_getRegisteredInterface_v(va_list * pParam)
388 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
389 void ** ppInterface = va_arg(*pParam, void **);
390 rtl_uString * pOId = va_arg(*pParam, rtl_uString *);
391 typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
392 ExtEnv_getRegisteredInterface * pGetRegisteredInterface
393 = va_arg(*pParam, ExtEnv_getRegisteredInterface *);
395 pGetRegisteredInterface(pExtEnv, ppInterface, pOId, pTypeDescr);
398 void Base::getRegisteredInterface(void ** ppInterface,
399 rtl::OUString const & oid,
400 typelib_InterfaceTypeDescription * pTypeDescr)
402 uno_Environment_invoke(m_pEnv,
403 s_getRegisteredInterface_v,
404 m_pEnv->pExtEnv,
405 ppInterface,
406 oid.pData,
407 pTypeDescr,
408 m_env_getRegisteredInterface);
412 extern "C" { static void s_getRegisteredInterfaces_v(va_list * pParam)
414 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
415 void *** pppInterface = va_arg(*pParam, void ***);
416 sal_Int32 * pnLen = va_arg(*pParam, sal_Int32 *);
417 uno_memAlloc memAlloc = va_arg(*pParam, uno_memAlloc);
418 ExtEnv_getRegisteredInterfaces * pGetRegisteredInterfaces
419 = va_arg(*pParam, ExtEnv_getRegisteredInterfaces *);
421 pGetRegisteredInterfaces(pExtEnv, pppInterface, pnLen, memAlloc);
424 void Base::getRegisteredInterfaces(void *** pppInterface,
425 sal_Int32 * pnLen,
426 uno_memAlloc memAlloc)
428 uno_Environment_invoke(m_pEnv,
429 s_getRegisteredInterfaces_v,
430 m_pEnv->pExtEnv,
431 pppInterface,
432 pnLen,
433 memAlloc,
434 m_env_getRegisteredInterfaces);
438 extern "C" { static void s_computeObjectIdentifier_v(va_list * pParam)
440 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
441 void * pInterface = va_arg(*pParam, void *);
442 rtl::OUString * pOId = va_arg(*pParam, rtl::OUString *);
443 ExtEnv_computeObjectIdentifier * pComputeObjectIdentifier
444 = va_arg(*pParam, ExtEnv_computeObjectIdentifier *);
446 pComputeObjectIdentifier(pExtEnv, reinterpret_cast<rtl_uString **>(pOId), pInterface);
449 void Base::computeObjectIdentifier(void * pInterface, rtl::OUString * pOid)
451 uno_Environment_invoke(m_pEnv,
452 s_computeObjectIdentifier_v,
453 m_pEnv->pExtEnv,
454 pInterface,
455 pOid,
456 m_env_computeObjectIdentifier);
460 extern "C" { static void s_acquireInterface_v(va_list * pParam)
462 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
463 void * pInterface = va_arg(*pParam, void *);
464 ExtEnv_acquireInterface * pAcquireInterface
465 = va_arg(*pParam, ExtEnv_acquireInterface *);
467 pAcquireInterface(pExtEnv, pInterface);
470 void Base::acquireInterface(void * pInterface)
472 uno_Environment_invoke(m_pEnv, s_acquireInterface_v, m_pEnv->pExtEnv, pInterface, m_env_acquireInterface);
476 extern "C" { static void s_releaseInterface_v(va_list * pParam)
478 uno_ExtEnvironment * pExtEnv = va_arg(*pParam, uno_ExtEnvironment *);
479 void * pInterface = va_arg(*pParam, void *);
480 ExtEnv_releaseInterface * pReleaseInterface
481 = va_arg(*pParam, ExtEnv_releaseInterface *);
483 pReleaseInterface(pExtEnv, pInterface);
486 void Base::releaseInterface(void * pInterface)
488 uno_Environment_invoke(m_pEnv,
489 s_releaseInterface_v,
490 m_pEnv->pExtEnv,
491 pInterface,
492 m_env_releaseInterface);
495 void Base::v_enter(void)
497 m_pEnterable->enter();
500 void Base::v_leave(void)
502 m_pEnterable->leave();
505 void Base::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
507 m_pEnterable->callInto_v(pCallee, pParam);
510 void Base::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
512 m_pEnterable->callOut_v(pCallee, pParam);
515 int Base::v_isValid(rtl::OUString * pReason)
517 return m_pEnterable->isValid(pReason);
520 namespace cppu { namespace helper { namespace purpenv {
522 void Environment_initWithEnterable(uno_Environment * pEnvironment, cppu::Enterable * pEnterable)
524 new Base(pEnvironment, pEnterable);
529 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */