implement enum and fix double/float byte order

This commit is contained in:
2019-02-07 22:25:49 +01:00
parent f76a5c2fc3
commit fcc66ab9b5

View File

@@ -40,12 +40,12 @@ public:
uint16_t ru16() { return m_swap ? swap(read<uint16_t>()) : read<uint16_t>(); }
uint32_t ru32() { return m_swap ? swap(read<uint32_t>()) : read<uint32_t>(); }
uint64_t ru64() { return m_swap ? swap(read<uint64_t>()) : read<uint64_t>(); }
int8_t ri8() { return read<int8_t>(); }
int16_t ri16() { return m_swap ? swap(read<int16_t>()) : read<int16_t>(); }
int32_t ri32() { return m_swap ? swap(read<int32_t>()) : read<int32_t>(); }
int64_t ri64() { return m_swap ? swap(read<int64_t>()) : read<int64_t>(); }
float rflt() { return read<float>(); }
double rdbl() { return read<double>(); }
int8_t ri8() { return read<int8_t>(); }
int16_t ri16() { return m_swap ? swap(read<int16_t>()) : read<int16_t>(); }
int32_t ri32() { return m_swap ? swap(read<int32_t>()) : read<int32_t>(); }
int64_t ri64() { return m_swap ? swap(read<int64_t>()) : read<int64_t>(); }
float rflt() { return m_swap ? swap(read<float>()) : read<float>(); }
double rdbl() { return m_swap ? swap(read<double>()) : read<double>(); }
std::string pick(size_t chars) { return { (char*)m_cur, chars }; }
std::string rstring()
{
@@ -92,12 +92,22 @@ protected:
template<typename T> T swap(T x)
{
#if _MSC_VER >= 1400
if (sizeof(T) == 2)
return _byteswap_ushort(x);
{
auto y = _byteswap_ushort(*reinterpret_cast<uint16_t*>(&x));
return *reinterpret_cast<T*>(&y);
}
else if (sizeof(T) == 4)
return _byteswap_ulong(x);
{
auto y = _byteswap_ulong(*reinterpret_cast<uint32_t*>(&x));
return *reinterpret_cast<T*>(&y);
}
else if (sizeof(T) == 8)
return _byteswap_uint64(x);
{
auto y = _byteswap_uint64(*reinterpret_cast<uint64_t*>(&x));
return *reinterpret_cast<T*>(&y);
}
#else
auto p = reinterpret_cast<uint16_t*>(&x);
if (sizeof(T) == 2)
@@ -179,7 +189,10 @@ class ABR : public BinaryStream
};
struct Enum : public Type
{
std::string type;
std::string value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("enum {}: {}", type, value); }
};
struct EnumRef : public Type { };
struct Offset : public Type { };