Line data Source code
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 : #ifndef _UNO_CURRENT_CONTEXT_HXX_
20 : #define _UNO_CURRENT_CONTEXT_HXX_
21 :
22 : #include <uno/current_context.h>
23 :
24 : #include <com/sun/star/uno/XCurrentContext.hpp>
25 :
26 :
27 : namespace com
28 : {
29 : namespace sun
30 : {
31 : namespace star
32 : {
33 : namespace uno
34 : {
35 :
36 : /** Getting the current context.
37 : @attention
38 : Don't spread the returned interface around to other threads. Every thread has its own
39 : current context.
40 :
41 : @return current context or null ref, if none is set
42 : */
43 36 : inline Reference< XCurrentContext > SAL_CALL getCurrentContext()
44 : SAL_THROW(())
45 : {
46 36 : Reference< XCurrentContext > xRet;
47 36 : ::rtl::OUString aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
48 36 : ::uno_getCurrentContext( (void **)&xRet, aEnvTypeName.pData, 0 );
49 36 : return xRet;
50 : }
51 : /** Setting the current context.
52 :
53 : @param xContext current context to be set
54 : @return true, if context has been successfully set
55 : */
56 32 : inline bool SAL_CALL setCurrentContext(
57 : Reference< XCurrentContext > const & xContext )
58 : SAL_THROW(())
59 : {
60 32 : ::rtl::OUString aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
61 32 : return (::uno_setCurrentContext( xContext.get(), aEnvTypeName.pData, 0 ) != sal_False);
62 : }
63 :
64 : /** Objects of this class are used for applying a current context until they are destructed, i.e.
65 : the ctor of this class saves the previous and sets the given context while the dtor restores
66 : the previous one upon destruction.
67 : */
68 : class ContextLayer
69 : {
70 : /** this C++ environment type name.
71 : */
72 : ::rtl::OUString m_aEnvTypeName;
73 : /** previous context
74 : */
75 : Reference< XCurrentContext > m_xPreviousContext;
76 :
77 : public:
78 : /** Constructor: Saves the previous context and sets the new (given) one.
79 :
80 : @param xNewContext new context to be set
81 : */
82 : inline ContextLayer(
83 : Reference< XCurrentContext > const & xNewContext = Reference< XCurrentContext >() )
84 : SAL_THROW(());
85 : /** Destructor: restores the previous context.
86 : */
87 : inline ~ContextLayer() SAL_THROW(());
88 :
89 : /** Gets the previously set context.
90 :
91 : @return the previously set context
92 : */
93 : inline Reference< XCurrentContext > SAL_CALL getPreviousContext() const
94 : SAL_THROW(())
95 : { return m_xPreviousContext; }
96 : };
97 : //__________________________________________________________________________________________________
98 4 : inline ContextLayer::ContextLayer( Reference< XCurrentContext > const & xNewContext )
99 : SAL_THROW(())
100 4 : : m_aEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) )
101 : {
102 4 : ::uno_getCurrentContext( (void **)&m_xPreviousContext, m_aEnvTypeName.pData, 0 );
103 4 : ::uno_setCurrentContext( xNewContext.get(), m_aEnvTypeName.pData, 0 );
104 4 : }
105 : //__________________________________________________________________________________________________
106 8 : inline ContextLayer::~ContextLayer()
107 : SAL_THROW(())
108 : {
109 4 : ::uno_setCurrentContext( m_xPreviousContext.get(), m_aEnvTypeName.pData, 0 );
110 4 : }
111 :
112 : }
113 : }
114 : }
115 : }
116 :
117 : #endif
118 :
119 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|