forstio/source/forstio/string_literal.h

41 lines
989 B
C
Raw Permalink Normal View History

#pragma once
#include <array>
#include <string_view>
2021-12-29 19:26:22 +01:00
namespace saw {
2020-12-04 22:13:31 +01:00
/**
* 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.
*/
2021-12-18 17:26:14 +01:00
template <class CharT, size_t N> class StringLiteral {
public:
2021-11-29 15:08:18 +01:00
constexpr StringLiteral(const CharT (&input)[N]) noexcept {
2021-12-05 16:17:23 +01:00
for (size_t i = 0; i < N; ++i) {
2021-11-29 15:08:18 +01:00
data[i] = input[i];
}
}
std::array<CharT, N> data{};
constexpr std::string_view view() const noexcept {
return std::string_view{data.data()};
}
2021-12-18 20:54:51 +01:00
constexpr bool
operator==(const StringLiteral<CharT, N> &) const noexcept = default;
2021-12-18 20:54:51 +01:00
template <class CharTR, size_t NR>
constexpr bool
operator==(const StringLiteral<CharTR, NR> &) const noexcept {
2021-12-18 20:54:51 +01:00
return false;
}
};
template <typename T, T... Chars>
2021-12-29 19:49:20 +01:00
constexpr StringLiteral<T, sizeof...(Chars)> operator""_key() {
return StringLiteral<T, sizeof...(Chars) + 1u>{Chars..., '\0'};
2021-11-29 15:08:18 +01:00
}
2021-12-29 19:26:22 +01:00
} // namespace saw