types/description: add a validation specialisation

This commit is contained in:
Danny Robson 2019-05-28 10:47:29 +10:00
parent 5ba25330d0
commit a21f09493d

View File

@ -37,3 +37,54 @@ cruft::types::operator<< (std::ostream &os, cruft::types::description val)
<< ", alignment: " << val.alignment << ", alignment: " << val.alignment
<< " }"; << " }";
} }
///////////////////////////////////////////////////////////////////////////////
#include "../debug/validate.hpp"
#include <iostream>
//-----------------------------------------------------------------------------
template <>
struct cruft::debug::validator<cruft::types::description> {
static bool is_valid (cruft::types::description const&);
};
//-----------------------------------------------------------------------------
bool
cruft::debug::validator<cruft::types::description>::is_valid (
cruft::types::description const &val
) {
switch (val.category) {
// Ensure types that look fundamental might have a chance of being
// represented in hardware.
case cruft::types::category::UNSIGNED:
case cruft::types::category::SIGNED:
case cruft::types::category::REAL:
case cruft::types::category::BOOL:
// Most numbers are only realistic if they're powers of two; it's not
// a requirement, it's just stupid suspicious.
//
// Though arity of 3 is common for 3D points so we make an exception
// here.
WARN_RETURN (!is_pow2 (val.width), false);
WARN_RETURN (!is_pow2 (val.arity) && val.arity != 3, false);
WARN_RETURN (!is_pow2 (val.alignment), false);
// Restrict bounds of native type constants to values that correspond
// to the hardware we tend to work on.
//
// Again, this is mostly for plausibilty rather than anything else.
// Feel free to make this less restrictive.
WARN_RETURN (val.width == 0 || val.width > 8, false);
WARN_RETURN (val.arity == 0 || val.arity > 16, false);
WARN_RETURN (val.alignment == 0 || val.alignment > 64, false);
// All bets are off. Have fun.
case cruft::types::category::NONE:
return true;
}
WARN ("Unknown value category");
return false;
}