1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | |
3 | #pragma once |
4 | |
5 | #include <algorithm> |
6 | #include <mos/mos_global.h> |
7 | #include <mos/string_view.hpp> |
8 | |
9 | namespace mos |
10 | { |
11 | template<auto n> |
12 | struct string_literal |
13 | { |
14 | constexpr string_literal(const char (&str)[n]) |
15 | { |
16 | std::copy_n(str, n, value); |
17 | } |
18 | |
19 | char value[n]; |
20 | }; |
21 | |
22 | template<mos::string_literal name> |
23 | struct NamedType |
24 | { |
25 | static constexpr mos::string_view type_name = name.value; |
26 | }; |
27 | |
28 | template<typename T> |
29 | concept HasTypeName = std::is_same_v<std::remove_const_t<decltype(T::type_name)>, mos::string_view>; |
30 | |
31 | template<typename V, typename TSpecialisation = V> |
32 | struct InitOnce |
33 | { |
34 | V *operator->() |
35 | { |
36 | return &value; |
37 | } |
38 | |
39 | V &operator*() |
40 | { |
41 | return value; |
42 | } |
43 | |
44 | private: |
45 | static inline V value; |
46 | }; |
47 | |
48 | #define PrivateTag \ |
49 | private: \ |
50 | struct Private \ |
51 | { \ |
52 | }; |
53 | |
54 | template<typename T> |
55 | consteval static string_view getTypeName() |
56 | { |
57 | #if defined(MOS_COMPILER_CLANG) || defined(MOS_COMPILER_GCC) |
58 | constexpr string_view f = __PRETTY_FUNCTION__; |
59 | constexpr auto g = f.substring(start: f.find(str: "with T = " ) + 9); |
60 | constexpr auto k = g.substring(start: 0, end: g.find(str: ";" )); |
61 | return k; |
62 | #else |
63 | #error "unknown compiler" |
64 | #endif |
65 | } |
66 | } // namespace mos |
67 | |