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: current.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"
35 #include "osl/thread.h"
36 #include "osl/mutex.hxx"
38 #include "uno/environment.hxx"
39 #include "uno/mapping.hxx"
40 #include "uno/lbnames.h"
41 #include "typelib/typedescription.h"
43 #include "current.hxx"
46 using namespace ::osl
;
47 using namespace ::rtl
;
48 using namespace ::cppu
;
49 using namespace ::com::sun::star::uno
;
54 //--------------------------------------------------------------------------------------------------
55 class SAL_NO_VTABLE XInterface
58 virtual void SAL_CALL
slot_queryInterface() = 0;
59 virtual void SAL_CALL
acquire() throw () = 0;
60 virtual void SAL_CALL
release() throw () = 0;
62 //--------------------------------------------------------------------------------------------------
63 static typelib_InterfaceTypeDescription
* get_type_XCurrentContext()
65 static typelib_InterfaceTypeDescription
* s_type_XCurrentContext
= 0;
66 if (0 == s_type_XCurrentContext
)
68 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
69 if (0 == s_type_XCurrentContext
)
71 OUString
sTypeName( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XCurrentContext") );
72 typelib_InterfaceTypeDescription
* pTD
= 0;
73 typelib_TypeDescriptionReference
* pMembers
[1] = { 0 };
74 OUString
sMethodName0(
75 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XCurrentContext::getValueByName") );
76 typelib_typedescriptionreference_new(
78 typelib_TypeClass_INTERFACE_METHOD
,
80 typelib_typedescription_newInterface(
82 sTypeName
.pData
, 0x00000000, 0x0000, 0x0000, 0x00000000, 0x00000000,
83 * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE
),
87 typelib_typedescription_register( (typelib_TypeDescription
**)&pTD
);
88 typelib_typedescriptionreference_release( pMembers
[0] );
90 typelib_InterfaceMethodTypeDescription
* pMethod
= 0;
91 typelib_Parameter_Init aParameters
[1];
92 OUString
sParamName0( RTL_CONSTASCII_USTRINGPARAM("Name") );
93 OUString
sParamType0( RTL_CONSTASCII_USTRINGPARAM("string") );
94 aParameters
[0].pParamName
= sParamName0
.pData
;
95 aParameters
[0].eTypeClass
= typelib_TypeClass_STRING
;
96 aParameters
[0].pTypeName
= sParamType0
.pData
;
97 aParameters
[0].bIn
= sal_True
;
98 aParameters
[0].bOut
= sal_False
;
99 rtl_uString
* pExceptions
[1];
100 OUString
sExceptionName0(
101 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.RuntimeException") );
102 pExceptions
[0] = sExceptionName0
.pData
;
103 OUString
sReturnType0( RTL_CONSTASCII_USTRINGPARAM("any") );
104 typelib_typedescription_newInterfaceMethod(
108 typelib_TypeClass_ANY
, sReturnType0
.pData
,
109 1, aParameters
, 1, pExceptions
);
110 typelib_typedescription_register( (typelib_TypeDescription
**)&pMethod
);
111 typelib_typedescription_release( (typelib_TypeDescription
*)pMethod
);
112 #if ! defined CPPU_LEAK_STATIC_DATA
113 // another static ref
114 ++reinterpret_cast< typelib_TypeDescription
* >( pTD
)->
117 s_type_XCurrentContext
= pTD
;
120 return s_type_XCurrentContext
;
123 //##################################################################################################
125 //==================================================================================================
129 oslThreadKey _hThreadKey
;
130 oslThreadKeyCallbackFunction _pCallback
;
133 inline oslThreadKey
getThreadKey() SAL_THROW( () );
135 inline ThreadKey( oslThreadKeyCallbackFunction pCallback
) SAL_THROW( () );
136 inline ~ThreadKey() SAL_THROW( () );
138 //__________________________________________________________________________________________________
139 inline ThreadKey::ThreadKey( oslThreadKeyCallbackFunction pCallback
) SAL_THROW( () )
140 : _bInit( sal_False
)
141 , _pCallback( pCallback
)
144 //__________________________________________________________________________________________________
145 inline ThreadKey::~ThreadKey() SAL_THROW( () )
149 ::osl_destroyThreadKey( _hThreadKey
);
152 //__________________________________________________________________________________________________
153 inline oslThreadKey
ThreadKey::getThreadKey() SAL_THROW( () )
157 MutexGuard
aGuard( Mutex::getGlobalMutex() );
160 _hThreadKey
= ::osl_createThreadKey( _pCallback
);
167 //==================================================================================================
168 extern "C" void SAL_CALL
delete_IdContainer( void * p
)
172 IdContainer
* pId
= reinterpret_cast< IdContainer
* >( p
);
173 if (pId
->pCurrentContext
)
175 (*pId
->pCurrentContextEnv
->releaseInterface
)(
176 pId
->pCurrentContextEnv
, pId
->pCurrentContext
);
177 (*((uno_Environment
*)pId
->pCurrentContextEnv
)->release
)(
178 (uno_Environment
*)pId
->pCurrentContextEnv
);
182 ::rtl_byte_sequence_release( pId
->pLocalThreadId
);
183 ::rtl_byte_sequence_release( pId
->pCurrentId
);
188 //==================================================================================================
189 IdContainer
* getIdContainer() SAL_THROW( () )
191 static ThreadKey
s_key( delete_IdContainer
);
192 oslThreadKey aKey
= s_key
.getThreadKey();
194 IdContainer
* pId
= reinterpret_cast< IdContainer
* >( ::osl_getThreadKeyData( aKey
) );
197 pId
= new IdContainer();
198 pId
->pCurrentContext
= 0;
199 pId
->pCurrentContextEnv
= 0;
200 pId
->bInit
= sal_False
;
201 ::osl_setThreadKeyData( aKey
, pId
);
208 //##################################################################################################
209 extern "C" sal_Bool SAL_CALL
uno_setCurrentContext(
210 void * pCurrentContext
,
211 rtl_uString
* pEnvTypeName
, void * pEnvContext
)
214 IdContainer
* pId
= getIdContainer();
218 if (pId
->pCurrentContext
)
220 (*pId
->pCurrentContextEnv
->releaseInterface
)(
221 pId
->pCurrentContextEnv
, pId
->pCurrentContext
);
222 (*((uno_Environment
*)pId
->pCurrentContextEnv
)->release
)(
223 (uno_Environment
*)pId
->pCurrentContextEnv
);
224 pId
->pCurrentContextEnv
= 0;
226 pId
->pCurrentContext
= 0;
231 uno_Environment
* pEnv
= 0;
232 ::uno_getEnvironment( &pEnv
, pEnvTypeName
, pEnvContext
);
233 OSL_ASSERT( pEnv
&& pEnv
->pExtEnv
);
238 pId
->pCurrentContextEnv
= pEnv
->pExtEnv
;
239 (*pId
->pCurrentContextEnv
->acquireInterface
)(
240 pId
->pCurrentContextEnv
, pCurrentContext
);
241 pId
->pCurrentContext
= pCurrentContext
;
245 (*pEnv
->release
)( pEnv
);
256 //##################################################################################################
257 extern "C" sal_Bool SAL_CALL
uno_getCurrentContext(
258 void ** ppCurrentContext
, rtl_uString
* pEnvTypeName
, void * pEnvContext
)
261 IdContainer
* pId
= getIdContainer();
264 Environment target_env
;
266 // release inout parameter
267 if (*ppCurrentContext
)
269 target_env
= Environment(rtl::OUString(pEnvTypeName
), pEnvContext
);
270 OSL_ASSERT( target_env
.is() );
271 if (! target_env
.is())
273 uno_ExtEnvironment
* pEnv
= target_env
.get()->pExtEnv
;
274 OSL_ASSERT( 0 != pEnv
);
277 (*pEnv
->releaseInterface
)( pEnv
, *ppCurrentContext
);
279 *ppCurrentContext
= 0;
283 if (0 == pId
->pCurrentContext
)
286 if (! target_env
.is())
288 target_env
= Environment(rtl::OUString(pEnvTypeName
), pEnvContext
);
289 OSL_ASSERT( target_env
.is() );
290 if (! target_env
.is())
294 Mapping
mapping((uno_Environment
*) pId
->pCurrentContextEnv
, target_env
.get());
295 OSL_ASSERT( mapping
.is() );
299 mapping
.mapInterface(ppCurrentContext
, pId
->pCurrentContext
, ::cppu::get_type_XCurrentContext() );