BinaryStream.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <BinaryStream.h>
  2. #include <stdexcept> // for runtime_error
  3. using namespace bell;
  4. BinaryStream::BinaryStream(std::ostream* ostr) {
  5. this->ostr = ostr;
  6. byteOrder = std::endian::native;
  7. }
  8. BinaryStream::BinaryStream(std::istream* istr) {
  9. this->istr = istr;
  10. byteOrder = std::endian::native;
  11. }
  12. void BinaryStream::setByteOrder(std::endian byteOrder) {
  13. this->byteOrder = byteOrder;
  14. flipBytes = byteOrder != std::endian::native;
  15. }
  16. void BinaryStream::ensureReadable() {
  17. if (istr == nullptr)
  18. throw std::runtime_error("No input provided for binary stream");
  19. }
  20. void BinaryStream::ensureWritable() {
  21. if (ostr == nullptr)
  22. throw std::runtime_error("No output provided for binary stream");
  23. }
  24. BinaryStream& BinaryStream::operator>>(int16_t& value) {
  25. ensureReadable();
  26. istr->read((char*)&value, sizeof(value));
  27. if (flipBytes)
  28. value = swap16(value);
  29. return *this;
  30. }
  31. BinaryStream& BinaryStream::operator>>(uint16_t& value) {
  32. ensureReadable();
  33. istr->read((char*)&value, sizeof(value));
  34. if (flipBytes)
  35. swap16(value);
  36. return *this;
  37. }
  38. BinaryStream& BinaryStream::operator>>(int32_t& value) {
  39. ensureReadable();
  40. istr->read((char*)&value, sizeof(value));
  41. if (flipBytes)
  42. value = swap32(value);
  43. return *this;
  44. }
  45. BinaryStream& BinaryStream::operator>>(uint32_t& value) {
  46. ensureReadable();
  47. istr->read((char*)&value, sizeof(value));
  48. if (flipBytes)
  49. value = swap32(value);
  50. return *this;
  51. }
  52. BinaryStream& BinaryStream::operator>>(int64_t& value) {
  53. ensureReadable();
  54. istr->read((char*)&value, sizeof(value));
  55. if (flipBytes)
  56. value = swap64(value);
  57. return *this;
  58. }
  59. BinaryStream& BinaryStream::operator>>(uint64_t& value) {
  60. ensureReadable();
  61. istr->read((char*)&value, sizeof(value));
  62. if (flipBytes)
  63. value = swap64(value);
  64. return *this;
  65. }
  66. BinaryStream& BinaryStream::operator<<(char value) {
  67. ensureWritable();
  68. ostr->write((const char*)&value, sizeof(value));
  69. return *this;
  70. }
  71. BinaryStream& BinaryStream::operator<<(std::byte value) {
  72. ensureWritable();
  73. ostr->write((const char*)&value, sizeof(value));
  74. return *this;
  75. }
  76. BinaryStream& BinaryStream::operator<<(int16_t value) {
  77. ensureWritable();
  78. if (flipBytes)
  79. value = swap16(value);
  80. ostr->write((const char*)&value, sizeof(value));
  81. return *this;
  82. }
  83. BinaryStream& BinaryStream::operator<<(uint16_t value) {
  84. ensureWritable();
  85. if (flipBytes)
  86. value = swap16(value);
  87. ostr->write((const char*)&value, sizeof(value));
  88. return *this;
  89. }
  90. BinaryStream& BinaryStream::operator<<(int32_t value) {
  91. ensureWritable();
  92. if (flipBytes)
  93. value = swap32(value);
  94. ostr->write((const char*)&value, sizeof(value));
  95. return *this;
  96. }
  97. BinaryStream& BinaryStream::operator<<(uint32_t value) {
  98. ensureWritable();
  99. if (flipBytes)
  100. value = swap32(value);
  101. ostr->write((const char*)&value, sizeof(value));
  102. return *this;
  103. }
  104. BinaryStream& BinaryStream::operator<<(int64_t value) {
  105. ensureWritable();
  106. if (flipBytes)
  107. value = swap64(value);
  108. ostr->write((const char*)&value, sizeof(value));
  109. return *this;
  110. }
  111. BinaryStream& BinaryStream::operator<<(uint64_t value) {
  112. ensureWritable();
  113. if (flipBytes)
  114. value = swap64(value);
  115. ostr->write((const char*)&value, sizeof(value));
  116. return *this;
  117. }