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
9namespace 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, __data);
17 }
18
19 consteval char operator[](size_t i) const noexcept
20 {
21 return __data[i];
22 }
23
24 consteval const char *at(size_t i) const noexcept
25 {
26 return __data + i;
27 }
28
29 const size_t strlen = n;
30 char __data[n];
31 };
32
33 struct _BaseNamedType
34 {
35 };
36
37 template<mos::string_literal name>
38 struct NamedType : public _BaseNamedType
39 {
40 static constexpr mos::string_view type_name = name.__data;
41 };
42
43 template<typename T>
44 constexpr auto HasTypeName = std::is_base_of_v<_BaseNamedType, T>;
45
46 template<typename V, typename TSpecialisation = V>
47 struct InitOnce
48 {
49 V *operator->()
50 {
51 return &value;
52 }
53
54 V &operator*()
55 {
56 return value;
57 }
58
59 private:
60 static inline V value;
61 };
62
63#define PrivateTag \
64 private: \
65 struct Private \
66 { \
67 };
68
69 template<typename T>
70 consteval static string_view getTypeName()
71 {
72#if defined(MOS_COMPILER_CLANG) || defined(MOS_COMPILER_GCC)
73 constexpr string_view f = __PRETTY_FUNCTION__;
74 constexpr auto g = f.substr(start: f.find(str: "with T = ") + 9);
75 constexpr auto k = g.substr(start: 0, end: g.find(str: ";"));
76 return k;
77#else
78#error "unknown compiler"
79#endif
80 }
81} // namespace mos
82