types/description: add is_fundamental_match for descriptions

This commit is contained in:
Danny Robson 2020-05-01 13:10:13 +10:00
parent 03e29aede1
commit 750b3b6519

View File

@ -274,6 +274,49 @@ namespace cruft::types {
};
}
}
///////////////////////////////////////////////////////////////////////////
/// Tests if the underlying scalar value specified by two descriptions
/// matches.
///
/// This deliberately ignores arity, alignment, and index.
///
/// It is provided to assist systems that needs to coerce values and
/// assumes the caller is prepared to deal with indexing, alignment, and
/// interpretation.
inline bool
is_fundamental_match (description const &a, description const &b)
{
// Both categories must match, or one must be ENUM and the other must
// be INTEGER.
if (a.category != b.category) {
if (a.category == category::ENUM)
if (b.category != category::INTEGER)
return false;
if (b.category == category::ENUM)
if (a.category != category::INTEGER)
return false;
}
return a.signedness == b.signedness &&
a.width == b.width;
}
///------------------------------------------------------------------------
/// Tests if a type matches the underlying scalar value specified by a
/// description.
///
/// This exists as a convenience wrapper for `is_fundemental_wrapper`
/// with two descriptions.
template <typename ValueT>
bool
is_fundamental_match (description const &desc)
{
return is_fundamental_match (desc, make_description<ValueT> ());
}
}