StreamWrapper.h 1.4 KB

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