xref: /aoo42x/main/cppu/inc/uno/current_context.hxx (revision c6ed87c9)
1*c6ed87c9SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c6ed87c9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c6ed87c9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c6ed87c9SAndrew Rist  * distributed with this work for additional information
6*c6ed87c9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c6ed87c9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c6ed87c9SAndrew Rist  * "License"); you may not use this file except in compliance
9*c6ed87c9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c6ed87c9SAndrew Rist  *
11*c6ed87c9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c6ed87c9SAndrew Rist  *
13*c6ed87c9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c6ed87c9SAndrew Rist  * software distributed under the License is distributed on an
15*c6ed87c9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c6ed87c9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c6ed87c9SAndrew Rist  * specific language governing permissions and limitations
18*c6ed87c9SAndrew Rist  * under the License.
19*c6ed87c9SAndrew Rist  *
20*c6ed87c9SAndrew Rist  *************************************************************/
21*c6ed87c9SAndrew Rist 
22*c6ed87c9SAndrew Rist 
23cdf0e10cSrcweir #ifndef _UNO_CURRENT_CONTEXT_HXX_
24cdf0e10cSrcweir #define _UNO_CURRENT_CONTEXT_HXX_
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <uno/current_context.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <com/sun/star/uno/XCurrentContext.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace com
32cdf0e10cSrcweir {
33cdf0e10cSrcweir namespace sun
34cdf0e10cSrcweir {
35cdf0e10cSrcweir namespace star
36cdf0e10cSrcweir {
37cdf0e10cSrcweir namespace uno
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /** Getting the current context.
41cdf0e10cSrcweir 	@attention
42cdf0e10cSrcweir     Don't spread the returned interface around to other threads.  Every thread has its own
43cdf0e10cSrcweir     current context.
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     @return current context or null ref, if none is set
46cdf0e10cSrcweir */
getCurrentContext()47cdf0e10cSrcweir inline Reference< XCurrentContext > SAL_CALL getCurrentContext()
48cdf0e10cSrcweir     SAL_THROW( () )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	Reference< XCurrentContext > xRet;
51cdf0e10cSrcweir 	::rtl::OUString aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
52cdf0e10cSrcweir 	::uno_getCurrentContext( (void **)&xRet, aEnvTypeName.pData, 0 );
53cdf0e10cSrcweir 	return xRet;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir /** Setting the current context.
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     @param xContext current context to be set
58cdf0e10cSrcweir     @return true, if context has been successfully set
59cdf0e10cSrcweir */
setCurrentContext(Reference<XCurrentContext> const & xContext)60cdf0e10cSrcweir inline bool SAL_CALL setCurrentContext(
61cdf0e10cSrcweir     Reference< XCurrentContext > const & xContext )
62cdf0e10cSrcweir     SAL_THROW( () )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	::rtl::OUString aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
65cdf0e10cSrcweir 	return (::uno_setCurrentContext( xContext.get(), aEnvTypeName.pData, 0 ) != sal_False);
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir /** Objects of this class are used for applying a current context until they are destructed, i.e.
69cdf0e10cSrcweir     the ctor of this class saves the previous and sets the given context while the dtor restores
70cdf0e10cSrcweir     the previous one upon destruction.
71cdf0e10cSrcweir */
72cdf0e10cSrcweir class ContextLayer
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     /** this C++ environment type name.
75cdf0e10cSrcweir         @internal
76cdf0e10cSrcweir     */
77cdf0e10cSrcweir     ::rtl::OUString m_aEnvTypeName;
78cdf0e10cSrcweir     /** previous context
79cdf0e10cSrcweir         @internal
80cdf0e10cSrcweir     */
81cdf0e10cSrcweir     Reference< XCurrentContext > m_xPreviousContext;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir public:
84cdf0e10cSrcweir     /** Constructor: Saves the previous context and sets the new (given) one.
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         @param xNewContext new context to be set
87cdf0e10cSrcweir     */
88cdf0e10cSrcweir     inline ContextLayer(
89cdf0e10cSrcweir         Reference< XCurrentContext > const & xNewContext = Reference< XCurrentContext >() )
90cdf0e10cSrcweir         SAL_THROW( () );
91cdf0e10cSrcweir     /** Destructor: restores the previous context.
92cdf0e10cSrcweir     */
93cdf0e10cSrcweir     inline ~ContextLayer() SAL_THROW( () );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     /** Gets the previously set context.
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         @return the previously set context
98cdf0e10cSrcweir     */
getPreviousContext() const99cdf0e10cSrcweir     inline Reference< XCurrentContext > SAL_CALL getPreviousContext() const
100cdf0e10cSrcweir         SAL_THROW( () )
101cdf0e10cSrcweir         { return m_xPreviousContext; }
102cdf0e10cSrcweir };
103cdf0e10cSrcweir //__________________________________________________________________________________________________
ContextLayer(Reference<XCurrentContext> const & xNewContext)104cdf0e10cSrcweir inline ContextLayer::ContextLayer( Reference< XCurrentContext > const & xNewContext )
105cdf0e10cSrcweir     SAL_THROW( () )
106cdf0e10cSrcweir     : m_aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	::uno_getCurrentContext( (void **)&m_xPreviousContext, m_aEnvTypeName.pData, 0 );
109cdf0e10cSrcweir 	::uno_setCurrentContext( xNewContext.get(), m_aEnvTypeName.pData, 0 );
110cdf0e10cSrcweir }
111cdf0e10cSrcweir //__________________________________________________________________________________________________
~ContextLayer()112cdf0e10cSrcweir inline ContextLayer::~ContextLayer()
113cdf0e10cSrcweir     SAL_THROW( () )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	::uno_setCurrentContext( m_xPreviousContext.get(), m_aEnvTypeName.pData, 0 );
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir }
119cdf0e10cSrcweir }
120cdf0e10cSrcweir }
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir #endif
124