LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basic / source / runtime / basrdll.cxx
blobba94fd7b9d2b1b24e0fb2c2f2233f5273d530ce8
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 .
20 #include <memory>
21 #include <mutex>
23 #include <vcl/svapp.hxx>
24 #include <tools/debug.hxx>
25 #include <vcl/weld.hxx>
27 #include <basic/sbstar.hxx>
28 #include <basic/basrdll.hxx>
29 #include <strings.hrc>
30 #include <sbxbase.hxx>
31 #include <config_features.h>
33 namespace
35 struct BasicDLLImpl : public SvRefBase
37 bool bDebugMode;
38 bool bBreakEnabled;
40 std::unique_ptr<SbxAppData> xSbxAppData;
42 BasicDLLImpl()
43 : bDebugMode(false)
44 , bBreakEnabled(true)
45 , xSbxAppData(new SbxAppData)
46 { }
48 static BasicDLLImpl* BASIC_DLL;
49 static std::mutex& getMutex()
51 static std::mutex aMutex;
52 return aMutex;
56 BasicDLLImpl* BasicDLLImpl::BASIC_DLL = nullptr;
59 BasicDLL::BasicDLL()
61 std::scoped_lock aGuard(BasicDLLImpl::getMutex());
62 if (!BasicDLLImpl::BASIC_DLL)
63 BasicDLLImpl::BASIC_DLL = new BasicDLLImpl;
64 m_xImpl = BasicDLLImpl::BASIC_DLL;
67 BasicDLL::~BasicDLL()
69 std::scoped_lock aGuard(BasicDLLImpl::getMutex());
70 const bool bLastRef = m_xImpl->GetRefCount() == 1;
71 if (bLastRef) {
72 BasicDLLImpl::BASIC_DLL->xSbxAppData->m_aGlobErr.clear();
74 m_xImpl.clear();
75 // only reset BASIC_DLL after the object had been destroyed
76 if (bLastRef)
77 BasicDLLImpl::BASIC_DLL = nullptr;
80 void BasicDLL::EnableBreak( bool bEnable )
82 DBG_ASSERT( BasicDLLImpl::BASIC_DLL, "BasicDLL::EnableBreak: No instance yet!" );
83 if (BasicDLLImpl::BASIC_DLL)
85 BasicDLLImpl::BASIC_DLL->bBreakEnabled = bEnable;
89 void BasicDLL::SetDebugMode( bool bDebugMode )
91 DBG_ASSERT( BasicDLLImpl::BASIC_DLL, "BasicDLL::EnableBreak: No instance yet!" );
92 if (BasicDLLImpl::BASIC_DLL)
94 BasicDLLImpl::BASIC_DLL->bDebugMode = bDebugMode;
99 void BasicDLL::BasicBreak()
101 DBG_ASSERT( BasicDLLImpl::BASIC_DLL, "BasicDLL::EnableBreak: No instance yet!" );
102 #if HAVE_FEATURE_SCRIPTING
103 if (!BasicDLLImpl::BASIC_DLL)
104 return;
106 // bJustStopping: if there's someone pressing STOP like crazy umpteen times,
107 // but the Basic doesn't stop early enough, the box might appear more often...
108 static bool bJustStopping = false;
109 if (StarBASIC::IsRunning() && !bJustStopping
110 && (BasicDLLImpl::BASIC_DLL->bBreakEnabled || BasicDLLImpl::BASIC_DLL->bDebugMode))
112 bJustStopping = true;
113 StarBASIC::Stop();
114 std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(nullptr,
115 VclMessageType::Info, VclButtonsType::Ok,
116 BasResId(IDS_SBERR_TERMINATED)));
117 xInfoBox->run();
118 bJustStopping = false;
120 #endif
123 SbxAppData& GetSbxData_Impl()
125 return *BasicDLLImpl::BASIC_DLL->xSbxAppData;
128 bool IsSbxData_Impl()
130 return BasicDLLImpl::BASIC_DLL && BasicDLLImpl::BASIC_DLL->xSbxAppData;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */