KPN/include/kpn/fixed_string.hpp
2026-05-08 17:48:16 +02:00

47 lines
1.4 KiB
C++

#pragma once
#include <algorithm>
#include <string_view>
#include <cstddef>
namespace kpn {
template<std::size_t N>
struct fixed_string {
char data[N]{};
constexpr fixed_string(const char (&s)[N]) { std::copy_n(s, N, data); }
constexpr bool operator==(const fixed_string&) const = default;
template<std::size_t M>
constexpr bool operator==(const fixed_string<M>&) const { return false; }
constexpr std::string_view view() const { return {data, N - 1}; }
};
template<std::size_t N>
fixed_string(const char (&)[N]) -> fixed_string<N>;
// ── Port name pack lookup ─────────────────────────────────────────────────────
inline constexpr std::size_t npos = std::size_t(-1);
template<fixed_string Name, fixed_string... Names>
constexpr std::size_t index_of() {
std::size_t i = 0;
bool found = false;
auto check = [&](auto n) {
if (!found) {
if (Name == n) found = true;
else ++i;
}
};
(check(Names), ...);
return found ? i : npos;
}
// ── in<> / out<> name tag structs ─────────────────────────────────────────────
template<fixed_string... Names> struct in {};
template<fixed_string... Names> struct out {};
} // namespace kpn