| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 | #include <BinaryStream.h>#include <stdexcept>  // for runtime_errorusing namespace bell;BinaryStream::BinaryStream(std::ostream* ostr) {  this->ostr = ostr;  byteOrder = std::endian::native;}BinaryStream::BinaryStream(std::istream* istr) {  this->istr = istr;  byteOrder = std::endian::native;}void BinaryStream::setByteOrder(std::endian byteOrder) {  this->byteOrder = byteOrder;  flipBytes = byteOrder != std::endian::native;}void BinaryStream::ensureReadable() {  if (istr == nullptr)    throw std::runtime_error("No input provided for binary stream");}void BinaryStream::ensureWritable() {  if (ostr == nullptr)    throw std::runtime_error("No output provided for binary stream");}BinaryStream& BinaryStream::operator>>(int16_t& value) {  ensureReadable();  istr->read((char*)&value, sizeof(value));  if (flipBytes)    value = swap16(value);  return *this;}BinaryStream& BinaryStream::operator>>(uint16_t& value) {  ensureReadable();  istr->read((char*)&value, sizeof(value));  if (flipBytes)    swap16(value);  return *this;}BinaryStream& BinaryStream::operator>>(int32_t& value) {  ensureReadable();  istr->read((char*)&value, sizeof(value));  if (flipBytes)    value = swap32(value);  return *this;}BinaryStream& BinaryStream::operator>>(uint32_t& value) {  ensureReadable();  istr->read((char*)&value, sizeof(value));  if (flipBytes)    value = swap32(value);  return *this;}BinaryStream& BinaryStream::operator>>(int64_t& value) {  ensureReadable();  istr->read((char*)&value, sizeof(value));  if (flipBytes)    value = swap64(value);  return *this;}BinaryStream& BinaryStream::operator>>(uint64_t& value) {  ensureReadable();  istr->read((char*)&value, sizeof(value));  if (flipBytes)    value = swap64(value);  return *this;}BinaryStream& BinaryStream::operator<<(char value) {  ensureWritable();  ostr->write((const char*)&value, sizeof(value));  return *this;}BinaryStream& BinaryStream::operator<<(std::byte value) {  ensureWritable();  ostr->write((const char*)&value, sizeof(value));  return *this;}BinaryStream& BinaryStream::operator<<(int16_t value) {  ensureWritable();  if (flipBytes)    value = swap16(value);  ostr->write((const char*)&value, sizeof(value));  return *this;}BinaryStream& BinaryStream::operator<<(uint16_t value) {  ensureWritable();  if (flipBytes)    value = swap16(value);  ostr->write((const char*)&value, sizeof(value));  return *this;}BinaryStream& BinaryStream::operator<<(int32_t value) {  ensureWritable();  if (flipBytes)    value = swap32(value);  ostr->write((const char*)&value, sizeof(value));  return *this;}BinaryStream& BinaryStream::operator<<(uint32_t value) {  ensureWritable();  if (flipBytes)    value = swap32(value);  ostr->write((const char*)&value, sizeof(value));  return *this;}BinaryStream& BinaryStream::operator<<(int64_t value) {  ensureWritable();  if (flipBytes)    value = swap64(value);  ostr->write((const char*)&value, sizeof(value));  return *this;}BinaryStream& BinaryStream::operator<<(uint64_t value) {  ensureWritable();  if (flipBytes)    value = swap64(value);  ostr->write((const char*)&value, sizeof(value));  return *this;}
 |