Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / include / uno / current_context.hxx
blobd4d5608f83af95b492ce7be55f7783f6927cde30
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_UNO_CURRENT_CONTEXT_HXX
20 #define INCLUDED_UNO_CURRENT_CONTEXT_HXX
22 #include <sal/config.h>
24 #include <cstddef>
26 #include <uno/current_context.h>
27 #include <uno/lbnames.h>
29 #include <com/sun/star/uno/XCurrentContext.hpp>
32 namespace com
34 namespace sun
36 namespace star
38 namespace uno
41 /** Getting the current context.
42 @attention
43 Don't spread the returned interface around to other threads. Every thread has its own
44 current context.
46 @return current context or null ref, if none is set
48 inline Reference< XCurrentContext > SAL_CALL getCurrentContext()
50 Reference< XCurrentContext > xRet;
51 ::rtl::OUString aEnvTypeName( CPPU_CURRENT_LANGUAGE_BINDING_NAME );
52 ::uno_getCurrentContext( reinterpret_cast<void **>(&xRet), aEnvTypeName.pData, NULL );
53 return xRet;
55 /** Setting the current context.
57 @param xContext current context to be set
58 @return true, if context has been successfully set
60 inline bool SAL_CALL setCurrentContext(
61 Reference< XCurrentContext > const & xContext )
63 ::rtl::OUString aEnvTypeName( CPPU_CURRENT_LANGUAGE_BINDING_NAME );
64 return ::uno_setCurrentContext( xContext.get(), aEnvTypeName.pData, NULL );
67 /** Objects of this class are used for applying a current context until they are destructed, i.e.
68 the ctor of this class saves the previous and sets the given context while the dtor restores
69 the previous one upon destruction.
71 class ContextLayer
73 /** this C++ environment type name.
75 ::rtl::OUString m_aEnvTypeName;
76 /** previous context
78 Reference< XCurrentContext > m_xPreviousContext;
80 public:
81 /** Constructor: Saves the previous context and sets the new (given) one.
83 @param xNewContext new context to be set
85 inline ContextLayer(
86 Reference< XCurrentContext > const & xNewContext = Reference< XCurrentContext >() );
87 /** Destructor: restores the previous context.
89 inline ~ContextLayer();
91 /** Gets the previously set context.
93 @return the previously set context
95 inline Reference< XCurrentContext > SAL_CALL getPreviousContext() const
96 { return m_xPreviousContext; }
99 inline ContextLayer::ContextLayer( Reference< XCurrentContext > const & xNewContext )
100 : m_aEnvTypeName( CPPU_CURRENT_LANGUAGE_BINDING_NAME )
102 ::uno_getCurrentContext( reinterpret_cast<void **>(&m_xPreviousContext), m_aEnvTypeName.pData, NULL );
103 ::uno_setCurrentContext( xNewContext.get(), m_aEnvTypeName.pData, NULL );
106 inline ContextLayer::~ContextLayer()
108 ::uno_setCurrentContext( m_xPreviousContext.get(), m_aEnvTypeName.pData, NULL );
116 #endif
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */