StreamWrapper.h 1.5 KB

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