LibreOffice Module cppu (master) 1
helper_purpenv_Mapping.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
22
23#include "Proxy.hxx"
24
25#include <osl/interlck.h>
26#include <sal/log.hxx>
27#include <uno/environment.hxx>
28#include <uno/dispatcher.h>
29
30using namespace com::sun::star;
31
32namespace {
33
34class Mapping : public uno_Mapping
35{
36 uno::Environment m_from;
37 uno::Environment m_to;
38
39 oslInterlockedCount m_nCount;
40
42 void * m_pContext;
43
44public:
45 explicit Mapping(uno_Environment * pFrom,
46 uno_Environment * pTo,
48 void * pProbeContext);
49 virtual ~Mapping();
50
51 void mapInterface(
52 uno_Interface ** ppOut,
53 uno_Interface * pUnoI,
54 typelib_InterfaceTypeDescription * pTypeDescr);
55
56 void acquire();
57 void release();
58};
59
60}
61
62static void s_mapInterface(
63 uno_Mapping * puno_Mapping,
64 void ** ppOut,
65 void * pUnoI,
66 typelib_InterfaceTypeDescription * pTypeDescr )
68{
69 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
70 pMapping->mapInterface(
71 reinterpret_cast<uno_Interface **>(ppOut),
72 static_cast<uno_Interface *>(pUnoI), pTypeDescr);
73}
74
75extern "C" {
76static void s_acquire(uno_Mapping * puno_Mapping)
78{
79 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
80 pMapping->acquire();
81}
82
83static void s_release(uno_Mapping * puno_Mapping)
85{
86 Mapping * pMapping = static_cast<Mapping * >(puno_Mapping);
87 pMapping->release();
88}
89
90
91static void s_getIdentifier_v(va_list * pParam)
92{
93 uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *);
94 rtl_uString ** ppOid = va_arg(*pParam, rtl_uString **);
95 uno_Interface * pUnoI = va_arg(*pParam, uno_Interface *);
96
97 pEnv->getObjectIdentifier(pEnv, ppOid, pUnoI);
98}
99
100static void s_free(uno_Mapping * puno_Mapping)
102{
103 Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
104 delete pMapping;
105}
106}
107
109 uno_Environment * pTo,
111 void * pProbeContext
112)
113 : m_from (pFrom),
114 m_to (pTo),
115 m_nCount (1),
116 m_probeFun(probeFun),
117 m_pContext(pProbeContext)
118{
119 SAL_INFO("cppu.purpenv", "LIFE: Mapping::Mapping(uno_Environment * pFrom, uno_Environment * pTo -> " << this);
120
121 uno_Mapping::acquire = s_acquire;
122 uno_Mapping::release = s_release;
123 uno_Mapping::mapInterface = s_mapInterface;
124}
125
126Mapping::~Mapping()
127{
128 SAL_INFO("cppu.purpenv", "LIFE: Mapping:~Mapping() -> " << this);
129}
130
131
132void Mapping::mapInterface(
133 uno_Interface ** ppOut,
134 uno_Interface * pUnoI,
135 typelib_InterfaceTypeDescription * pTypeDescr)
136{
137 OSL_ASSERT(ppOut && pTypeDescr);
138 if (*ppOut)
139 {
140 (*ppOut)->release(*ppOut);
141 *ppOut = nullptr;
142 }
143
144 if (!pUnoI)
145 return;
146
147 // get object id of uno interface to be wrapped
148 // need to enter environment because of potential "queryInterface" call
149 rtl_uString * pOId = nullptr;
150 uno_Environment_invoke(m_from.get(), s_getIdentifier_v, m_from.get(), &pOId, pUnoI);
151 OSL_ASSERT(pOId);
152
153 // try to get any known interface from target environment
154 m_to.get()->pExtEnv->getRegisteredInterface(m_to.get()->pExtEnv, reinterpret_cast<void **>(ppOut), pOId, pTypeDescr);
155
156 if (!*ppOut) // not yet there, register new proxy interface
157 {
158 // try to publish a new proxy (ref count initially 1)
159 uno_Interface * pProxy = new Proxy(this,
160 m_from.get(),
161 m_to.get(),
162 pUnoI,
163 pTypeDescr,
164 pOId,
165 m_probeFun,
166 m_pContext);
167
168 // proxy may be exchanged during registration
169 m_to.get()->pExtEnv->registerProxyInterface(m_to.get()->pExtEnv,
170 reinterpret_cast<void **>(&pProxy),
172 pOId,
173 pTypeDescr);
174
175 *ppOut = pProxy;
176 }
177
178 rtl_uString_release(pOId);
179}
180
181
182void Mapping::acquire()
183{
184 if (osl_atomic_increment(&m_nCount) == 1)
185 {
186 uno_Mapping * pMapping = this;
187
188 ::uno_registerMapping(&pMapping, s_free, m_from.get(), m_to.get(), nullptr);
189 }
190}
191
192void Mapping::release()
193{
194 if (osl_atomic_decrement(&m_nCount) == 0)
196}
197
198
199namespace cppu::helper::purpenv {
200
201void createMapping(uno_Mapping ** ppMapping,
202 uno_Environment * pFrom,
203 uno_Environment * pTo,
204 ProbeFun * probeFun,
205 void * pContext
206 )
207{
208 *ppMapping = new Mapping(pFrom, pTo, probeFun, pContext);
209
210 ::uno_registerMapping(ppMapping, s_free, pFrom, pTo, nullptr);
211}
212
213}
214
215/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL uno_Environment_invoke(uno_Environment *pEnv, uno_EnvCallee *pCallee,...) SAL_THROW_EXTERN_C()
Definition: EnvStack.cxx:315
Definition: Proxy.hxx:32
struct _uno_Environment uno_Environment
static void s_release(uno_Mapping *puno_Mapping) SAL_THROW_EXTERN_C()
static void s_mapInterface(uno_Mapping *puno_Mapping, void **ppOut, void *pUnoI, typelib_InterfaceTypeDescription *pTypeDescr) SAL_THROW_EXTERN_C()
static void s_free(uno_Mapping *puno_Mapping) SAL_THROW_EXTERN_C()
static void s_getIdentifier_v(va_list *pParam)
static void s_acquire(uno_Mapping *puno_Mapping) SAL_THROW_EXTERN_C()
void Proxy_free(SAL_UNUSED_PARAMETER uno_ExtEnvironment *, void *pProxy) SAL_THROW_EXTERN_C()
uno_Mapping * pMapping
Definition: lbmap.cxx:123
void SAL_CALL uno_registerMapping(uno_Mapping **ppMapping, uno_freeMappingFunc freeMapping, uno_Environment *pFrom, uno_Environment *pTo, rtl_uString *pAddPurpose) SAL_THROW_EXTERN_C()
Definition: lbmap.cxx:675
void SAL_CALL uno_revokeMapping(uno_Mapping *pMapping) SAL_THROW_EXTERN_C()
Definition: lbmap.cxx:710
sal_Int16 m_nCount
#define SAL_INFO(area, stream)
struct _uno_Mapping uno_Mapping
void ProbeFun(bool pre, void *pThis, void *pContext, typelib_TypeDescriptionReference *pReturnTypeRef, typelib_MethodParameter *pParams, sal_Int32 nParams, typelib_TypeDescription const *pMemberType, void *pReturn, void *pArgs[], uno_Any **ppException)
C++ helper for implementing Purpose Environments.
Definition: Mapping.hxx:42
void createMapping(uno_Mapping **ppMapping, uno_Environment *pFrom, uno_Environment *pTo, ProbeFun *probeFun, void *pContext)
#define SAL_THROW_EXTERN_C()