xref: /trunk/main/cppu/inc/uno/current_context.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _UNO_CURRENT_CONTEXT_HXX_
28 #define _UNO_CURRENT_CONTEXT_HXX_
29 
30 #include <uno/current_context.h>
31 
32 #include <com/sun/star/uno/XCurrentContext.hpp>
33 
34 
35 namespace com
36 {
37 namespace sun
38 {
39 namespace star
40 {
41 namespace uno
42 {
43 
44 /** Getting the current context.
45 	@attention
46     Don't spread the returned interface around to other threads.  Every thread has its own
47     current context.
48 
49     @return current context or null ref, if none is set
50 */
51 inline Reference< XCurrentContext > SAL_CALL getCurrentContext()
52     SAL_THROW( () )
53 {
54 	Reference< XCurrentContext > xRet;
55 	::rtl::OUString aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
56 	::uno_getCurrentContext( (void **)&xRet, aEnvTypeName.pData, 0 );
57 	return xRet;
58 }
59 /** Setting the current context.
60 
61     @param xContext current context to be set
62     @return true, if context has been successfully set
63 */
64 inline bool SAL_CALL setCurrentContext(
65     Reference< XCurrentContext > const & xContext )
66     SAL_THROW( () )
67 {
68 	::rtl::OUString aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
69 	return (::uno_setCurrentContext( xContext.get(), aEnvTypeName.pData, 0 ) != sal_False);
70 }
71 
72 /** Objects of this class are used for applying a current context until they are destructed, i.e.
73     the ctor of this class saves the previous and sets the given context while the dtor restores
74     the previous one upon destruction.
75 */
76 class ContextLayer
77 {
78     /** this C++ environment type name.
79         @internal
80     */
81     ::rtl::OUString m_aEnvTypeName;
82     /** previous context
83         @internal
84     */
85     Reference< XCurrentContext > m_xPreviousContext;
86 
87 public:
88     /** Constructor: Saves the previous context and sets the new (given) one.
89 
90         @param xNewContext new context to be set
91     */
92     inline ContextLayer(
93         Reference< XCurrentContext > const & xNewContext = Reference< XCurrentContext >() )
94         SAL_THROW( () );
95     /** Destructor: restores the previous context.
96     */
97     inline ~ContextLayer() SAL_THROW( () );
98 
99     /** Gets the previously set context.
100 
101         @return the previously set context
102     */
103     inline Reference< XCurrentContext > SAL_CALL getPreviousContext() const
104         SAL_THROW( () )
105         { return m_xPreviousContext; }
106 };
107 //__________________________________________________________________________________________________
108 inline ContextLayer::ContextLayer( Reference< XCurrentContext > const & xNewContext )
109     SAL_THROW( () )
110     : m_aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) )
111 {
112 	::uno_getCurrentContext( (void **)&m_xPreviousContext, m_aEnvTypeName.pData, 0 );
113 	::uno_setCurrentContext( xNewContext.get(), m_aEnvTypeName.pData, 0 );
114 }
115 //__________________________________________________________________________________________________
116 inline ContextLayer::~ContextLayer()
117     SAL_THROW( () )
118 {
119 	::uno_setCurrentContext( m_xPreviousContext.get(), m_aEnvTypeName.pData, 0 );
120 }
121 
122 }
123 }
124 }
125 }
126 
127 #endif
128