StreamWrapper.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Core/StreamIn.h>
  5. #include <Core/StreamOut.h>
  6. #include <ostream>
  7. namespace JPH {
  8. /// Wrapper around std::ostream
  9. class StreamOutWrapper : public StreamOut
  10. {
  11. public:
  12. /// Constructor
  13. StreamOutWrapper(ostream &ioWrapped) : mWrapped(ioWrapped) { }
  14. /// Write a string of bytes to the binary stream
  15. virtual void WriteBytes(const void *inData, size_t inNumBytes) override { mWrapped.write((const char *)inData, inNumBytes); }
  16. /// Returns true if there was an IO failure
  17. virtual bool IsFailed() const override { return mWrapped.fail(); }
  18. private:
  19. ostream & mWrapped;
  20. };
  21. /// Wrapper around std::istream
  22. class StreamInWrapper : public StreamIn
  23. {
  24. public:
  25. /// Constructor
  26. StreamInWrapper(istream &ioWrapped) : mWrapped(ioWrapped) { }
  27. /// Write a string of bytes to the binary stream
  28. virtual void ReadBytes(void *outData, size_t inNumBytes) override { mWrapped.read((char *)outData, inNumBytes); }
  29. /// Returns true when an attempt has been made to read past the end of the file
  30. virtual bool IsEOF() const override { return mWrapped.eof(); }
  31. /// Returns true if there was an IO failure
  32. virtual bool IsFailed() const override { return mWrapped.fail(); }
  33. private:
  34. istream & mWrapped;
  35. };
  36. } // JPH