OGLplus  (0.59.0) a C++ wrapper for rendering APIs

mp_string.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MP_STRING_HPP
10 #define EAGINE_MP_STRING_HPP
11 
12 #include "int_constant.hpp"
13 #include "string_span.hpp"
14 #include <utility>
15 
16 namespace eagine {
17 
18 // mp_string
19 template <char... C>
20 struct mp_string {
21  using type = mp_string;
22 
23  using value_type = const char[sizeof...(C) + 1];
24  static constexpr const char value[sizeof...(C) + 1] = {C..., '\0'};
25 
26  constexpr operator string_view() const noexcept {
27  return {value};
28  }
29 };
30 
31 template <char... C>
32 constexpr const char mp_string<C...>::value[sizeof...(C) + 1];
33 
34 // mp_strlen
35 template <typename String>
36 struct mp_strlen;
37 
38 template <char... C>
39 struct mp_strlen<mp_string<C...>> : size_constant<sizeof...(C)> {};
40 
41 // mp_concat
42 template <typename... S>
43 struct mp_concat;
44 
45 template <typename... S>
46 using mp_concat_t = typename mp_concat<S...>::type;
47 
48 template <char... C>
49 struct mp_concat<mp_string<C...>> : mp_string<C...> {};
50 
51 template <char... C1, char... C2, typename... S>
52 struct mp_concat<mp_string<C1...>, mp_string<C2...>, S...>
53  : mp_concat_t<mp_string<C1..., C2...>, S...> {};
54 
55 // mp_translate
56 template <typename S, template <char> class Transf>
57 struct mp_translate;
58 
59 template <typename S, template <char> class Transf>
60 using mp_translate_t = typename mp_translate<S, Transf>::type;
61 
62 template <char... C, template <char> class Transf>
63 struct mp_translate<mp_string<C...>, Transf>
64  : mp_concat_t<typename Transf<C>::type...> {};
65 
66 // mp_uint_to_string
67 template <unsigned I>
68 struct mp_uint_to_string;
69 
70 template <unsigned I>
71 using mp_uint_to_string_t = typename mp_uint_to_string<I>::type;
72 
73 template <>
74 struct mp_uint_to_string<0U> : mp_string<'0'> {};
75 template <>
76 struct mp_uint_to_string<1U> : mp_string<'1'> {};
77 template <>
78 struct mp_uint_to_string<2U> : mp_string<'2'> {};
79 template <>
80 struct mp_uint_to_string<3U> : mp_string<'3'> {};
81 template <>
82 struct mp_uint_to_string<4U> : mp_string<'4'> {};
83 template <>
84 struct mp_uint_to_string<5U> : mp_string<'5'> {};
85 template <>
86 struct mp_uint_to_string<6U> : mp_string<'6'> {};
87 template <>
88 struct mp_uint_to_string<7U> : mp_string<'7'> {};
89 template <>
90 struct mp_uint_to_string<8U> : mp_string<'8'> {};
91 template <>
92 struct mp_uint_to_string<9U> : mp_string<'9'> {};
93 
94 template <unsigned I>
95 struct mp_uint_to_string
96  : mp_concat_t<mp_uint_to_string_t<I / 10>, mp_uint_to_string_t<I % 10>> {};
97 
98 // mp_int_to_string
99 template <int I>
100 struct mp_int_to_string
101  : std::conditional_t<
102  (I < 0),
103  mp_concat<mp_string<'-'>, mp_uint_to_string_t<unsigned(-I)>>,
104  mp_uint_to_string_t<unsigned(I)>> {};
105 
106 template <int I>
107 using mp_int_to_string_t = typename mp_int_to_string<I>::type;
108 
109 // bits
110 namespace bits {
111 
112 // mp_create_string
113 template <typename S, typename IS>
114 struct mp_create_string;
115 
116 template <typename S, std::size_t... I>
117 struct mp_create_string<S, std::index_sequence<I...>>
118  : mp_string<S::mp_str[I]...> {};
119 
120 // mp_do_make_string
121 template <typename S, typename A>
122 struct mp_do_make_string;
123 
124 template <typename S, std::size_t N>
125 struct mp_do_make_string<S, const char[N]>
126  : mp_create_string<S, std::make_index_sequence<N - 1>> {};
127 
128 template <typename X>
129 auto does_have_mp_str_const(X*, decltype(X::mp_str) = X::mp_str)
130  -> std::true_type;
131 
132 auto does_have_mp_str_const(...) -> std::false_type;
133 
134 } // namespace bits
135 
136 // has_mp_str_const
137 template <typename T>
138 struct has_mp_str_const
139  : decltype(bits::does_have_mp_str_const(static_cast<T*>(nullptr))) {};
140 
141 template <typename T>
142 using has_mp_str_const_t = typename has_mp_str_const<T>::type;
143 
144 // mp_make_string
145 template <typename S>
146 struct mp_make_string : bits::mp_do_make_string<S, decltype(S::mp_str)> {};
147 
148 template <typename S>
149 using mp_make_string_t = typename mp_make_string<S>::type;
150 
151 } // namespace eagine
152 
153 #endif // EAGINE_MP_STRING_HPP
value_type
Value tree value element data type enumeration.
Definition: interface.hpp:27
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
Common code is placed in this namespace.
Definition: eagine.hpp:21
std::integral_constant< std::size_t, S > size_constant
Alias for size_t constant type.
Definition: int_constant.hpp:35

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).