StreamIn.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// Simple binary input stream
  6. class StreamIn
  7. {
  8. public:
  9. /// Virtual destructor
  10. virtual ~StreamIn() = default;
  11. /// Read a string of bytes from the binary stream
  12. virtual void ReadBytes(void *outData, size_t inNumBytes) = 0;
  13. /// Returns true when an attempt has been made to read past the end of the file
  14. virtual bool IsEOF() const = 0;
  15. /// Returns true if there was an IO failure
  16. virtual bool IsFailed() const = 0;
  17. /// Read a primitive (e.g. float, int, etc.) from the binary stream
  18. template <class T>
  19. void Read(T &outT)
  20. {
  21. ReadBytes(&outT, sizeof(outT));
  22. }
  23. /// Read a vector of primitives from the binary stream
  24. template <class T, class A>
  25. void Read(std::vector<T, A> &outT)
  26. {
  27. typename Array<T>::size_type len = outT.size(); // Initialize to previous array size, this is used for validation in the StateRecorder class
  28. Read(len);
  29. if (!IsEOF() && !IsFailed())
  30. {
  31. outT.resize(len);
  32. for (typename Array<T>::size_type i = 0; i < len; ++i)
  33. Read(outT[i]);
  34. }
  35. else
  36. outT.clear();
  37. }
  38. /// Read a string from the binary stream (reads the number of characters and then the characters)
  39. template <class Type, class Traits, class Allocator>
  40. void Read(std::basic_string<Type, Traits, Allocator> &outString)
  41. {
  42. typename std::basic_string<Type, Traits, Allocator>::size_type len = 0;
  43. Read(len);
  44. if (!IsEOF() && !IsFailed())
  45. {
  46. outString.resize(len);
  47. ReadBytes(outString.data(), len * sizeof(Type));
  48. }
  49. else
  50. outString.clear();
  51. }
  52. /// Read a Vec3 (don't read W)
  53. void Read(Vec3 &outVec)
  54. {
  55. ReadBytes(&outVec, 3 * sizeof(float));
  56. outVec = Vec3::sFixW(outVec.mValue);
  57. }
  58. };
  59. JPH_NAMESPACE_END