diff options
author | Claudius Holeksa <mail@keldu.de> | 2023-04-18 18:34:29 +0200 |
---|---|---|
committer | Claudius Holeksa <mail@keldu.de> | 2023-04-18 18:34:29 +0200 |
commit | c4033b8c2028efeb9bac236e6b279bdcd8efec34 (patch) | |
tree | fa5a336d0870240604e9037c37e63414764be7e1 /forstio/core/string_literal.h |
First commit to restructure the forstio repo
Diffstat (limited to 'forstio/core/string_literal.h')
-rw-r--r-- | forstio/core/string_literal.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/forstio/core/string_literal.h b/forstio/core/string_literal.h new file mode 100644 index 0000000..d530a54 --- /dev/null +++ b/forstio/core/string_literal.h @@ -0,0 +1,40 @@ +#pragma once + +#include <array> +#include <string_view> + +namespace saw { +/** + * Helper object which creates a templated string from the provided string + * literal. It guarantees compile time uniqueness and thus allows using strings + * in template parameters. + */ +template <class CharT, size_t N> class string_literal { +public: + constexpr string_literal(const CharT (&input)[N]) noexcept { + for (size_t i = 0; i < N; ++i) { + data[i] = input[i]; + } + } + + std::array<CharT, N> data{}; + + constexpr std::string_view view() const noexcept { + return std::string_view{data.data()}; + } + + constexpr bool + operator==(const string_literal<CharT, N> &) const noexcept = default; + + template <class CharTR, size_t NR> + constexpr bool + operator==(const string_literal<CharTR, NR> &) const noexcept { + return false; + } +}; + +template <typename T, T... Chars> +constexpr string_literal<T, sizeof...(Chars)> operator""_key() { + return string_literal<T, sizeof...(Chars) + 1u>{Chars..., '\0'}; +} +} // namespace saw |