Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
exception.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
21
23#include <stdexcept>
24#include <string>
25
26namespace shambase {
27
39 std::string exception_format(SourceLocation loc);
40
42 using exception_gen_callback_t = void (*)(std::string msg);
43
54
63
65 class scoped_exception_gen_callback {
66 private:
67 exception_gen_callback_t old_callback;
68
69 public:
70 scoped_exception_gen_callback(exception_gen_callback_t callback)
71 : old_callback(get_exception_gen_callback()) {
73 }
74 ~scoped_exception_gen_callback() { set_exception_gen_callback(old_callback); }
75
76 scoped_exception_gen_callback(const scoped_exception_gen_callback &) = delete;
77 scoped_exception_gen_callback &operator=(const scoped_exception_gen_callback &) = delete;
78 scoped_exception_gen_callback(scoped_exception_gen_callback &&) = delete;
79 scoped_exception_gen_callback &operator=(scoped_exception_gen_callback &&) = delete;
80 };
81
90 void exception_gen_callback(std::string msg);
91
104 template<class ExcptTypes>
105 inline ExcptTypes make_except_with_loc(
106 std::string message, SourceLocation loc = SourceLocation{}) {
107 std::string msg = message + exception_format(loc);
109 return ExcptTypes(msg);
110 }
111
131 template<class ExcptTypes>
132 inline void throw_with_loc(std::string message, SourceLocation loc = SourceLocation{}) {
134 }
135
154 throw_with_loc<std::runtime_error>("unimplemented", loc);
155 }
156
175 inline void throw_unimplemented(std::string message, SourceLocation loc = SourceLocation{}) {
176 throw_with_loc<std::runtime_error>(message + "\nunimplemented", loc);
177 }
178
179} // namespace shambase
Source location utility.
namespace for basic c++ utilities
void set_exception_gen_callback(exception_gen_callback_t callback)
Set the exception generator callback.
Definition exception.cpp:39
std::string exception_format(SourceLocation loc)
Format the exception message with the source location information.
Definition exception.cpp:26
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void(*)(std::string msg) exception_gen_callback_t
type of the exception generator callback
Definition exception.hpp:42
void exception_gen_callback(std::string msg)
The callback called when an exception is thrown.
Definition exception.cpp:33
exception_gen_callback_t get_exception_gen_callback()
Get the current exception generator callback.
Definition exception.cpp:43
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
provide information about the source location