Fix for serializable type names that change base C types

MR !2311 fixed an issue where some base C types were not recognized by
`SerializableTypeString`. However, the fix was such that different types
with the same layout had the same string. For example `char` and
`signed char` both were given the string `I8`. That meant that the
serialization/deseralization would work, but the deserialization could
change the type. That could cause problems if two arrays were expected
to have the same type but did not.

This change undoes much of MR !2311 and redoes it so that the types are
correct.
This commit is contained in:
Kenneth Moreland 2020-11-12 16:38:06 -07:00
parent 88eed2bbeb
commit d82fc92a10
2 changed files with 108 additions and 23 deletions

@ -49,35 +49,88 @@ std::string GetVariadicSerializableTypeString(const T&)
return SerializableTypeString<T>::Get();
}
template <typename T>
std::string IntTypeString()
{
return (std::is_signed<T>::value ? "I" : "U") + std::to_string(sizeof(T) * 8);
}
} // internal
/// @cond SERIALIZATION
#define VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(int_type) \
template <> \
struct SerializableTypeString<int_type> \
{ \
static VTKM_CONT std::string Get() { return internal::IntTypeString<int_type>(); } \
template <>
struct SerializableTypeString<vtkm::Int8>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "I8";
return name;
}
};
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(char);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(signed char);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(unsigned char);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(signed short);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(unsigned short);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(signed int);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(unsigned int);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(signed long);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(unsigned long);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(signed long long);
VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL(unsigned long long);
template <>
struct SerializableTypeString<vtkm::UInt8>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "U8";
return name;
}
};
#undef VTK_M_SERIALIZABLE_INT_TYPE_STRING_IMPL
template <>
struct SerializableTypeString<vtkm::Int16>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "I16";
return name;
}
};
template <>
struct SerializableTypeString<vtkm::UInt16>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "U16";
return name;
}
};
template <>
struct SerializableTypeString<vtkm::Int32>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "I32";
return name;
}
};
template <>
struct SerializableTypeString<vtkm::UInt32>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "U32";
return name;
}
};
template <>
struct SerializableTypeString<vtkm::Int64>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "I64";
return name;
}
};
template <>
struct SerializableTypeString<vtkm::UInt64>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "U64";
return name;
}
};
template <>
struct SerializableTypeString<vtkm::Float32>
@ -99,6 +152,36 @@ struct SerializableTypeString<vtkm::Float64>
}
};
template <>
struct SerializableTypeString<char>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "C8";
return name;
}
};
template <>
struct SerializableTypeString<VTKM_UNUSED_INT_TYPE>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "L" + std::to_string(sizeof(VTKM_UNUSED_INT_TYPE) * 8);
return name;
}
};
template <>
struct SerializableTypeString<unsigned VTKM_UNUSED_INT_TYPE>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "UL" + std::to_string(sizeof(unsigned VTKM_UNUSED_INT_TYPE) * 8);
return name;
}
};
template <typename T, vtkm::IdComponent NumComponents>
struct SerializableTypeString<vtkm::Vec<T, NumComponents>>
{

@ -360,6 +360,8 @@ void TestArrayHandleSerialization()
{
std::cout << "Testing ArrayHandleBasic\n";
vtkm::testing::Testing::TryTypes(TestArrayHandleBasic(), TestTypesList());
vtkm::testing::Testing::TryTypes(
TestArrayHandleBasic(), vtkm::List<char, long, long long, unsigned long, unsigned long long>());
std::cout << "Testing ArrayHandleSOA\n";
vtkm::testing::Testing::TryTypes(TestArrayHandleSOA(), TestTypesList());