1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/Mapping.hxx"
25 #include "osl/interlck.h"
26 #include "uno/environment.hxx"
27 #include "uno/dispatcher.h"
28 #include "typelib/typedescription.h"
32 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
35 #ifdef LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
37 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x) x
40 # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x)
45 using namespace com::sun::star
;
48 class Mapping
: public uno_Mapping
50 uno::Environment m_from
;
51 uno::Environment m_to
;
53 oslInterlockedCount m_nCount
;
55 cppu::helper::purpenv::ProbeFun
* m_probeFun
;
59 explicit Mapping(uno_Environment
* pFrom
,
60 uno_Environment
* pTo
,
61 cppu::helper::purpenv::ProbeFun
* probeFun
,
62 void * pProbeContext
);
66 uno_Interface
** ppOut
,
67 uno_Interface
* pUnoI
,
68 typelib_InterfaceTypeDescription
* pTypeDescr
);
74 static void SAL_CALL
s_mapInterface(
75 uno_Mapping
* puno_Mapping
,
78 typelib_InterfaceTypeDescription
* pTypeDescr
)
81 Mapping
* pMapping
= static_cast<Mapping
*>(puno_Mapping
);
82 pMapping
->mapInterface(
83 reinterpret_cast<uno_Interface
**>(ppOut
),
84 static_cast<uno_Interface
*>(pUnoI
), pTypeDescr
);
88 static void SAL_CALL
s_acquire(uno_Mapping
* puno_Mapping
)
91 Mapping
* pMapping
= static_cast<Mapping
*>(puno_Mapping
);
95 static void SAL_CALL
s_release(uno_Mapping
* puno_Mapping
)
98 Mapping
* pMapping
= static_cast<Mapping
* >(puno_Mapping
);
103 static void s_getIdentifier_v(va_list * pParam
)
105 uno_ExtEnvironment
* pEnv
= va_arg(*pParam
, uno_ExtEnvironment
*);
106 rtl_uString
** ppOid
= va_arg(*pParam
, rtl_uString
**);
107 uno_Interface
* pUnoI
= va_arg(*pParam
, uno_Interface
*);
109 pEnv
->getObjectIdentifier(pEnv
, ppOid
, pUnoI
);
112 static void SAL_CALL
s_free(uno_Mapping
* puno_Mapping
)
115 Mapping
* pMapping
= static_cast<Mapping
*>(puno_Mapping
);
120 Mapping::Mapping(uno_Environment
* pFrom
,
121 uno_Environment
* pTo
,
122 cppu::helper::purpenv::ProbeFun
* probeFun
,
128 m_probeFun(probeFun
),
129 m_pContext(pProbeContext
)
131 LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "Mapping::Mapping(uno_Environment * pFrom, uno_Environment * pTo)", this));
133 uno_Mapping::acquire
= s_acquire
;
134 uno_Mapping::release
= s_release
;
135 uno_Mapping::mapInterface
= (uno_MapInterfaceFunc
)s_mapInterface
;
140 LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "Mapping::~Mapping()", this));
144 void Mapping::mapInterface(
145 uno_Interface
** ppOut
,
146 uno_Interface
* pUnoI
,
147 typelib_InterfaceTypeDescription
* pTypeDescr
)
149 OSL_ASSERT(ppOut
&& pTypeDescr
);
152 (*ppOut
)->release(*ppOut
);
159 // get object id of uno interface to be wrapped
160 // need to enter environment because of potential "queryInterface" call
161 rtl_uString
* pOId
= 0;
162 uno_Environment_invoke(m_from
.get(), s_getIdentifier_v
, m_from
.get(), &pOId
, pUnoI
);
165 // try to get any known interface from target environment
166 m_to
.get()->pExtEnv
->getRegisteredInterface(m_to
.get()->pExtEnv
, reinterpret_cast<void **>(ppOut
), pOId
, pTypeDescr
);
168 if (!*ppOut
) // not yet there, register new proxy interface
170 // try to publish a new proxy (ref count initially 1)
171 uno_Interface
* pProxy
= new Proxy(this,
180 // proxy may be exchanged during registration
181 m_to
.get()->pExtEnv
->registerProxyInterface(m_to
.get()->pExtEnv
,
182 reinterpret_cast<void **>(&pProxy
),
190 rtl_uString_release(pOId
);
194 void Mapping::acquire()
196 if (osl_atomic_increment(&m_nCount
) == 1)
198 uno_Mapping
* pMapping
= this;
200 ::uno_registerMapping(&pMapping
, s_free
, m_from
.get(), m_to
.get(), NULL
);
204 void Mapping::release()
206 if (osl_atomic_decrement(&m_nCount
) == 0)
207 ::uno_revokeMapping(this);
211 namespace cppu
{ namespace helper
{ namespace purpenv
{
213 void createMapping(uno_Mapping
** ppMapping
,
214 uno_Environment
* pFrom
,
215 uno_Environment
* pTo
,
220 *ppMapping
= new Mapping(pFrom
, pTo
, probeFun
, pContext
);
222 ::uno_registerMapping(ppMapping
, s_free
, pFrom
, pTo
, NULL
);
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */