mitteLib
Loading...
Searching...
No Matches
type_name.hpp
Go to the documentation of this file.
1//
2// Created by spak on 7/14/23.
3//
4
5#ifndef MLAB_TYPE_NAME_HPP
6#define MLAB_TYPE_NAME_HPP
7
8#include <algorithm>
9#include <string>
10#include <string_view>
11
12namespace mlab {
13
15 } constexpr null_terminated{};
16
17 template <std::size_t N>
19 char data[N];
20
21 [[nodiscard]] std::size_t constexpr size() const;
22
23 constexpr explicit fixed_size_string(const char s[N]);
24 constexpr explicit fixed_size_string(null_terminated_tag, const char *s);
25
26 template <std::size_t M>
27 [[nodiscard]] constexpr std::size_t find(fixed_size_string<M> subs) const;
28
29 [[nodiscard]] constexpr std::size_t find_last_of(std::initializer_list<char> cs) const;
30
31 template <std::size_t Start, std::size_t End>
32 [[nodiscard]] constexpr auto substr() const;
33
34 [[nodiscard]] operator std::string() const;
35 };
36
37 template <class T, std::size_t BufSize = 256>
38 [[nodiscard]] constexpr auto type_name();
39}// namespace mlab
40
41namespace mlab {
42 template <std::size_t N>
43 std::size_t constexpr fixed_size_string<N>::size() const {
44 return N;
45 }
46
47 template <std::size_t N>
48 constexpr fixed_size_string<N>::fixed_size_string(const char s[N]) : data{} {
49 std::copy_n(s, size(), std::begin(data));
50 }
51
52 template <std::size_t N>
54 std::size_t i = 0;
55 for (; i < N - 1; ++i) {
56 if (s[i] == '\0') {
57 break;
58 }
59 data[i] = s[i];
60 }
61 for (; i < N; ++i) {
62 data[i] = '\0';
63 }
64 }
65
66 template <std::size_t N>
67 template <std::size_t M>
68 constexpr std::size_t fixed_size_string<N>::find(fixed_size_string<M> subs) const {
69 return std::distance(std::begin(data), std::search(std::begin(data), std::end(data), std::begin(subs.data), std::end(subs.data)));
70 }
71
72 template <std::size_t N>
73 constexpr std::size_t fixed_size_string<N>::find_last_of(std::initializer_list<char> cs) const {
74 using iterator_t = decltype(std::begin(data));
75 auto rbegin = std::reverse_iterator<iterator_t>(std::end(data));
76 auto rend = std::reverse_iterator<iterator_t>(std::begin(data));
77 auto it = std::find_first_of(rbegin, rend, std::begin(cs), std::end(cs));
78 auto pos = std::distance(it, rend);
79 return pos >= 1 ? pos - 1 : N;
80 }
81
82 template <std::size_t N>
83 template <std::size_t Start, std::size_t End>
84 constexpr auto fixed_size_string<N>::substr() const {
85 static_assert(Start < End and End <= N);
86 auto retval = fixed_size_string<End - Start + 1>{&data[Start]};
87 retval.data[End - Start] = '\0';
88 return retval;
89 }
90
91 template <std::size_t N>
92 fixed_size_string<N>::operator std::string() const {
93 return std::string{data, std::find(std::begin(data), std::end(data), '\0')};
94 }
95
96 template <class T, std::size_t BufSize>
97 [[nodiscard]] constexpr auto type_name() {
98 constexpr auto method_name = fixed_size_string<BufSize>{null_terminated, __PRETTY_FUNCTION__};
99 constexpr std::size_t t_pos = method_name.find(fixed_size_string<4>{"T = "});
100 constexpr std::size_t bufsize_pos = method_name.find(fixed_size_string<10>{"BufSize = "});
101 if constexpr (t_pos >= BufSize or bufsize_pos >= BufSize or t_pos + 4 >= bufsize_pos) {
102 return fixed_size_string<1>{""};
103 } else {
104 constexpr auto candidate = method_name.template substr<t_pos + 4, bufsize_pos>();
105 constexpr std::size_t t_end = candidate.find_last_of({',', ';'});
106 if constexpr (t_end >= candidate.size()) {
107 return fixed_size_string<1>{""};
108 } else {
109 return candidate.template substr<0, t_end>();
110 }
111 }
112 }
113}// namespace mlab
114#endif//MLAB_TYPE_NAME_HPP
Definition log.cpp:8
struct mlab::null_terminated_tag null_terminated
constexpr auto type_name()
Definition type_name.hpp:97
Definition type_name.hpp:18
constexpr std::size_t find(fixed_size_string< M > subs) const
Definition type_name.hpp:68
constexpr std::size_t find_last_of(std::initializer_list< char > cs) const
Definition type_name.hpp:73
std::size_t constexpr size() const
Definition type_name.hpp:43
char data[N]
Definition type_name.hpp:19
constexpr auto substr() const
Definition type_name.hpp:84
constexpr fixed_size_string(const char s[N])
Definition type_name.hpp:48
Definition type_name.hpp:14