My Project
source_location.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_SOURCE_LOCATION_HPP
29 #define BSL_SOURCE_LOCATION_HPP
30 
31 #include "color.hpp"
32 #include "cstdint.hpp"
33 #include "cstr_type.hpp"
34 #include "debug.hpp"
35 
36 namespace bsl
37 {
47  class source_location final
48  {
50  using file_type = bsl::cstr_type;
52  using func_type = bsl::cstr_type;
54  using line_type = bsl::intmax;
55 
64  constexpr source_location( // --
65  file_type const current_file, // --
66  func_type const current_func, // --
67  line_type const current_line) noexcept // --
68  : m_file{current_file} // --
69  , m_func{current_func} // --
70  , m_line{current_line}
71  {}
72 
73  public:
80  constexpr source_location() noexcept // --
81  : m_file{"unknown"} // --
82  , m_func{"unknown"} // --
83  , m_line{-1}
84  {}
85 
111  static constexpr source_location
113  file_type const current_file = BSL_BUILTIN_FILE,
114  func_type const current_func = BSL_BUILTIN_FUNCTION,
115  line_type const current_line = BSL_BUILTIN_LINE) noexcept
116  {
117  return {current_file, current_func, current_line};
118  }
119 
128  [[nodiscard]] constexpr file_type
129  file_name() const noexcept
130  {
131  return m_file;
132  }
133 
142  [[nodiscard]] constexpr func_type
143  function_name() const noexcept
144  {
145  return m_func;
146  }
147 
156  [[nodiscard]] constexpr line_type
157  line() const noexcept
158  {
159  return m_line;
160  }
161 
162  private:
164  file_type m_file;
166  func_type m_func;
168  line_type m_line;
169  };
170 
184  constexpr source_location
186  {
187  return sloc;
188  }
189 
201  template<typename T>
202  [[maybe_unused]] constexpr out<T>
203  operator<<(out<T> const o, source_location const &sloc) noexcept
204  {
205  if constexpr (o.empty()) {
206  return o;
207  }
208 
209  o << " --> " // --
210  << bsl::yellow << sloc.file_name() << bsl::reset_color // --
211  << ": " // --
212  << bsl::cyan << sloc.line() << bsl::reset_color // --
213  << bsl::endl; // --
214 
215  return o;
216  }
217 
220 } // namespace bsl
221 
222 #endif
constexpr cstr_type reset_color
resets the color output of debug statements
Definition: color.hpp:36
constexpr line_type line() const noexcept
Returns the line location associated with the bsl::source_location.
Definition: source_location.hpp:157
constexpr bsl::char_type endl
newline constant
Definition: debug.hpp:53
constexpr file_type file_name() const noexcept
Returns the file name associated with the bsl::source_location.
Definition: source_location.hpp:129
constexpr func_type function_name() const noexcept
Returns the function name associated with the bsl::source_location.
Definition: source_location.hpp:143
This class implements the source_location specification that will eventually be included in C++20....
Definition: source_location.hpp:47
::intmax_t intmax
defines a signed integer with the maximum possible size
Definition: cstdint.hpp:95
static constexpr source_location current(file_type const current_file=BSL_BUILTIN_FILE, func_type const current_func=BSL_BUILTIN_FUNCTION, line_type const current_line=BSL_BUILTIN_LINE) noexcept
Constructs a new source_location object corresponding to the location of the call site.
Definition: source_location.hpp:112
constexpr source_location here(source_location const &sloc=source_location::current()) noexcept
This provides a less verbose version of bsl::source_location::current() to help reduce how large this...
Definition: source_location.hpp:185
char const * cstr_type
C-style string type.
Definition: cstr_type.hpp:39
constexpr cstr_type yellow
changes the foreground color to normal yellow
Definition: color.hpp:45
constexpr source_location() noexcept
Creates a default constructed source location. By default, a source location's file name is "unknown"...
Definition: source_location.hpp:80
constexpr cstr_type cyan
changes the foreground color to normal cyan
Definition: color.hpp:51