-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathException.h
98 lines (77 loc) · 2.2 KB
/
Exception.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Include cstdint for GCC 13+ or Clang 13+
#if (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ >= 13)) || (defined(__clang__) && (__clang_major__ >= 13))
#include <cstdint>
#endif
#pragma once
#include <exception>
#include <sstream>
#include <string>
#include <vector>
// Simplified version of the c10 Exception infrastructure
// https://github.com/pytorch/pytorch/blob/master/c10/util/Exception.h
// Hopefully makes it easer to merge codebases later and still gives better
// errors
namespace torch_tensorrt {
class Error : public std::exception {
std::vector<std::string> msg_stack_;
std::string msg_;
const void* caller_;
public:
Error(const std::string& msg, const void* caller = nullptr);
Error(const char* file, const uint32_t line, const std::string& msg, const void* caller = nullptr);
void AppendMessage(const std::string& msg);
std::string msg() const;
const std::vector<std::string>& msg_stack() const {
return msg_stack_;
}
const char* what() const noexcept override {
return msg_.c_str();
}
const void* caller() const noexcept {
return caller_;
}
};
std::string GetExceptionString(const std::exception& e);
namespace detail {
inline std::string if_empty_then(std::string x, std::string y) {
if (x.empty()) {
return y;
} else {
return x;
}
}
template <typename T>
struct CanonicalizeStrTypes {
using type = const T&;
};
inline std::ostream& _str(std::ostream& ss) {
return ss;
}
template <typename T>
inline std::ostream& _str(std::ostream& ss, const T& t) {
ss << t;
return ss;
}
template <typename T, typename... Args>
inline std::ostream& _str(std::ostream& ss, const T& t, const Args&... args) {
return _str(_str(ss, t), args...);
}
template <typename... Args>
inline std::string _str_wrapper(const Args&... args) {
std::ostringstream ss;
_str(ss, args...);
return ss.str();
}
} // namespace detail
template <typename... Args>
inline std::string str(const Args&... args) {
return detail::_str_wrapper<typename detail::CanonicalizeStrTypes<Args>::type...>(args...);
}
template <>
inline std::string str(const std::string& str) {
return str;
}
inline std::string str(const char* c_str) {
return c_str;
}
} // namespace torch_tensorrt