merged tag ooo/DEV300_m102
[LibreOffice.git] / cppu / source / helper / purpenv / helper_purpenv_Mapping.cxx
blob0b4b3c9e078611346ab5275850608cea8c60e224
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/Mapping.hxx"
33 #include "Proxy.hxx"
35 #include "osl/interlck.h"
36 #include "uno/environment.hxx"
37 #include "uno/dispatcher.h"
38 #include "typelib/typedescription.h"
41 #ifdef debug
42 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
43 #endif
45 #ifdef LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
46 # include <iostream>
47 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x) x
49 #else
50 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x)
52 #endif
55 using namespace com::sun::star;
58 class Mapping : public uno_Mapping
60 uno::Environment m_from;
61 uno::Environment m_to;
63 oslInterlockedCount m_nCount;
65 cppu::helper::purpenv::ProbeFun * m_probeFun;
66 void * m_pContext;
68 public:
69 explicit Mapping(uno_Environment * pFrom,
70 uno_Environment * pTo,
71 cppu::helper::purpenv::ProbeFun * probeFun,
72 void * pProbeContext);
73 virtual ~Mapping(void);
75 void mapInterface(
76 uno_Interface ** ppOut,
77 uno_Interface * pUnoI,
78 typelib_InterfaceTypeDescription * pTypeDescr);
80 void acquire(void);
81 void release(void);
84 static void SAL_CALL s_mapInterface(
85 uno_Mapping * puno_Mapping,
86 uno_Interface ** ppOut,
87 uno_Interface * pUnoI,
88 typelib_InterfaceTypeDescription * pTypeDescr )
89 SAL_THROW_EXTERN_C()
91 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
92 pMapping->mapInterface(ppOut, pUnoI, pTypeDescr);
95 extern "C" {
96 static void SAL_CALL s_acquire(uno_Mapping * puno_Mapping)
97 SAL_THROW_EXTERN_C()
99 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
100 pMapping->acquire();
103 static void SAL_CALL s_release(uno_Mapping * puno_Mapping)
104 SAL_THROW_EXTERN_C()
106 Mapping * pMapping = static_cast<Mapping * >(puno_Mapping);
107 pMapping->release();
111 static void s_getIdentifier_v(va_list * pParam)
113 uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *);
114 rtl_uString ** ppOid = va_arg(*pParam, rtl_uString **);
115 uno_Interface * pUnoI = va_arg(*pParam, uno_Interface *);
117 pEnv->getObjectIdentifier(pEnv, ppOid, pUnoI);
120 static void SAL_CALL s_free(uno_Mapping * puno_Mapping)
121 SAL_THROW_EXTERN_C()
123 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
124 delete pMapping;
128 Mapping::Mapping(uno_Environment * pFrom,
129 uno_Environment * pTo,
130 cppu::helper::purpenv::ProbeFun * probeFun,
131 void * pProbeContext
132 ) SAL_THROW( () )
133 : m_from (pFrom),
134 m_to (pTo),
135 m_nCount (1),
136 m_probeFun(probeFun),
137 m_pContext(pProbeContext)
139 LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Mapping::Mapping(uno_Environment * pFrom, uno_Environment * pTo) SAL_THROW( () )", this));
141 uno_Mapping::acquire = s_acquire;
142 uno_Mapping::release = s_release;
143 uno_Mapping::mapInterface = (uno_MapInterfaceFunc)s_mapInterface;
146 Mapping::~Mapping(void)
148 LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Mapping::~Mapping(void)", this));
152 void Mapping::mapInterface(
153 uno_Interface ** ppOut,
154 uno_Interface * pUnoI,
155 typelib_InterfaceTypeDescription * pTypeDescr)
157 OSL_ASSERT(ppOut && pTypeDescr);
158 if (*ppOut)
160 (*ppOut)->release(*ppOut);
161 *ppOut = 0;
164 if (!pUnoI)
165 return;
167 // get object id of uno interface to be wrapped
168 // need to enter environment because of potential "queryInterface" call
169 rtl_uString * pOId = 0;
170 uno_Environment_invoke(m_from.get(), s_getIdentifier_v, m_from.get(), &pOId, pUnoI);
171 OSL_ASSERT(pOId);
173 // try to get any known interface from target environment
174 m_to.get()->pExtEnv->getRegisteredInterface(m_to.get()->pExtEnv, (void **)ppOut, pOId, pTypeDescr);
176 if (!*ppOut) // not yet there, register new proxy interface
178 // try to publish a new proxy (ref count initially 1)
179 uno_Interface * pProxy = new Proxy(this,
180 m_from.get(),
181 m_to.get(),
182 pUnoI,
183 pTypeDescr,
184 pOId,
185 m_probeFun,
186 m_pContext);
188 // proxy may be exchanged during registration
189 m_to.get()->pExtEnv->registerProxyInterface(m_to.get()->pExtEnv,
190 (void **)&pProxy,
191 Proxy_free,
192 pOId,
193 pTypeDescr);
195 *ppOut = pProxy;
198 rtl_uString_release(pOId);
202 void Mapping::acquire() SAL_THROW(())
204 if (osl_incrementInterlockedCount(&m_nCount) == 1)
206 uno_Mapping * pMapping = this;
208 ::uno_registerMapping(&pMapping, s_free, m_from.get(), m_to.get(), NULL);
212 void Mapping::release() SAL_THROW(())
214 if (osl_decrementInterlockedCount(&m_nCount) == 0)
215 ::uno_revokeMapping(this);
219 namespace cppu { namespace helper { namespace purpenv {
221 void createMapping(uno_Mapping ** ppMapping,
222 uno_Environment * pFrom,
223 uno_Environment * pTo,
224 ProbeFun * probeFun,
225 void * pContext
228 *ppMapping = new Mapping(pFrom, pTo, probeFun, pContext);
230 ::uno_registerMapping(ppMapping, s_free, pFrom, pTo, NULL);