Clean up error handling in NIFStream reading

No particular reason to introduce is_arithmetic specifically for 16-bit floats, there's no significant code duplication
Bypassing getBoolean() could be error-prone for records that are shared between pre-4.2 and post-4.2 formats
macos_ci_fix
Alexei Kotov 1 year ago
parent c9f03b4174
commit aed440ce64

@ -26,20 +26,16 @@ namespace Nif
class Reader;
template <class T>
struct is_arithmetic
: std::integral_constant<bool, std::is_arithmetic_v<T> || std::is_same<T, Misc::float16_t>::value>
{
};
template <std::size_t numInstances, typename T>
inline void readLittleEndianBufferOfType(Files::IStreamPtr& pIStream, T* dest)
{
static_assert(is_arithmetic<T>(), "Buffer element type is not arithmetic");
static_assert(
std::is_arithmetic_v<T> || std::is_same_v<T, Misc::float16_t>, "Buffer element type is not arithmetic");
static_assert(!std::is_same_v<T, bool>, "Buffer element type is boolean");
pIStream->read((char*)dest, numInstances * sizeof(T));
if (pIStream->bad())
throw std::runtime_error("Failed to read little endian typed (" + std::string(typeid(T).name())
+ ") buffer of " + std::to_string(numInstances) + " instances");
throw std::runtime_error("Failed to read typed (" + std::string(typeid(T).name()) + ") buffer of "
+ std::to_string(numInstances) + " instances");
if constexpr (Misc::IS_BIG_ENDIAN)
for (std::size_t i = 0; i < numInstances; i++)
Misc::swapEndiannessInplace(dest[i]);
@ -48,11 +44,13 @@ namespace Nif
template <typename T>
inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr& pIStream, T* dest, std::size_t numInstances)
{
static_assert(is_arithmetic<T>(), "Buffer element type is not arithmetic");
static_assert(
std::is_arithmetic_v<T> || std::is_same_v<T, Misc::float16_t>, "Buffer element type is not arithmetic");
static_assert(!std::is_same_v<T, bool>, "Buffer element type is boolean");
pIStream->read((char*)dest, numInstances * sizeof(T));
if (pIStream->bad())
throw std::runtime_error(
"Failed to read little endian dynamic buffer of " + std::to_string(numInstances) + " instances");
throw std::runtime_error("Failed to read typed (" + std::string(typeid(T).name()) + ") dynamic buffer of "
+ std::to_string(numInstances) + " instances");
if constexpr (Misc::IS_BIG_ENDIAN)
for (std::size_t i = 0; i < numInstances; i++)
Misc::swapEndiannessInplace(dest[i]);

Loading…
Cancel
Save