LibreOffice Module cppu (master) 1
current.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#include <sal/config.h>
21
22#include <rtl/byteseq.h>
23#include <osl/mutex.hxx>
24
25#include <uno/current_context.h>
26#include <uno/environment.hxx>
27#include <uno/mapping.hxx>
28#include <typelib/typedescription.h>
29
30#include "current.hxx"
31
32
33using namespace ::osl;
34using namespace ::rtl;
35using namespace ::cppu;
36using namespace ::com::sun::star::uno;
37
38namespace cppu
39{
40
41static typelib_InterfaceTypeDescription * get_type_XCurrentContext()
42{
43 static typelib_InterfaceTypeDescription* s_type_XCurrentContext = []() {
44 OUString sTypeName("com.sun.star.uno.XCurrentContext");
45 typelib_InterfaceTypeDescription* pTD = nullptr;
46 typelib_TypeDescriptionReference* pMembers[1] = { nullptr };
47 OUString sMethodName0("com.sun.star.uno.XCurrentContext::getValueByName");
48 typelib_typedescriptionreference_new(&pMembers[0], typelib_TypeClass_INTERFACE_METHOD,
49 sMethodName0.pData);
51 &pTD, sTypeName.pData, 0, 0, 0, 0, 0,
52 *typelib_static_type_getByTypeClass(typelib_TypeClass_INTERFACE), 1, pMembers);
53
56
57 typelib_InterfaceMethodTypeDescription* pMethod = nullptr;
58 typelib_Parameter_Init aParameters[1];
59 OUString sParamName0("Name");
60 OUString sParamType0("string");
61 aParameters[0].pParamName = sParamName0.pData;
62 aParameters[0].eTypeClass = typelib_TypeClass_STRING;
63 aParameters[0].pTypeName = sParamType0.pData;
64 aParameters[0].bIn = true;
65 aParameters[0].bOut = false;
66 rtl_uString* pExceptions[1];
67 OUString sExceptionName0("com.sun.star.uno.RuntimeException");
68 pExceptions[0] = sExceptionName0.pData;
69 OUString sReturnType0("any");
70 typelib_typedescription_newInterfaceMethod(&pMethod, 3, false, sMethodName0.pData,
71 typelib_TypeClass_ANY, sReturnType0.pData, 1,
72 aParameters, 1, pExceptions);
73 typelib_typedescription_register(reinterpret_cast<typelib_TypeDescription**>(&pMethod));
74 typelib_typedescription_release(&pMethod->aBase.aBase);
75 // another static ref:
76 ++reinterpret_cast<typelib_TypeDescription*>(pTD)->nStaticRefCount;
77 return pTD;
78 }();
79
80 return s_type_XCurrentContext;
81}
82
84 : pCurrentContext(nullptr)
85 , pCurrentContextEnv(nullptr)
86 , pLocalThreadId(nullptr)
87 , pCurrentId(nullptr)
88 , nRefCountOfCurrentId(0)
89 , bInit(false)
90{
91}
92
94{
96 {
97 (*pCurrentContextEnv->releaseInterface)(
99 (*pCurrentContextEnv->aBase.release)(
100 &pCurrentContextEnv->aBase );
101 }
102 if (bInit)
103 {
104 ::rtl_byte_sequence_release( pLocalThreadId );
105 ::rtl_byte_sequence_release( pCurrentId );
106 }
107}
108
110{
111 static thread_local IdContainer aId;
112 return aId;
113}
114
115}
116
117extern "C" sal_Bool SAL_CALL uno_setCurrentContext(
118 void * pCurrentContext,
119 rtl_uString * pEnvTypeName, void * pEnvContext )
121{
122 IdContainer& id = getIdContainer();
123
124 // free old one
125 if (id.pCurrentContext)
126 {
127 (*id.pCurrentContextEnv->releaseInterface)(
128 id.pCurrentContextEnv, id.pCurrentContext );
129 (*id.pCurrentContextEnv->aBase.release)(
130 &id.pCurrentContextEnv->aBase );
131 id.pCurrentContextEnv = nullptr;
132
133 id.pCurrentContext = nullptr;
134 }
135
136 if (!pCurrentContext)
137 return true;
138
139 uno_Environment * pEnv = nullptr;
140 ::uno_getEnvironment( &pEnv, pEnvTypeName, pEnvContext );
141 OSL_ASSERT( pEnv && pEnv->pExtEnv );
142 if (pEnv)
143 {
144 if (pEnv->pExtEnv)
145 {
146 id.pCurrentContextEnv = pEnv->pExtEnv;
147 (*id.pCurrentContextEnv->acquireInterface)(
148 id.pCurrentContextEnv, pCurrentContext );
149 id.pCurrentContext = pCurrentContext;
150 }
151 else
152 {
153 (*pEnv->release)( pEnv );
154 return false;
155 }
156 }
157 else
158 {
159 return false;
160 }
161 return true;
162}
163
164extern "C" sal_Bool SAL_CALL uno_getCurrentContext(
165 void ** ppCurrentContext, rtl_uString * pEnvTypeName, void * pEnvContext )
167{
168 IdContainer& id = getIdContainer();
169
170 Environment target_env;
171
172 // release inout parameter
173 if (*ppCurrentContext)
174 {
175 target_env = Environment(OUString(pEnvTypeName), pEnvContext);
176 OSL_ASSERT( target_env.is() );
177 if (! target_env.is())
178 return false;
179 uno_ExtEnvironment * pEnv = target_env.get()->pExtEnv;
180 OSL_ASSERT( nullptr != pEnv );
181 if (nullptr == pEnv)
182 return false;
183 (*pEnv->releaseInterface)( pEnv, *ppCurrentContext );
184
185 *ppCurrentContext = nullptr;
186 }
187
188 // case: null-ref
189 if (nullptr == id.pCurrentContext)
190 return true;
191
192 if (! target_env.is())
193 {
194 target_env = Environment(OUString(pEnvTypeName), pEnvContext);
195 OSL_ASSERT( target_env.is() );
196 if (! target_env.is())
197 return false;
198 }
199
200 Mapping mapping(&id.pCurrentContextEnv->aBase, target_env.get());
201 OSL_ASSERT( mapping.is() );
202 if (! mapping.is())
203 return false;
204
205 mapping.mapInterface(ppCurrentContext, id.pCurrentContext, ::cppu::get_type_XCurrentContext());
206
207 return true;
208}
209
210/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Bool SAL_CALL uno_getCurrentContext(void **ppCurrentContext, rtl_uString *pEnvTypeName, void *pEnvContext) SAL_THROW_EXTERN_C()
Definition: current.cxx:164
sal_Bool SAL_CALL uno_setCurrentContext(void *pCurrentContext, rtl_uString *pEnvTypeName, void *pEnvContext) SAL_THROW_EXTERN_C()
Definition: current.cxx:117
struct _uno_Environment uno_Environment
void SAL_CALL uno_getEnvironment(uno_Environment **ppEnv, rtl_uString *pEnvDcp, void *pContext) SAL_THROW_EXTERN_C()
Definition: lbenv.cxx:1110
struct _typelib_TypeDescription typelib_TypeDescription
static typelib_InterfaceTypeDescription * get_type_XCurrentContext()
Definition: current.cxx:41
IdContainer & getIdContainer()
Definition: current.cxx:109
typelib_TypeDescriptionReference **SAL_CALL typelib_static_type_getByTypeClass(typelib_TypeClass eTypeClass) SAL_THROW_EXTERN_C()
void * pCurrentContext
Definition: current.hxx:32
sal_Sequence * pCurrentId
Definition: current.hxx:36
sal_Sequence * pLocalThreadId
Definition: current.hxx:35
_uno_ExtEnvironment * pCurrentContextEnv
Definition: current.hxx:33
void SAL_CALL typelib_typedescriptionreference_new(typelib_TypeDescriptionReference **ppTDR, typelib_TypeClass eTypeClass, rtl_uString *pTypeName) SAL_THROW_EXTERN_C()
Definition: typelib.cxx:2042
void SAL_CALL typelib_typedescription_register(typelib_TypeDescription **ppNewDescription) SAL_THROW_EXTERN_C()
Definition: typelib.cxx:1393
void SAL_CALL typelib_typedescription_newInterface(typelib_InterfaceTypeDescription **ppRet, rtl_uString *pTypeName, SAL_UNUSED_PARAMETER sal_uInt32, SAL_UNUSED_PARAMETER sal_uInt16, SAL_UNUSED_PARAMETER sal_uInt16, SAL_UNUSED_PARAMETER sal_uInt32, SAL_UNUSED_PARAMETER sal_uInt32, typelib_TypeDescriptionReference *pBaseInterface, sal_Int32 nMembers, typelib_TypeDescriptionReference **ppMembers) SAL_THROW_EXTERN_C()
Definition: typelib.cxx:823
void SAL_CALL typelib_typedescription_release(typelib_TypeDescription *pTD) SAL_THROW_EXTERN_C()
Definition: typelib.cxx:1325
void SAL_CALL typelib_typedescriptionreference_release(typelib_TypeDescriptionReference *pRef) SAL_THROW_EXTERN_C()
Definition: typelib.cxx:2141
void SAL_CALL typelib_typedescription_newInterfaceMethod(typelib_InterfaceMethodTypeDescription **ppRet, sal_Int32 nAbsolutePosition, sal_Bool bOneWay, rtl_uString *pTypeName, typelib_TypeClass eReturnTypeClass, rtl_uString *pReturnTypeName, sal_Int32 nParams, typelib_Parameter_Init *pParams, sal_Int32 nExceptions, rtl_uString **ppExceptionNames) SAL_THROW_EXTERN_C()
Definition: typelib.cxx:1042
unsigned char sal_Bool
#define SAL_THROW_EXTERN_C()