Inline DERIVE_EQ and DERIVE_HASH.

This commit is contained in:
Alexander McCord 2024-05-26 22:18:25 -07:00
parent 2fdc5c8f98
commit f808ed2438

View file

@ -56,32 +56,28 @@ inline size_t hashCombine(size_t& seed, size_t hash)
using Base::Node; \ using Base::Node; \
} }
#define DERIVE_EQ(name, field) \
bool operator==(const name& rhs) const \
{ \
return field == rhs.field; \
} \
bool operator!=(const name& rhs) const \
{ \
return !(*this == rhs); \
}
#define DERIVE_HASH(name, field) \
struct Hash \
{ \
size_t operator()(const name& value) const \
{ \
return languageHash(value.field); \
} \
}
template<typename Phantom, typename T> template<typename Phantom, typename T>
struct Atom struct Atom
{ {
T value; T value;
DERIVE_EQ(Atom, value); bool operator==(const Atom& rhs) const
DERIVE_HASH(Atom, value); {
return value == rhs.value;
}
bool operator!=(const Atom& rhs) const
{
return !(*this == rhs);
}
struct Hash
{
size_t operator()(const Atom& value) const
{
return languageHash(value.value);
}
};
}; };
/// Empty base class just for static_asserts. /// Empty base class just for static_asserts.
@ -132,12 +128,24 @@ public:
return array[getIndex<T>()]; return array[getIndex<T>()];
} }
DERIVE_EQ(Node, array); bool operator==(const Node& rhs) const
DERIVE_HASH(Node, array); {
}; return array == rhs.array;
}
#undef DERIVE_EQ bool operator!=(const Node& rhs) const
#undef DERIVE_HASH {
return !(*this == rhs);
}
struct Hash
{
size_t operator()(const Node& value) const
{
return languageHash(value.array);
}
};
};
// `Language` is very similar to `Luau::Variant` with enough differences warranting a different type altogether. // `Language` is very similar to `Luau::Variant` with enough differences warranting a different type altogether.
// //