StreamIn.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/NonCopyable.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Simple binary input stream
  8. class JPH_EXPORT StreamIn : public NonCopyable
  9. {
  10. public:
  11. /// Virtual destructor
  12. virtual ~StreamIn() = default;
  13. /// Read a string of bytes from the binary stream
  14. virtual void ReadBytes(void *outData, size_t inNumBytes) = 0;
  15. /// Returns true when an attempt has been made to read past the end of the file
  16. virtual bool IsEOF() const = 0;
  17. /// Returns true if there was an IO failure
  18. virtual bool IsFailed() const = 0;
  19. /// Read a primitive (e.g. float, int, etc.) from the binary stream
  20. template <class T>
  21. void Read(T &outT)
  22. {
  23. ReadBytes(&outT, sizeof(outT));
  24. }
  25. /// Read a vector of primitives from the binary stream
  26. template <class T, class A>
  27. void Read(std::vector<T, A> &outT)
  28. {
  29. typename Array<T>::size_type len = outT.size(); // Initialize to previous array size, this is used for validation in the StateRecorder class
  30. Read(len);
  31. if (!IsEOF() && !IsFailed())
  32. {
  33. outT.resize(len);
  34. for (typename Array<T>::size_type i = 0; i < len; ++i)
  35. Read(outT[i]);
  36. }
  37. else
  38. outT.clear();
  39. }
  40. /// Read a string from the binary stream (reads the number of characters and then the characters)
  41. template <class Type, class Traits, class Allocator>
  42. void Read(std::basic_string<Type, Traits, Allocator> &outString)
  43. {
  44. typename std::basic_string<Type, Traits, Allocator>::size_type len = 0;
  45. Read(len);
  46. if (!IsEOF() && !IsFailed())
  47. {
  48. outString.resize(len);
  49. ReadBytes(outString.data(), len * sizeof(Type));
  50. }
  51. else
  52. outString.clear();
  53. }
  54. /// Read a vector of primitives from the binary stream using a custom function to read the elements
  55. template <class T, class A, typename F>
  56. void Read(std::vector<T, A> &outT, const F &inReadElement)
  57. {
  58. typename Array<T>::size_type len = outT.size(); // Initialize to previous array size, this is used for validation in the StateRecorder class
  59. Read(len);
  60. if (!IsEOF() && !IsFailed())
  61. {
  62. outT.resize(len);
  63. for (typename Array<T>::size_type i = 0; i < len; ++i)
  64. inReadElement(*this, outT[i]);
  65. }
  66. else
  67. outT.clear();
  68. }
  69. /// Read a Vec3 (don't read W)
  70. void Read(Vec3 &outVec)
  71. {
  72. ReadBytes(&outVec, 3 * sizeof(float));
  73. outVec = Vec3::sFixW(outVec.mValue);
  74. }
  75. /// Read a DVec3 (don't read W)
  76. void Read(DVec3 &outVec)
  77. {
  78. ReadBytes(&outVec, 3 * sizeof(double));
  79. outVec = DVec3::sFixW(outVec.mValue);
  80. }
  81. /// Read a DMat44 (don't read W component of translation)
  82. void Read(DMat44 &outVec)
  83. {
  84. Vec4 x, y, z;
  85. Read(x);
  86. Read(y);
  87. Read(z);
  88. DVec3 t;
  89. Read(t);
  90. outVec = DMat44(x, y, z, t);
  91. }
  92. };
  93. JPH_NAMESPACE_END