| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2023 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
|
| 16 |
#ifndef dealii_sundials_utilities_h |
| 17 |
#define dealii_sundials_utilities_h |
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/exceptions.h> |
| 22 |
|
| 23 |
|
| 24 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 25 |
# include <exception> |
| 26 |
|
| 27 |
|
| 28 |
DEAL_II_NAMESPACE_OPEN |
| 29 |
|
| 30 |
namespace SUNDIALS |
| 31 |
{ |
| 32 |
namespace Utilities |
| 33 |
{ |
| 34 |
/** |
| 35 |
* A function that calls the function object given by its first argument |
| 36 |
* with the set of arguments following at the end. If the call returns |
| 37 |
* regularly, the current function returns zero to indicate success. If the |
| 38 |
* call fails with an exception of type RecoverableUserCallbackError, then |
| 39 |
* the current function returns 1 to indicate that the called function |
| 40 |
* object thought the error it encountered is recoverable. If the call fails |
| 41 |
* with any other exception, then the current function returns with an error |
| 42 |
* code of -1. In each of the last two cases, the exception thrown by `f` |
| 43 |
* is captured and `eptr` is set to the exception. In case of success, |
| 44 |
* `eptr` is set to `nullptr`. |
| 45 |
*/ |
| 46 |
template <typename F, typename... Args> |
| 47 |
int |
| 48 |
call_and_possibly_capture_exception(const F &f, |
| 49 |
std::exception_ptr &eptr, |
| 50 |
Args &&...args) |
| 51 |
{ |
| 52 |
// See whether there is already something in the exception pointer |
| 53 |
// variable. This can only happen if we had previously had |
| 54 |
// a recoverable exception, and the underlying library actually |
| 55 |
// did recover successfully. In that case, we can abandon the |
| 56 |
// exception previously thrown. If eptr contains anything other, |
| 57 |
// then we really don't know how that could have happened, and |
| 58 |
// should probably bail out: |
| 59 |
if (eptr) |
| 60 |
{ |
| 61 |
try |
| 62 |
{ |
| 63 |
std::rethrow_exception(eptr); |
| 64 |
} |
| 65 |
catch (const RecoverableUserCallbackError &) |
| 66 |
{ |
| 67 |
// ok, ignore, but reset the pointer |
| 68 |
eptr = nullptr; |
| 69 |
} |
| 70 |
catch (...) |
| 71 |
{ |
| 72 |
// uh oh: |
| 73 |
AssertThrow(false, ExcInternalError()); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Call the function and if that succeeds, return zero: |
| 78 |
try |
| 79 |
{ |
| 80 |
f(std::forward<Args>(args)...); |
| 81 |
eptr = nullptr; |
| 82 |
return 0; |
| 83 |
} |
| 84 |
// If the call failed with a recoverable error, then |
| 85 |
// ignore the exception for now (but store a pointer to it) |
| 86 |
// and return a positive return value (+1). If the underlying |
| 87 |
// implementation manages to recover |
| 88 |
catch (const RecoverableUserCallbackError &) |
| 89 |
{ |
| 90 |
eptr = std::current_exception(); |
| 91 |
return 1; |
| 92 |
} |
| 93 |
// For any other exception, capture the exception and |
| 94 |
// return -1: |
| 95 |
catch (const std::exception &) |
| 96 |
{ |
| 97 |
eptr = std::current_exception(); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
} |
| 101 |
} // namespace Utilities |
| 102 |
} // namespace SUNDIALS |
| 103 |
|
| 104 |
DEAL_II_NAMESPACE_CLOSE |
| 105 |
|
| 106 |
#else |
| 107 |
|
| 108 |
// Make sure the scripts that create the C++20 module input files have |
| 109 |
// something to latch on if the preprocessor #ifdef above would |
| 110 |
// otherwise lead to an empty content of the file. |
| 111 |
DEAL_II_NAMESPACE_OPEN |
| 112 |
DEAL_II_NAMESPACE_CLOSE |
| 113 |
|
| 114 |
#endif // DEAL_II_WITH_SUNDIALS |
| 115 |
|
| 116 |
#endif |