1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: helper_purpenv_Mapping.cxx,v $
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"
38 #include "osl/interlck.h"
39 #include "uno/environment.hxx"
40 #include "uno/dispatcher.h"
41 #include "typelib/typedescription.h"
45 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
48 #ifdef LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
50 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x) x
53 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x)
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
;
72 explicit Mapping(uno_Environment
* pFrom
,
73 uno_Environment
* pTo
,
74 cppu::helper::purpenv::ProbeFun
* probeFun
,
75 void * pProbeContext
);
76 virtual ~Mapping(void);
79 uno_Interface
** ppOut
,
80 uno_Interface
* pUnoI
,
81 typelib_InterfaceTypeDescription
* pTypeDescr
);
87 static void SAL_CALL
s_mapInterface(
88 uno_Mapping
* puno_Mapping
,
89 uno_Interface
** ppOut
,
90 uno_Interface
* pUnoI
,
91 typelib_InterfaceTypeDescription
* pTypeDescr
)
94 Mapping
* pMapping
= static_cast<Mapping
*>(puno_Mapping
);
95 pMapping
->mapInterface(ppOut
, pUnoI
, pTypeDescr
);
99 static void SAL_CALL
s_acquire(uno_Mapping
* puno_Mapping
)
102 Mapping
* pMapping
= static_cast<Mapping
*>(puno_Mapping
);
106 static void SAL_CALL
s_release(uno_Mapping
* puno_Mapping
)
109 Mapping
* pMapping
= static_cast<Mapping
* >(puno_Mapping
);
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
)
126 Mapping
* pMapping
= static_cast<Mapping
*>(puno_Mapping
);
131 Mapping::Mapping(uno_Environment
* pFrom
,
132 uno_Environment
* pTo
,
133 cppu::helper::purpenv::ProbeFun
* probeFun
,
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
);
163 (*ppOut
)->release(*ppOut
);
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
);
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,
191 // proxy may be exchanged during registration
192 m_to
.get()->pExtEnv
->registerProxyInterface(m_to
.get()->pExtEnv
,
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
,
231 *ppMapping
= new Mapping(pFrom
, pTo
, probeFun
, pContext
);
233 ::uno_registerMapping(ppMapping
, s_free
, pFrom
, pTo
, NULL
);