merge the formfield patch from ooo-build
[ooovba.git] / cppu / source / helper / purpenv / helper_purpenv_Mapping.cxx
blob5daa299ce96bec3d5710d38d0f8a6bf4489de575
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: helper_purpenv_Mapping.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppu.hxx"
34 #include "cppu/helper/purpenv/Mapping.hxx"
36 #include "Proxy.hxx"
38 #include "osl/interlck.h"
39 #include "uno/environment.hxx"
40 #include "uno/dispatcher.h"
41 #include "typelib/typedescription.h"
44 #ifdef debug
45 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
46 #endif
48 #ifdef LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
49 # include <iostream>
50 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x) x
52 #else
53 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x)
55 #endif
58 using namespace com::sun::star;
61 class Mapping : public uno_Mapping
63 uno::Environment m_from;
64 uno::Environment m_to;
66 oslInterlockedCount m_nCount;
68 cppu::helper::purpenv::ProbeFun * m_probeFun;
69 void * m_pContext;
71 public:
72 explicit Mapping(uno_Environment * pFrom,
73 uno_Environment * pTo,
74 cppu::helper::purpenv::ProbeFun * probeFun,
75 void * pProbeContext);
76 virtual ~Mapping(void);
78 void mapInterface(
79 uno_Interface ** ppOut,
80 uno_Interface * pUnoI,
81 typelib_InterfaceTypeDescription * pTypeDescr);
83 void acquire(void);
84 void release(void);
87 static void SAL_CALL s_mapInterface(
88 uno_Mapping * puno_Mapping,
89 uno_Interface ** ppOut,
90 uno_Interface * pUnoI,
91 typelib_InterfaceTypeDescription * pTypeDescr )
92 SAL_THROW_EXTERN_C()
94 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
95 pMapping->mapInterface(ppOut, pUnoI, pTypeDescr);
98 extern "C" {
99 static void SAL_CALL s_acquire(uno_Mapping * puno_Mapping)
100 SAL_THROW_EXTERN_C()
102 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
103 pMapping->acquire();
106 static void SAL_CALL s_release(uno_Mapping * puno_Mapping)
107 SAL_THROW_EXTERN_C()
109 Mapping * pMapping = static_cast<Mapping * >(puno_Mapping);
110 pMapping->release();
114 static void s_getIdentifier_v(va_list * pParam)
116 uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *);
117 rtl_uString ** ppOid = va_arg(*pParam, rtl_uString **);
118 uno_Interface * pUnoI = va_arg(*pParam, uno_Interface *);
120 pEnv->getObjectIdentifier(pEnv, ppOid, pUnoI);
123 static void SAL_CALL s_free(uno_Mapping * puno_Mapping)
124 SAL_THROW_EXTERN_C()
126 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
127 delete pMapping;
131 Mapping::Mapping(uno_Environment * pFrom,
132 uno_Environment * pTo,
133 cppu::helper::purpenv::ProbeFun * probeFun,
134 void * pProbeContext
135 ) SAL_THROW( () )
136 : m_from (pFrom),
137 m_to (pTo),
138 m_nCount (1),
139 m_probeFun(probeFun),
140 m_pContext(pProbeContext)
142 LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Mapping::Mapping(uno_Environment * pFrom, uno_Environment * pTo) SAL_THROW( () )", this));
144 uno_Mapping::acquire = s_acquire;
145 uno_Mapping::release = s_release;
146 uno_Mapping::mapInterface = (uno_MapInterfaceFunc)s_mapInterface;
149 Mapping::~Mapping(void)
151 LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Mapping::~Mapping(void)", this));
155 void Mapping::mapInterface(
156 uno_Interface ** ppOut,
157 uno_Interface * pUnoI,
158 typelib_InterfaceTypeDescription * pTypeDescr)
160 OSL_ASSERT(ppOut && pTypeDescr);
161 if (*ppOut)
163 (*ppOut)->release(*ppOut);
164 *ppOut = 0;
167 if (!pUnoI)
168 return;
170 // get object id of uno interface to be wrapped
171 // need to enter environment because of potential "queryInterface" call
172 rtl_uString * pOId = 0;
173 uno_Environment_invoke(m_from.get(), s_getIdentifier_v, m_from.get(), &pOId, pUnoI);
174 OSL_ASSERT(pOId);
176 // try to get any known interface from target environment
177 m_to.get()->pExtEnv->getRegisteredInterface(m_to.get()->pExtEnv, (void **)ppOut, pOId, pTypeDescr);
179 if (!*ppOut) // not yet there, register new proxy interface
181 // try to publish a new proxy (ref count initially 1)
182 uno_Interface * pProxy = new Proxy(this,
183 m_from.get(),
184 m_to.get(),
185 pUnoI,
186 pTypeDescr,
187 pOId,
188 m_probeFun,
189 m_pContext);
191 // proxy may be exchanged during registration
192 m_to.get()->pExtEnv->registerProxyInterface(m_to.get()->pExtEnv,
193 (void **)&pProxy,
194 Proxy_free,
195 pOId,
196 pTypeDescr);
198 *ppOut = pProxy;
201 rtl_uString_release(pOId);
205 void Mapping::acquire() SAL_THROW(())
207 if (osl_incrementInterlockedCount(&m_nCount) == 1)
209 uno_Mapping * pMapping = this;
211 ::uno_registerMapping(&pMapping, s_free, m_from.get(), m_to.get(), NULL);
215 void Mapping::release() SAL_THROW(())
217 if (osl_decrementInterlockedCount(&m_nCount) == 0)
218 ::uno_revokeMapping(this);
222 namespace cppu { namespace helper { namespace purpenv {
224 void createMapping(uno_Mapping ** ppMapping,
225 uno_Environment * pFrom,
226 uno_Environment * pTo,
227 ProbeFun * probeFun,
228 void * pContext
231 *ppMapping = new Mapping(pFrom, pTo, probeFun, pContext);
233 ::uno_registerMapping(ppMapping, s_free, pFrom, pTo, NULL);