diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-11-26 22:04:04 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-11-26 22:04:04 +0100 |
commit | 5c34e8ae67998c53c1d4016d5b9f4c02917f0ecf (patch) | |
tree | 521f55ead53f6618721afc67986665b0bc53fb42 /c++/core | |
parent | 348d6734b34d69c3ebd9982320281a7329ad8730 (diff) |
core,codec,tools: Working on c binding generation
Diffstat (limited to 'c++/core')
-rw-r--r-- | c++/core/string_literal.h | 21 | ||||
-rw-r--r-- | c++/core/templates.h | 39 |
2 files changed, 58 insertions, 2 deletions
diff --git a/c++/core/string_literal.h b/c++/core/string_literal.h index d530a54..7373d5c 100644 --- a/c++/core/string_literal.h +++ b/c++/core/string_literal.h @@ -11,6 +11,8 @@ namespace saw { */ template <class CharT, size_t N> class string_literal { public: + static_assert(N > 0, "string_literal needs a null terminator"); + constexpr string_literal(const CharT (&input)[N]) noexcept { for (size_t i = 0; i < N; ++i) { data[i] = input[i]; @@ -31,10 +33,25 @@ public: operator==(const string_literal<CharTR, NR> &) const noexcept { return false; } + + template<size_t NR> + constexpr string_literal<CharT, N+NR-1> operator+(const string_literal<CharT, NR>& rhs) const noexcept { + CharT sum[N+NR-1]; + + // The weird +1 happen due to needing to skip the '\0' terminator + for(size_t i = 0; (i+1) < N; ++i){ + sum[i] = data[i]; + } + for(size_t i = N; (i+1) < (N+NR); ++i){ + sum[i+1] = rhs.data[i+1]; + } + + return string_literal<CharT, N+NR-1>{sum}; + } }; template <typename T, T... Chars> -constexpr string_literal<T, sizeof...(Chars)> operator""_key() { - return string_literal<T, sizeof...(Chars) + 1u>{Chars..., '\0'}; +constexpr string_literal<T, sizeof...(Chars)+1u> operator""_sl() { + return string_literal<T, sizeof...(Chars) + 1u>{{Chars..., '\0'}}; } } // namespace saw diff --git a/c++/core/templates.h b/c++/core/templates.h index e704f37..e2851a0 100644 --- a/c++/core/templates.h +++ b/c++/core/templates.h @@ -102,4 +102,43 @@ struct ct_multiply<T, V0, VN...> { static constexpr T value = V0 * ct_multiply<T,VN...>::value; }; +namespace impl { +template<typename T, size_t i> +struct ct_convert_digits_table_helper { + static constexpr std::array<T, 16> table = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F' + }; + + static constexpr T value = table[i]; +}; + +template<uint64_t Num, uint64_t Base, uint64_t... Digs> +struct ct_convert_digits_helper { + static constexpr size_t size = ct_convert_digits_helper<Num / Base, Base, Num % Base, Digs...>::size; + static constexpr std::array<uint64_t, size> value = ct_convert_digits_helper<Num / Base, Base, Num % Base, Digs...>::value; +}; + +template<uint64_t Base, uint64_t... Digs> +struct ct_convert_digits_helper<0, Base, Digs...> { + static constexpr size_t size = sizeof...(Digs); + static constexpr std::array<uint64_t, size> value = {Digs...}; +}; + +template<uint64_t Base> +struct ct_convert_digits_helper<0, Base> { + static constexpr size_t size = 0; + static constexpr std::array<uint64_t, 1> value = {0}; +}; +} + +template<uint64_t Num, uint64_t Base> +struct ct_convert_to_digits { + static_assert(Base <= 16, "Only conversion up to hex is supported"); + + static constexpr size_t size = impl::ct_convert_digits_helper<Num, Base>::size; + static constexpr std::array<uint64_t, size> value = impl::ct_convert_digits_helper<Num, Base>::value; +}; } |