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