Shamrock
2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
shambase
include
shambase
exception_ctx.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
19
#include "
shambase/SourceLocation.hpp
"
20
#include "
shambase/exception.hpp
"
21
#include "
shambase/string.hpp
"
22
#include <string>
23
#include <vector>
24
25
namespace
shambase
{
26
33
struct
args_info
{
34
std::string name;
35
std::string value;
36
37
args_info
() =
default
;
38
39
template
<
class
T>
40
args_info
(std::string name,
const
T &value) : name(std::move(name)) {
41
try
{
42
this->value = shambase::format(
"{}"
, value);
43
}
catch
(
const
std::exception &e) {
44
this->value =
"format failed : "
+ std::string(e.what());
45
}
46
}
47
};
48
55
struct
arg_group
{
56
std::string section_name;
57
std::vector<args_info> args;
58
59
arg_group
() =
default
;
60
61
template
<
typename
...
Args
>
62
arg_group
(std::string name,
Args
&&...
args_list
)
63
: section_name(std::move(name)), args{std::forward<Args>(
args_list
)...} {}
64
};
65
72
struct
context
{
73
std::vector<arg_group> groups;
74
75
context
() =
default
;
76
77
template
<
typename
...
Args
>
78
context
(
Args
&&...
args_list
) : groups{std::forward<Args>(
args_list
)...} {}
79
};
80
107
template
<
class
exception_type>
108
inline
exception_type
make_except_with_loc_with_ctx
(
109
std::string
message
,
context
ctx,
SourceLocation
loc =
SourceLocation
{}) {
110
std::string
msg
;
111
auto
out
= std::back_inserter(
msg
);
112
fmt::format_to(
out
,
"{}\nexception context :\n"
,
message
);
113
114
for
(
const
auto
&
group
: ctx.groups) {
115
fmt::format_to(
out
,
" {}:\n"
,
group
.section_name);
116
for
(
const
auto
&
arg
:
group
.args) {
117
fmt::format_to(
out
,
" {} = {}\n"
,
arg
.name,
arg
.value);
118
}
119
}
120
121
return
shambase::make_except_with_loc<exception_type>
(
msg
, loc);
122
}
123
124
}
// namespace shambase
125
138
#define ARG_INFO(var) shambase::args_info(#var, var)
SourceLocation.hpp
Source location utility.
exception.hpp
This header file contains utility functions related to exception handling in the code.
shambase
namespace for basic c++ utilities
Definition
sycl_builtins.hpp:25
shambase::throw_with_loc
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
Definition
exception.hpp:132
shambase::make_except_with_loc_with_ctx
exception_type make_except_with_loc_with_ctx(std::string message, context ctx, SourceLocation loc=SourceLocation{})
Make an exception with a message and variadic context groups.
Definition
exception_ctx.hpp:108
string.hpp
SourceLocation
provide information about the source location
Definition
SourceLocation.hpp:30
shambase::arg_group
A context group containing a section name and a list of arguments.
Definition
exception_ctx.hpp:55
shambase::args_info
An argument containing a name and a value.
Definition
exception_ctx.hpp:33
shambase::context
A context containing a list of argument groups.
Definition
exception_ctx.hpp:72
Generated on Wed May 13 2026 13:21:49 for Shamrock by
1.9.8