Debian Codegraph @

common/main/d_srcloc.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 #pragma once
9
10 #include <span>
11 #include <type_traits>
12 #include <utility>
13 #include "dxxsconf.h"
14
15 template <bool capture_source_location = false>
16 class location_wrapper
17 {
18 public:
19 /* This must be a template to be compatible with the `scratch_buffer`
20 * template-typedef in `location_wrapper<true>`.
21 */
22 template <std::size_t N>
23 using scratch_buffer = std::false_type;
24 template <std::size_t N>
25 static std::pair<std::size_t, std::span<char, N>> insert_location_leader(std::array<char, N> &buffer)
26 {
27 return {{}, buffer};
28 }
29 /* Define overloads to preserve const qualification */
30 static std::span<char> prepare_buffer(scratch_buffer<0> &, const std::span<char> text)
31 {
32 return text;
33 }
34 static std::span<const char> prepare_buffer(scratch_buffer<0> &, const std::span<const char> text)
35 {
36 return text;
37 }
38 };
39
40 #if DXX_HAVE_CXX_BUILTIN_FILE_LINE
41 #include <cstdio>
42
43 template <>
44 class location_wrapper<true>
45 {
46 const char *file{__builtin_FILE()};
47 unsigned line{__builtin_LINE()};
48 public:
49 template <std::size_t N>
50 using scratch_buffer = std::array<char, N>;
51 /* Return a span describing the unwritten area into which the caller can
52 * place further text.
53 */
54 template <std::size_t N>
55 std::pair<std::size_t, std::span<char>> insert_location_leader(std::array<char, N> &buffer)
56 {
57 const auto written = std::snprintf(buffer.data(), buffer.size(), "%s:%u: ", file, line);
58 return {written, std::span(buffer).subspan(written)};
59 }
60 /* Return a span describing the written area that the caller can read
61 * without accessing undefined bytes.
62 */
63 template <std::size_t N>
64 std::span<const char> prepare_buffer(std::array<char, N> &buffer, const std::span<const char> text) const
65 {
66 const auto written = std::snprintf(buffer, sizeof(buffer), "%s:%u: %.*s", file, line, static_cast<int>(text.size()), text.data());
67 return {buffer, written};
68 }
69 /* Delegate to the const-qualified version, but return a `char *` to
70 * match the non-const input `char *`, to preserve the choice of
71 * overload for the function to receive this result.
72 */
73 template <std::size_t N>
74 std::span<char> prepare_buffer(std::array<char, N> &buffer, const std::span<char> text) const
75 {
76 return {buffer, prepare_buffer(buffer, std::span<const char>(text)).size()};
77 }
78 };
79 #endif
80
81 template <typename T, bool capture_source_location = false>
82 class location_value_wrapper : public location_wrapper<capture_source_location>
83 {
84 T value;
85 public:
86 constexpr location_value_wrapper(const T &v) :
87 value{v}
88 {
89 }
90 operator T() const
91 {
92 return value;
93 }
94 };