draconisplusplus/include/rfl/internal/StringLiteral.hpp

53 lines
1.5 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_INTERNAL_STRINGLITERAL_HPP_
#define RFL_INTERNAL_STRINGLITERAL_HPP_
#include <algorithm>
#include <array>
#include <string>
#include <string_view>
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace internal {
/// Normal strings cannot be used as template
/// parameters, but this can. This is needed
/// for the parameters names in the NamedTuples.
template <size_t N>
struct StringLiteral {
2024-06-16 00:13:15 -04:00
constexpr StringLiteral(const auto... _chars) : arr_ { _chars..., '\0' } {}
2024-06-08 14:10:59 -04:00
constexpr StringLiteral(const std::array<char, N> _arr) : arr_(_arr) {}
2024-06-16 00:13:15 -04:00
constexpr StringLiteral(const char (&_str)[N]) { std::copy_n(_str, N, std::data(arr_)); }
2024-06-08 14:10:59 -04:00
/// Returns the value as a string.
std::string str() const { return std::string(std::data(arr_), N - 1); }
/// Returns the value as a string.
constexpr std::string_view string_view() const {
return std::string_view(std::data(arr_), N - 1);
}
std::array<char, N> arr_ {};
};
template <size_t N1, size_t N2>
2024-06-16 00:13:15 -04:00
constexpr inline bool
operator==(const StringLiteral<N1>& _first, const StringLiteral<N2>& _second) {
2024-06-09 18:55:00 -04:00
if constexpr (N1 != N2) {
return false;
}
2024-06-08 14:10:59 -04:00
return _first.string_view() == _second.string_view();
}
template <size_t N1, size_t N2>
2024-06-16 00:13:15 -04:00
constexpr inline bool
operator!=(const StringLiteral<N1>& _first, const StringLiteral<N2>& _second) {
2024-06-08 14:10:59 -04:00
return !(_first == _second);
}
} // namespace internal
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif