From f59395249f60becf4d59f64120d74cdcef7834ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Mon, 15 Apr 2024 15:41:03 +0200 Subject: [PATCH] Move part of errors.h to a new errors.cpp file No real changes, just refactoring. --- Poedit.vcxproj | 1 + Poedit.vcxproj.filters | 3 ++ Poedit.xcodeproj/project.pbxproj | 6 +++ src/Makefile.am | 2 +- src/concurrency.cpp | 4 +- src/errors.cpp | 56 ++++++++++++++++++++++++++ src/errors.h | 67 ++++++++++++++++---------------- 7 files changed, 102 insertions(+), 37 deletions(-) create mode 100644 src/errors.cpp diff --git a/Poedit.vcxproj b/Poedit.vcxproj index dbe0456de8..0ab8793585 100644 --- a/Poedit.vcxproj +++ b/Poedit.vcxproj @@ -135,6 +135,7 @@ + diff --git a/Poedit.vcxproj.filters b/Poedit.vcxproj.filters index c78cd7bf16..be3e01c8ec 100644 --- a/Poedit.vcxproj.filters +++ b/Poedit.vcxproj.filters @@ -210,6 +210,9 @@ Source Files + + Source Files + diff --git a/Poedit.xcodeproj/project.pbxproj b/Poedit.xcodeproj/project.pbxproj index e792856f37..db2ec2260d 100644 --- a/Poedit.xcodeproj/project.pbxproj +++ b/Poedit.xcodeproj/project.pbxproj @@ -94,6 +94,8 @@ B26E2C8F25A245BD008D6DF1 /* CloseButtonTemplate@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B26E2C8B25A245BC008D6DF1 /* CloseButtonTemplate@2x.png */; }; B26E2C9025A245BD008D6DF1 /* CloseButtonTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = B26E2C8C25A245BC008D6DF1 /* CloseButtonTemplate.png */; }; B26E2C9125A245BD008D6DF1 /* CloseButtonHoverTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = B26E2C8D25A245BD008D6DF1 /* CloseButtonHoverTemplate.png */; }; + B273818C2BD5027E005F24DA /* errors.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B273818B2BD5027E005F24DA /* errors.cpp */; }; + B273818D2BD5027E005F24DA /* errors.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B273818B2BD5027E005F24DA /* errors.cpp */; }; B27959DE1E85850A00DBA47D /* qa_checks.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B27959DC1E85850A00DBA47D /* qa_checks.cpp */; }; B280E84D1A92776D009F4A98 /* http_client_macos.mm in Sources */ = {isa = PBXBuildFile; fileRef = B280E84B1A92776D009F4A98 /* http_client_macos.mm */; }; B28602441DDB279400FCA617 /* colorscheme.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28602421DDB279400FCA617 /* colorscheme.cpp */; }; @@ -388,6 +390,7 @@ B26E2C8B25A245BC008D6DF1 /* CloseButtonTemplate@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "CloseButtonTemplate@2x.png"; path = "macos/CloseButtonTemplate@2x.png"; sourceTree = ""; }; B26E2C8C25A245BC008D6DF1 /* CloseButtonTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = CloseButtonTemplate.png; path = macos/CloseButtonTemplate.png; sourceTree = ""; }; B26E2C8D25A245BD008D6DF1 /* CloseButtonHoverTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = CloseButtonHoverTemplate.png; path = macos/CloseButtonHoverTemplate.png; sourceTree = ""; }; + B273818B2BD5027E005F24DA /* errors.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = errors.cpp; sourceTree = ""; }; B27959DC1E85850A00DBA47D /* qa_checks.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = qa_checks.cpp; sourceTree = ""; }; B27959DD1E85850A00DBA47D /* qa_checks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = qa_checks.h; sourceTree = ""; }; B27D1FC519EFFA2800AB1913 /* da */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = da; path = da.lproj/InfoPlist.strings; sourceTree = ""; }; @@ -759,6 +762,7 @@ B21B7B481DD4DB9F002A4C62 /* editing_area.h */, B28F1CBC16F629D30018AF7E /* edlistctrl.cpp */, B28F1CBD16F629D30018AF7E /* edlistctrl.h */, + B273818B2BD5027E005F24DA /* errors.cpp */, B29AE89B17105306008D1F8A /* errors.h */, B28F1CE216F629D30018AF7E /* export_html.cpp */, B238F674261237C4002D6845 /* filemonitor.cpp */, @@ -1503,6 +1507,7 @@ B28F1CF916F629D30018AF7E /* progressinfo.cpp in Sources */, B28F1CFA16F629D30018AF7E /* propertiesdlg.cpp in Sources */, B26D0655182697200069C378 /* welcomescreen.cpp in Sources */, + B273818C2BD5027E005F24DA /* errors.cpp in Sources */, B2E11F121A2C66FB00E4E42C /* text_control.cpp in Sources */, B28F1CFB16F629D30018AF7E /* cat_update.cpp in Sources */, B2CE2FEF1A94EBF50020A620 /* crowdin_client.cpp in Sources */, @@ -1533,6 +1538,7 @@ B260089529AF88A000349A0E /* catalog_json.cpp in Sources */, B201EBE41DCF8C0100FFB541 /* configuration.cpp in Sources */, B2DAD7121AD198DE00DCB398 /* language.cpp in Sources */, + B273818D2BD5027E005F24DA /* errors.cpp in Sources */, B201EBE31DCF8BFD00FFB541 /* catalog.cpp in Sources */, B2DAD70F1AD1984200DCB398 /* utility.cpp in Sources */, B260AA682BB2BDAE0003E378 /* unicode_helpers.cpp in Sources */, diff --git a/src/Makefile.am b/src/Makefile.am index 109a8a5649..e79eb92390 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -39,7 +39,7 @@ poedit_SOURCES = \ edframe.cpp edframe.h \ editing_area.cpp editing_area.h \ edlistctrl.cpp edlistctrl.h \ - errors.h \ + errors.cpp errors.h \ export_html.cpp \ extractors/extractor.cpp extractors/extractor.h \ extractors/extractor_gettext.cpp \ diff --git a/src/concurrency.cpp b/src/concurrency.cpp index d244ea8d78..732f981ed2 100644 --- a/src/concurrency.cpp +++ b/src/concurrency.cpp @@ -117,8 +117,8 @@ void detail::dispatch_async_cxx(boost::executors::work&& f, detail::queue q) namespace { -std::unique_ptr gs_background_executor; -std::unique_ptr gs_main_thread_executor; +std::unique_ptr gs_background_executor; +std::unique_ptr gs_main_thread_executor; static std::once_flag gs_background_executor_flag, gs_main_thread_executor_flag; } diff --git a/src/errors.cpp b/src/errors.cpp new file mode 100644 index 0000000000..7e3198ed87 --- /dev/null +++ b/src/errors.cpp @@ -0,0 +1,56 @@ +/* + * This file is part of Poedit (https://poedit.net) + * + * Copyright (C) 2013-2023 Vaclav Slavik + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include "errors.h" + +wxString errors::detail::DescribeExceptionImpl(Rethrower& rethrower) +{ + try + { + rethrower.rethrow(); + return "no error"; // silence stupid VC++ + } + catch (const Exception& e) + { + return e.What(); + } + catch (const std::exception& e) + { + const char *msg = e.what(); + // try interpreting as UTF-8 first as the most likely one (from external sources) + wxString s = wxString::FromUTF8(msg); + if (s.empty()) + { + s = wxString(msg); + if (s.empty()) // not in current locale either, fall back to Latin1 + s = wxString(msg, wxConvISO8859_1); + } + return s; + } + catch (...) + { + return "unknown error"; + } +} diff --git a/src/errors.h b/src/errors.h index 9cf33c080b..51be31c07b 100644 --- a/src/errors.h +++ b/src/errors.h @@ -23,12 +23,13 @@ * */ -#ifndef _ERRORS_H_ -#define _ERRORS_H_ +#ifndef Poedit_errors_h +#define Poedit_errors_h #include #include #include +#include #include @@ -55,46 +56,44 @@ class Exception : public std::runtime_error }; -/// Helper to convert an exception into a human-readable string -template -inline wxString DoDescribeException(Rethrow&& rethrow_exception) +namespace errors::detail +{ + +struct Rethrower { - try - { - rethrow_exception(); - return "no error"; // silence stupid VC++ - } - catch (const Exception& e) - { - return e.What(); - } - catch (const std::exception& e) - { - const char *msg = e.what(); - // try interpreting as UTF-8 first as the most likely one (from external sources) - wxString s = wxString::FromUTF8(msg); - if (s.empty()) - { - s = wxString(msg); - if (s.empty()) // not in current locale either, fall back to Latin1 - s = wxString(msg, wxConvISO8859_1); - } - return s; - } - catch (...) - { - return "unknown error"; - } + virtual void rethrow() = 0; + virtual ~Rethrower() {} +}; + +template +struct RethrowerImpl : public Rethrower +{ + RethrowerImpl(T&& func) : rethrow_exception(std::move(func)) {} + void rethrow() override { rethrow_exception(); } + + T rethrow_exception; +}; + +wxString DescribeExceptionImpl(Rethrower& rethrower); + +template +inline wxString DescribeException(T&& rethrow_exception) +{ + RethrowerImpl rethrower(std::move(rethrow_exception)); + return DescribeExceptionImpl(rethrower); } +} // namespace errors::detail + +/// Helper to convert an exception into a human-readable string inline wxString DescribeException(std::exception_ptr e) { - return DoDescribeException([e]{ std::rethrow_exception(e); }); + return errors::detail::DescribeException([e]{ std::rethrow_exception(e); }); } inline wxString DescribeException(boost::exception_ptr e) { - return DoDescribeException([e]{ boost::rethrow_exception(e); }); + return errors::detail::DescribeException([e]{ boost::rethrow_exception(e); }); } inline wxString DescribeCurrentException() @@ -102,4 +101,4 @@ inline wxString DescribeCurrentException() return DescribeException(std::current_exception()); } -#endif // _ERRORS_H_ +#endif // Poedit_errors_h