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