StreamOut.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. /// Simple binary output stream
  7. class StreamOut
  8. {
  9. public:
  10. /// Virtual destructor
  11. virtual ~StreamOut() = default;
  12. /// Write a string of bytes to the binary stream
  13. virtual void WriteBytes(const void *inData, size_t inNumBytes) = 0;
  14. /// Returns true if there was an IO failure
  15. virtual bool IsFailed() const = 0;
  16. /// Write a primitive (e.g. float, int, etc.) to the binary stream
  17. template <class T>
  18. void Write(const T &inT)
  19. {
  20. WriteBytes(&inT, sizeof(inT));
  21. }
  22. /// Write a vector of primitives from the binary stream
  23. template <class T, class A>
  24. void Write(const std::vector<T, A> &inT)
  25. {
  26. typename Array<T>::size_type len = inT.size();
  27. Write(len);
  28. if (!IsFailed())
  29. for (typename Array<T>::size_type i = 0; i < len; ++i)
  30. Write(inT[i]);
  31. }
  32. /// Write a string to the binary stream (writes the number of characters and then the characters)
  33. template <class Type, class Traits, class Allocator>
  34. void Write(const std::basic_string<Type, Traits, Allocator> &inString)
  35. {
  36. typename std::basic_string<Type, Traits, Allocator>::size_type len = inString.size();
  37. Write(len);
  38. if (!IsFailed())
  39. WriteBytes(inString.data(), len * sizeof(Type));
  40. }
  41. /// Write a Vec3 (don't write W)
  42. void Write(const Vec3 &inVec)
  43. {
  44. WriteBytes(&inVec, 3 * sizeof(float));
  45. }
  46. /// Write a DVec3 (don't write W)
  47. void Write(const DVec3 &inVec)
  48. {
  49. WriteBytes(&inVec, 3 * sizeof(double));
  50. }
  51. /// Write a DMat44 (don't write W component of translation)
  52. void Write(const DMat44 &inVec)
  53. {
  54. Write(inVec.GetColumn4(0));
  55. Write(inVec.GetColumn4(1));
  56. Write(inVec.GetColumn4(2));
  57. Write(inVec.GetTranslation());
  58. }
  59. };
  60. JPH_NAMESPACE_END