STX  1.0.0
source_location.h
Go to the documentation of this file.
1 
30 #pragma once
31 
32 #include "stx/config.h"
33 
35 
47 // It's equivalent to GCC's implementation
48 struct [[nodiscard]] SourceLocation {
49  static constexpr SourceLocation current(
50 #if STX_HAS_BUILTIN(FILE)
51  const char* file = __builtin_FILE(),
52 #else
53  const char* file = "unknown",
54 #endif
55 
56 #if STX_HAS_BUILTIN(FUNCTION)
57  const char* func = __builtin_FUNCTION(),
58 #else
59  const char* func = "unknown",
60 #endif
61 
62 #if STX_HAS_BUILTIN(LINE)
63  uint_least32_t line = __builtin_LINE(),
64 #else
65  uint_least32_t line = 0,
66 #endif
67 
68 #if STX_HAS_BUILTIN(COLUMN)
69  uint_least32_t column = __builtin_COLUMN()
70 #else
71  uint_least32_t column = 0
72 #endif
73  ) noexcept {
74  SourceLocation loc{};
75  loc.line_ = line;
76  loc.column_ = column;
77  loc.file_ = file;
78  loc.func_ = func;
79  return loc;
80  }
81 
82  // implementation-defined
83  constexpr SourceLocation() noexcept
84  : line_(), column_(), file_("\0"), func_("\0") {}
85  constexpr SourceLocation(SourceLocation const& other) noexcept = default;
86  constexpr SourceLocation(SourceLocation && other) noexcept = default;
87  constexpr SourceLocation& operator=(SourceLocation const& other) noexcept =
88  default;
89  constexpr SourceLocation& operator=(SourceLocation&& other) noexcept =
90  default;
91  ~SourceLocation() noexcept = default;
92 
94  constexpr uint_least32_t column() const noexcept { return column_; }
95 
97  constexpr uint_least32_t line() const noexcept { return line_; }
98 
100  constexpr const char* file_name() const noexcept { return file_; }
101 
103  constexpr const char* function_name() const noexcept { return func_; }
104 
105  private:
106  uint_least32_t line_;
107  uint_least32_t column_;
108  const char* file_;
109  const char* func_;
110 };
111 
constexpr SourceLocation() noexcept
Definition: source_location.h:83
constexpr const char * file_name() const noexcept
return the file name represented by this object
Definition: source_location.h:100
constexpr uint_least32_t column() const noexcept
return the column number represented by this object
Definition: source_location.h:94
static constexpr SourceLocation current(const char *file="unknown", const char *func="unknown", uint_least32_t line=0, uint_least32_t column=0) noexcept
Definition: source_location.h:49
constexpr uint_least32_t line() const noexcept
return the line number represented by this object
Definition: source_location.h:97
#define STX_END_NAMESPACE
Definition: config.h:329
#define STX_HAS_BUILTIN(feature)
Definition: config.h:214
Definition: source_location.h:48
#define STX_BEGIN_NAMESPACE
Definition: config.h:325
constexpr const char * function_name() const noexcept
return the name of the function represented by this object, if any
Definition: source_location.h:103