Enrollment 28.2.0
Loading...
Searching...
No Matches
SourceLocation.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <array>
14#include <string>
15#include <string_view>
16
17namespace inno
18{
24 */
25 class SourceLocation
26 {
27 public:
34 */
35 [[nodiscard]] std::string FileName() const
36 {
37 return f;
38 }
39
46 */
47 [[nodiscard]] std::string FunctionName() const
48 {
49 return fun;
50 }
51
58 */
59 [[nodiscard]] constexpr int Line() const
60 {
61 return l;
62 }
63
76 */
77 explicit SourceLocation(const char* file = __builtin_FILE(),
78 const char* function = __builtin_FUNCTION(),
79 int line = __builtin_LINE())
80 : f(Strip(file))
81 , fun(function)
82 , l(line)
83 {
84 }
85
90 */
91 [[nodiscard]] std::string ToString() const
92 {
93 return f + ":" + std::to_string(l) + " '" + fun + "'";
94 }
95
96 private:
97 std::string f;
98 std::string fun;
99 int l;
100 // Strip file name upto prefix
101 static std::string Strip(const char* file) noexcept
102 {
103 auto path = std::string_view(file);
104
105 // list of known prefixes to strip
106 constexpr std::array<std::string_view, 6> Prefixes = { "/include/", "\\include\\", "/src/",
107 "\\src\\", "/binding/", "\\binding\\" };
108
109 auto pos = std::string_view::npos;
110
111 // find the last occurrence of any of the prefixes
112 for (const auto& prefix : Prefixes)
113 {
114 pos = path.rfind(prefix);
115 if (pos != std::string_view::npos)
116 {
117 // skip leading slash
118 pos += 1;
119 break;
120 }
121 }
122
123 if (pos != std::string_view::npos)
124 {
125 return std::string(path.substr(pos));
126 }
127
128 return std::string(path); // return full path if nothing matched
129 }
130 };
131} // namespace inno
constexpr int Line() const
Get the line number where the exception occurred.
Definition SourceLocation.h:58
std::string ToString() const
Creates string representation of SourceLocation.
Definition SourceLocation.h:90
std::string FunctionName() const
Get the name of the function where the exception occurred.
Definition SourceLocation.h:46
std::string FileName() const
Get the name of the file where the exception occurred.
Definition SourceLocation.h:34
SourceLocation(const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Constructor to initialize a source location.
Definition SourceLocation.h:76