ADOL-C: Automatic Differentiation in C/C++ 2.7.3
Loading...
Searching...
No Matches
adolcerror.h
Go to the documentation of this file.
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File: adolcerror.h
4 Revision: $Id$
5 Contents: handling of fatal errors
6
7 Copyright (c) Kshitij Kulshreshtha
8
9 This file is part of ADOL-C. This software is provided as open source.
10 Any use, reproduction, or distribution of the software constitutes
11 recipient's acceptance of the terms of the accompanying license file.
12
13---------------------------------------------------------------------------*/
14
15#ifndef ADOLC_ERROR_H
16#define ADOLC_ERROR_H
17
18#include <adolc/adolcexport.h>
19#include <sstream>
20#include <stdexcept>
21#include <string>
22#include <string_view>
23#include <type_traits>
24
25// Macro to obtain location information easily
26#define CURRENT_LOCATION \
27 ADOLCError::ADOLCError::makeSourceLocation(__FILE__, __func__, __LINE__)
28
29namespace ADOLCError {
30
37
38 std::string_view file_;
39 std::string_view func_;
40 int line_;
41
42 constexpr source_location(std::string_view file, std::string_view func,
43 int line)
44 : file_(file), func_(func), line_(line) {}
45
46 constexpr std::string_view file() const { return file_; }
47 constexpr std::string_view func() const { return func_; }
48 constexpr int line() const { return line_; }
49};
56class ADOLCError : public std::runtime_error {
57
58 source_location info_;
59
60public:
66 ADOLCError(std::string_view message, const source_location &info)
67 : std::runtime_error(makeMessage(message, info)), info_(info) {}
68
69 constexpr static source_location
70 makeSourceLocation(std::string_view file, std::string_view func, int line) {
71 return source_location(file, func, line);
72 }
73
74 static std::string makeMessage(std::string_view message,
75 const source_location &info) {
76 std::ostringstream oss;
77 oss << message << "\n[Source] " << info.file() << ":" << info.func() << ":"
78 << info.line();
79 return oss.str();
80 }
81
86 const source_location &where() const noexcept { return info_; }
87};
88
89enum class ErrorType : size_t {
102 NO_MINMAX,
110
120
126
131
133
136
144
160
163
165
169
170 MYALLOC1,
171 MYALLOC2,
172 MYALLOC3,
173 MYALLOCI2,
177
182
189
190 FP_NO_EDF,
192
200
201 PARAM_OOB,
202
206
208
211
213
218
220
222};
223
225struct FailInfo {
226 short info1{0}; // for tapeId
227 size_t info2{0};
228 int info3{0}; // deps
229 int info4{0}; // num indeps
230 size_t info5{0};
231 size_t info6{0};
232 unsigned char info7{0}; // Operation char
233};
234
235// like c++23's to_underlying
236// help to statically cast an enum class to its parent type
237// used e.g. for enum class that inherits from size_t
238template <typename T> constexpr auto to_underlying(T t) noexcept {
239 return static_cast<std::underlying_type_t<T>>(t);
240}
241
242/*--------------------------------------------------------------------------*/
243// prints an error message describing the error type coming from a file function
244// (e.g. fopen)
246
247// would like to use always CURRENT_LOCATION as default but this
248// does not work for all compilers sometimes the funciton names are empty.
249// Currently you have to call CURRENT_LOCATION when calling fail
250// by hand
251void ADOLC_API fail(ErrorType error, const source_location LocInfo,
252 const FailInfo &failinfo = FailInfo());
253
254} // namespace ADOLCError
255#endif // ADOLC_ERROR_H
#define ADOLC_API
Definition adolcexport.h:16
ADOLCError(std::string_view message, const source_location &info)
Constructs an ADOLCError with message and source location.
Definition adolcerror.h:66
static std::string makeMessage(std::string_view message, const source_location &info)
Definition adolcerror.h:74
static constexpr source_location makeSourceLocation(std::string_view file, std::string_view func, int line)
Definition adolcerror.h:70
const source_location & where() const noexcept
Access the captured source location.
Definition adolcerror.h:86
Definition adolcerror.h:29
void ADOLC_API printError()
ErrorType
Definition adolcerror.h:89
@ CHECKPOINTING_NULLPOINTER_FUNCTION_DOUBLE
@ CHECKPOINTING_REVOLVE_IRREGULAR_TERMINATED
void ADOLC_API fail(ErrorType error, const source_location LocInfo, const FailInfo &failinfo=FailInfo())
constexpr auto to_underlying(T t) noexcept
Definition adolcerror.h:238
Additional typed fields used when formatting an ADOL-C failure.
Definition adolcerror.h:225
short info1
Definition adolcerror.h:226
int info4
Definition adolcerror.h:229
size_t info6
Definition adolcerror.h:231
int info3
Definition adolcerror.h:228
size_t info2
Definition adolcerror.h:227
size_t info5
Definition adolcerror.h:230
unsigned char info7
Definition adolcerror.h:232
Lightweight source-location record used by ADOL-C exceptions.
Definition adolcerror.h:36
int line_
Definition adolcerror.h:40
constexpr std::string_view file() const
Definition adolcerror.h:46
constexpr source_location(std::string_view file, std::string_view func, int line)
Definition adolcerror.h:42
constexpr std::string_view func() const
Definition adolcerror.h:47
std::string_view file_
Definition adolcerror.h:38
constexpr int line() const
Definition adolcerror.h:48
std::string_view func_
Definition adolcerror.h:39
Numeric limits for taped active scalars.
Definition adtb_types.h:1417