From 5c34e8ae67998c53c1d4016d5b9f4c02917f0ecf Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 26 Nov 2023 22:04:04 +0100 Subject: core,codec,tools: Working on c binding generation --- c++/core/string_literal.h | 21 +++++++++++++++++++-- c++/core/templates.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) (limited to 'c++/core') 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 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 &) const noexcept { return false; } + + template + constexpr string_literal operator+(const string_literal& 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{sum}; + } }; template -constexpr string_literal operator""_key() { - return string_literal{Chars..., '\0'}; +constexpr string_literal operator""_sl() { + return string_literal{{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 { static constexpr T value = V0 * ct_multiply::value; }; +namespace impl { +template +struct ct_convert_digits_table_helper { + static constexpr std::array table = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F' + }; + + static constexpr T value = table[i]; +}; + +template +struct ct_convert_digits_helper { + static constexpr size_t size = ct_convert_digits_helper::size; + static constexpr std::array value = ct_convert_digits_helper::value; +}; + +template +struct ct_convert_digits_helper<0, Base, Digs...> { + static constexpr size_t size = sizeof...(Digs); + static constexpr std::array value = {Digs...}; +}; + +template +struct ct_convert_digits_helper<0, Base> { + static constexpr size_t size = 0; + static constexpr std::array value = {0}; +}; +} + +template +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::size; + static constexpr std::array value = impl::ct_convert_digits_helper::value; +}; } -- cgit v1.2.3