mimic++ v9.2.1
Loading...
Searching...
No Matches
SourceLocation.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2025.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef MIMICPP_UTILITIES_SOURCE_LOCATION_HPP
7#define MIMICPP_UTILITIES_SOURCE_LOCATION_HPP
8
9#pragma once
10
16
17#ifndef MIMICPP_DETAIL_IS_MODULE
18 #include <cstddef>
19 #include <string_view>
20 #include <utility>
21
22 #ifdef __cpp_lib_source_location
23 #include <source_location>
24 #define MIMICPP_DETAIL_HAS_SOURCE_LOCATION 1
25 #endif
26#endif
27
29{
38 {
39 public:
40 ~SourceLocation() = default;
41
42#ifdef MIMICPP_DETAIL_HAS_SOURCE_LOCATION
48 [[nodiscard]] //
49 explicit(false) constexpr SourceLocation(
50 [[maybe_unused]] auto&&... canary,
51 std::source_location const& loc = std::source_location::current()) noexcept
52 : m_FileName{loc.file_name()},
53 m_FunctionName{loc.function_name()},
54 m_Line{loc.line()}
55 {
56 static_assert(0 == sizeof...(canary), "Do not supply custom arguments to util::SourceLocation.");
57 }
58#else
66 [[nodiscard]] //
67 explicit(false) constexpr SourceLocation(
68 [[maybe_unused]] auto&&... canary,
69 std::string_view const fileName = __builtin_FILE(),
70 std::string_view const functionName = __builtin_FUNCTION(),
71 std::size_t const line = __builtin_LINE()) noexcept
72 : m_FileName{fileName},
73 m_FunctionName{functionName},
74 m_Line{line}
75 {
76 static_assert(0 == sizeof...(canary), "Do not supply custom arguments to util::SourceLocation.");
77 }
78#endif
79
80 SourceLocation(SourceLocation const&) = default;
84
85 [[nodiscard]]
86 constexpr std::string_view file_name() const noexcept
87 {
88 return m_FileName;
89 }
90
91 [[nodiscard]]
92 constexpr std::string_view function_name() const noexcept
93 {
94 return m_FunctionName;
95 }
96
97 [[nodiscard]]
98 constexpr std::size_t line() const noexcept
99 {
100 return m_Line;
101 }
102
103 [[nodiscard]]
104 friend constexpr bool operator==(SourceLocation const& lhs, SourceLocation const& rhs) noexcept
105 {
106 return lhs.line() == rhs.line()
107 && lhs.file_name() == rhs.file_name()
108 && lhs.function_name() == rhs.function_name();
109 }
110
111 private:
112 std::string_view m_FileName;
113 std::string_view m_FunctionName;
114 std::size_t m_Line;
115 };
116}
117
118template <>
119struct mimicpp::printing::detail::state::common_type_printer<mimicpp::util::SourceLocation>
120{
121 template <print_iterator OutIter>
122 static constexpr OutIter print(OutIter out, util::SourceLocation const& loc)
123 {
124 return detail::print_source_location(
125 std::move(out),
126 loc.file_name(),
127 loc.line(),
128 loc.function_name());
129 }
130};
131
132#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
std::string_view const std::string_view const functionName
Definition SourceLocation.hpp:70
constexpr std::string_view function_name() const noexcept
Definition SourceLocation.hpp:92
SourceLocation & operator=(SourceLocation &&)=default
std::string_view const fileName
Definition SourceLocation.hpp:69
SourceLocation & operator=(SourceLocation const &)=default
SourceLocation(SourceLocation &&)=default
std::string_view const std::string_view const std::size_t const line
Definition SourceLocation.hpp:71
friend constexpr bool operator==(SourceLocation const &lhs, SourceLocation const &rhs) noexcept
Definition SourceLocation.hpp:104
constexpr std::string_view file_name() const noexcept
Definition SourceLocation.hpp:86
constexpr std::size_t line() const noexcept
Definition SourceLocation.hpp:98
constexpr printing::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Print.hpp:183
Definition Fwd.hpp:445
Definition Call.hpp:24