| 1 | #cxx . . $ `<file>/melder/melder_error.h`/ifndef _melder_error_h_ |
| 2 | #define _melder_error_h_cxx . . $ `melder/melder_error.h:2:9`! |
| 3 | /* melder_error.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2021,2025 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | #include <mutex> |
| 21 | |
| 22 | class MelderErrorcxx . . $ MelderError# { }; // empty exception class |
| 23 | |
| 24 | extern stdcxx . . $ std/::mutexcxx . . $ std/mutex# theMelder_error_mutexcxx . . $ theMelder_error_mutex.; |
| 25 | extern stdcxx . . $ std/::atomiccxx . . $ std/atomic# <integercxx . . $ integer#> theMelder_error_threadIdcxx . . $ theMelder_error_threadId.; |
| 26 | |
| 27 | bool Melder_hasErrorcxx . . $ Melder_hasError(c9857a73d68e59ba). (); |
| 28 | bool Melder_hasErrorcxx . . $ Melder_hasError(2df281243f91fe91). (conststring32cxx . . $ conststring32# partialError); |
| 29 | /* Returns true if there is an error message in store, otherwise false. */ |
| 30 | bool Melder_hasCrashcxx . . $ Melder_hasCrash(c9857a73d68e59ba). (); |
| 31 | /* Returns true if there is an error message in store, and that error message is a crash message. */ |
| 32 | |
| 33 | void Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 34 | /* Cancel all stored error messages. */ |
| 35 | |
| 36 | conststring32cxx . . $ conststring32# Melder_getErrorcxx . . $ Melder_getError(1912f6684645c851). (); |
| 37 | /* Returns the error string. Mainly used with str32str. */ |
| 38 | |
| 39 | void Melder_appendError_noLinecxx . . $ Melder_appendError_noLine(afb30bc799649a62). (const MelderArgcxx . . $ MelderArg#& arg1); |
| 40 | |
| 41 | /** |
| 42 | The usual error reporting function is Melder_throw. However, |
| 43 | you may sometimes want to prepend other messages before Melder_throw, |
| 44 | without jumping away from the error-generating location. |
| 45 | In such a special case you can use Melder_appendError. |
| 46 | |
| 47 | Melder_appendError() has to be followed by one of these: |
| 48 | - Melder_throw() (or just `throw`) to prepend the error to a normal exception; |
| 49 | - Melder_flushError() to show the error in the GUI |
| 50 | (this is where a trail of Melder_throw will usually end up as well); |
| 51 | - Melder_clearError() to ignore the error. |
| 52 | |
| 53 | If you don't do this, the error will linger in your error buffer until |
| 54 | the next, probably unrelated, error is generated, |
| 55 | and your prepended error text will be shown to the user out of context, |
| 56 | which is wrong. |
| 57 | */ |
| 58 | |
| 59 | void MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (conststring32cxx . . $ conststring32# message); |
| 60 | |
| 61 | template <typename... Arg> |
| 62 | void Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (const Arg... arg) { |
| 63 | stdcxx . . $ std/::lock_guard lockcxx . . $ std/lock_guard#lock_guard(8e935d9c2e576e3a). (theMelder_error_mutexcxx . . $ theMelder_error_mutex.); |
| 64 | if (Melder_hasErrorcxx . . $ Melder_hasError(c9857a73d68e59ba). ()) { |
| 65 | if (Melder_thisThread_getUniqueIDcxx . . $ Melder_thisThread_getUniqueID(da6335ded91d79c2). () != theMelder_error_threadIdcxx . . $ theMelder_error_threadId.) |
| 66 | return; |
| 67 | } else { |
| 68 | theMelder_error_threadIdcxx . . $ theMelder_error_threadId. =cxx . . $ std/__atomic_base#`operator=`(73de6b30e1938209). Melder_thisThread_getUniqueIDcxx . . $ Melder_thisThread_getUniqueID(da6335ded91d79c2). (); |
| 69 | } |
| 70 | (// fold |
| 71 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (MelderArgcxx . . $ MelderArg# { arg }. _argcxx . . $ MelderArg#_arg.) |
| 72 | , ... |
| 73 | ); |
| 74 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (U"\n"); |
| 75 | } |
| 76 | |
| 77 | #define Melder_throwcxx . . $ `melder/melder_error.h:77:9`!(...) do { Melder_appendError (__VA_ARGS__); throw MelderError (); } while (false) |
| 78 | |
| 79 | void Melder_flushErrorcxx . . $ Melder_flushError(49f6e7a06ebc5aa8). (); |
| 80 | |
| 81 | template <typename... Arg> |
| 82 | void Melder_flushErrorcxx . . $ Melder_flushError(6298c93241566110). (const Arg... arg) { |
| 83 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (arg...); |
| 84 | Melder_flushErrorcxx . . $ Melder_flushError(49f6e7a06ebc5aa8). (); |
| 85 | } |
| 86 | /* Send all deferred error messages to stderr (batch) or to an "Error" dialog, */ |
| 87 | /* including, if there are arguments, the error message generated by this function. */ |
| 88 | |
| 89 | void Melder_setErrorProccxx . . $ Melder_setErrorProc(f441b548652834d9). (void (*p_errorProc) (conststring32cxx . . $ conststring32#)); |
| 90 | |
| 91 | void _private_Melder_crashcxx . . $ _private_Melder_crash(6658699d5eee445d). (const MelderArgcxx . . $ MelderArg#&, |
| 92 | const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., |
| 93 | const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., |
| 94 | const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., |
| 95 | const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec)., const MelderArgcxx . . $ MelderArg#& = U""cxx . . $ MelderArg#MelderArg(ff00d524ab0355ec). |
| 96 | ); |
| 97 | |
| 98 | #define Melder_crashcxx . . $ `melder/melder_error.h:98:9`!(...) do { _private_Melder_crash (__VA_ARGS__); abort (); } while (0) |
| 99 | |
| 100 | void Melder_setCrashProccxx . . $ Melder_setCrashProc(f441b548652834d9). (void (*p_crashProc) (conststring32cxx . . $ conststring32#)); |
| 101 | |
| 102 | /* End of file melder_error.h */ |
| 103 | #endif |