pclass: Rename Type::is_byte_based -> Type::is_byte_aligned

Also introduced the Type::cast convenience method. This is primarily to
fix type casting over in kipy when deserializing XML values. This
was necessary since properties in kipy are not type-aware, and as such
couldn't determine if the value was actually a string, or meant to
be casted to something else.
This commit is contained in:
pythonology 2019-07-04 00:15:41 -04:00
parent 81376458ac
commit 6518148b50
5 changed files with 36 additions and 18 deletions

View File

@ -8,18 +8,18 @@ namespace pclass
namespace detail namespace detail
{ {
/** /**
* Provides implementations to PrimitiveType<ValueT>::is_byte_based, * Provides implementations to PrimitiveType<ValueT>::is_byte_aligned,
* PrimitiveType<ValueT>::write_to, and PrimitiveType<ValueT>::read_from. * PrimitiveType<ValueT>::write_to, and PrimitiveType<ValueT>::read_from.
*/ */
template <typename ValueT, typename Enable = void> template <typename ValueT, typename Enable = void>
struct primitive_type_helper struct primitive_type_helper
{ {
static bool is_byte_based() static bool is_byte_aligned()
{ {
// Provide a compiler error if this is not specialized // Provide a compiler error if this is not specialized
static_assert( static_assert(
sizeof(ValueT) == 0, sizeof(ValueT) == 0,
"Missing specialization of primitive_type_helper::is_byte_based" "Missing specialization of primitive_type_helper::is_byte_aligned"
); );
} }
@ -51,7 +51,7 @@ namespace pclass
typename std::enable_if<std::is_integral<ValueT>::value>::type typename std::enable_if<std::is_integral<ValueT>::value>::type
> >
{ {
static bool is_byte_based() static bool is_byte_aligned()
{ {
return true; return true;
} }
@ -79,7 +79,7 @@ namespace pclass
using type = ki::BitInteger<N, Unsigned>; using type = ki::BitInteger<N, Unsigned>;
public: public:
static bool is_byte_based() static bool is_byte_aligned()
{ {
return false; return false;
} }
@ -107,7 +107,7 @@ namespace pclass
using underlying_type = ki::BitInteger<1, true>; using underlying_type = ki::BitInteger<1, true>;
public: public:
static bool is_byte_based() static bool is_byte_aligned()
{ {
return false; return false;
} }
@ -142,7 +142,7 @@ namespace pclass
using uint_type = typename bits<bitsizeof<ValueT>::value>::uint_type; using uint_type = typename bits<bitsizeof<ValueT>::value>::uint_type;
public: public:
static bool is_byte_based() static bool is_byte_aligned()
{ {
return true; return true;
} }
@ -177,7 +177,7 @@ namespace pclass
using type = std::basic_string<_Elem, _Traits, _Alloc>; using type = std::basic_string<_Elem, _Traits, _Alloc>;
public: public:
static bool is_byte_based() static bool is_byte_aligned()
{ {
return true; return true;
} }
@ -250,9 +250,14 @@ namespace pclass
} }
~PrimitiveType() = default; ~PrimitiveType() = default;
bool is_byte_based() const override bool is_byte_aligned() const override
{ {
return detail::primitive_type_helper<ValueT>::is_byte_based(); return detail::primitive_type_helper<ValueT>::is_byte_aligned();
}
Value cast(Value &value) const override
{
return value.as<ValueT>();
} }
void write_to(BitStream &stream, const bool is_file, Value &value) const override void write_to(BitStream &stream, const bool is_file, Value &value) const override

View File

@ -56,15 +56,21 @@ namespace pclass
Kind get_kind() const; Kind get_kind() const;
/** /**
* @returns Whether or not this type works in bytes, rather than bits. * @returns Whether or not this type should be byte aligned.
*/ */
virtual bool is_byte_based() const; virtual bool is_byte_aligned() const;
/** /**
* The TypeSystem used to define this Type instance. * The TypeSystem used to define this Type instance.
*/ */
const TypeSystem &get_type_system() const; const TypeSystem &get_type_system() const;
/**
* Casts the provided value to this type.
* @returns A new Value instance that has been casted to this type.
*/
virtual Value cast(Value &value) const;
/** /**
* Create an instance of the type being represented. * Create an instance of the type being represented.
* @returns A pointer to a new PropertyClass instance. * @returns A pointer to a new PropertyClass instance.

View File

@ -38,11 +38,18 @@ namespace pclass
return m_type_system; return m_type_system;
} }
bool Type::is_byte_based() const bool Type::is_byte_aligned() const
{ {
return true; return true;
} }
Value Type::cast(Value &value) const
{
std::ostringstream oss;
oss << "Type '" << m_name << "' does not implement Type::cast.";
throw runtime_error(oss.str());
}
void Type::write_to(BitStream &stream, const bool is_file, Value &value) const void Type::write_to(BitStream &stream, const bool is_file, Value &value) const
{ {
std::ostringstream oss; std::ostringstream oss;

View File

@ -201,8 +201,8 @@ namespace serialization
for (auto i = 0; i < prop.get_element_count(); ++i) for (auto i = 0; i < prop.get_element_count(); ++i)
{ {
// Realign the stream if this property works in bytes // Realign the stream if necessary
if (prop.get_type().is_byte_based()) if (prop.get_type().is_byte_aligned())
stream.realign(); stream.realign();
if (prop.is_pointer() if (prop.is_pointer()
@ -407,8 +407,8 @@ namespace serialization
for (auto i = 0; i < prop.get_element_count(); ++i) for (auto i = 0; i < prop.get_element_count(); ++i)
{ {
// Realign the stream if this property works in bytes // Realign the stream if necessary
if (prop.get_type().is_byte_based()) if (prop.get_type().is_byte_aligned())
stream.realign(); stream.realign();
if (prop.is_pointer() && if (prop.is_pointer() &&

View File

@ -93,7 +93,7 @@ namespace detail
template <> template <>
struct primitive_type_helper<Vector3D> struct primitive_type_helper<Vector3D>
{ {
static bool is_byte_based() static bool is_byte_aligned()
{ {
return true; return true;
} }