Remove optional semantics from identifier

This commit was merged in pull request #3.
This commit is contained in:
2024-07-04 10:53:37 +02:00
parent b307f44333
commit f5c6b75621
+13 -16
View File
@@ -7,46 +7,43 @@
namespace tl namespace tl
{ {
template<typename T, T invalid_value, typename Tag> template<typename T, typename Tag>
struct identifier struct identifier
{ {
using underlying_type = T; using underlying_type = T;
constexpr identifier() noexcept = default; identifier() = delete;
explicit constexpr identifier(T value) noexcept : m_value(std::move(value)) {} explicit constexpr identifier(T value) noexcept : m_value(std::move(value)) {}
constexpr T value() const noexcept { return m_value; } constexpr T value() const noexcept { return m_value; }
constexpr bool is_valid() const noexcept { return m_value != invalid_value; } auto operator<=>(const identifier<T, Tag>& other) const noexcept { return m_value <=> other.value(); }
//inline static identifier<T, invalid_value, Tag> invalid() noexcept { return identifier<T, invalid_value, Tag>(); }
auto operator<=>(const identifier<T, invalid_value, Tag>& other) const noexcept { return m_value <=> other.value(); }
auto operator<=>(const T& other) const noexcept { return m_value <=> other; } auto operator<=>(const T& other) const noexcept { return m_value <=> other; }
bool operator==(const identifier<T, invalid_value, Tag>& other) const noexcept { return m_value == other.value(); } bool operator==(const identifier<T, Tag>& other) const noexcept { return m_value == other.value(); }
bool operator==(const T& other) const noexcept { return m_value == other; } bool operator==(const T& other) const noexcept { return m_value == other; }
private: private:
T m_value = invalid_value; T m_value;
}; };
} }
#define TL_DECLARE_INTEGRAL_ID(NAME, TYPE, INVALID_VALUE) \ #define TL_DECLARE_INTEGRAL_ID(NAME, TYPE) \
struct NAME##_tag {};\ struct NAME##_tag {};\
using NAME = tl::identifier<TYPE, INVALID_VALUE, NAME##_tag>; using NAME = tl::identifier<TYPE, NAME##_tag>;
template <typename T, T invalid_value, typename Tag> template <typename T, typename Tag>
struct std::formatter<tl::identifier<T, invalid_value, Tag>> struct std::formatter<tl::identifier<T, Tag>>
{ {
constexpr auto parse(format_parse_context& ctx) noexcept { return ctx.begin(); } constexpr auto parse(format_parse_context& ctx) noexcept { return ctx.begin(); }
auto format(const tl::identifier<T, invalid_value, Tag>& id, std::format_context& ctx) const auto format(const tl::identifier<T, Tag>& id, std::format_context& ctx) const
{ {
return format_to(ctx.out(), "{}", id.value()); return format_to(ctx.out(), "{}", id.value());
} }
}; };
template<typename T, T invalid_value, typename Tag> template<typename T, typename Tag>
struct eastl::hash<tl::identifier<T, invalid_value, Tag>> struct eastl::hash<tl::identifier<T, Tag>>
{ {
size_t operator()(tl::identifier<T, invalid_value, Tag> const& id) const size_t operator()(tl::identifier<T, Tag> const& id) const
{ {
return hash<T>()(id.value()); return hash<T>()(id.value());
} }