I have this function for reading data and cast them to any type I want
template <class T> inline T readBytes() {
T result = {};
if constexpr (std::is_same_v<T, char>)
file.read(&result, sizeof(result));
else
file.read(reinterpret_cast<char*>(&result), sizeof(result));
return result;
}
I can use it like this
uint32_t num = readBytes<uint32_t>();
It works like I expected. But one thing that I want is being able to read data in big-endian mode too, but I'm not sure what is the best option, maybe this one (I'm not sure if it works).
template<class T>
inline T binaryReader::readBytes(bool LE)
{
T result = {};
uint8_t size = sizeof(result);
char* dst = reinterpret_cast<char*>(&result);
char *buf = new char[size];
file.get(buf, size);
for (uint8_t i = 0; i < size; ++i)
dst[i] = buf[size - i - 1];
delete[] buf;
return result;
}
by best option I mean what way can work the best (in both coding style, and performance)