LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / include / comphelper / SetFlagContextHelper.hxx
blob1f1e2743cea9c1972f562248a5da9f833b3c04fe
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/.
8 */
10 #ifndef INCLUDED_COMPHELPER_SETFLAGCONTEXTHELPER_HXX
11 #define INCLUDED_COMPHELPER_SETFLAGCONTEXTHELPER_HXX
13 #include <com/sun/star/uno/XCurrentContext.hpp>
14 #include <cppuhelper/implbase.hxx>
15 #include <uno/current_context.hxx>
17 namespace comphelper
19 // Used to flag some named value to be true for all code running in this context
20 class SetFlagContext final : public cppu::WeakImplHelper<css::uno::XCurrentContext>
22 public:
23 explicit SetFlagContext(const OUString& sName,
24 css::uno::Reference<css::uno::XCurrentContext> const& xContext)
25 : m_sName(sName)
26 , mxNextContext(xContext)
29 SetFlagContext(const SetFlagContext&) = delete;
30 SetFlagContext& operator=(const SetFlagContext&) = delete;
32 virtual css::uno::Any SAL_CALL getValueByName(OUString const& Name) override
34 if (Name == m_sName)
35 return css::uno::Any(true);
36 else if (mxNextContext.is())
37 return mxNextContext->getValueByName(Name);
38 else
39 return css::uno::Any();
42 private:
43 OUString m_sName;
44 css::uno::Reference<css::uno::XCurrentContext> mxNextContext;
47 // Returns a new context that reports the named value to be true
48 inline css::uno::Reference<css::uno::XCurrentContext> NewFlagContext(const OUString& sName)
50 return new SetFlagContext(sName, css::uno::getCurrentContext());
53 // A specialization for preventing "Java must be enabled" interaction
54 inline css::uno::Reference<css::uno::XCurrentContext> NoEnableJavaInteractionContext()
56 return NewFlagContext("DontEnableJava");
59 inline bool IsContextFlagActive(const OUString& sName)
61 bool bFlag = false;
62 if (const auto xContext = css::uno::getCurrentContext())
63 xContext->getValueByName(sName) >>= bFlag;
64 return bFlag;
67 } // namespace comphelper
69 #endif // INCLUDED_COMPHELPER_SETFLAGCONTEXTHELPER_HXX
71 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */