summaryrefslogtreecommitdiff
path: root/forstio/core/string_literal.h
blob: d530a54b18e957c6a093a99aab47a52ff0fcb4d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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