StreamOut.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 output stream
  8. class JPH_EXPORT StreamOut : public NonCopyable
  9. {
  10. public:
  11. /// Virtual destructor
  12. virtual ~StreamOut() = default;
  13. /// Write a string of bytes to the binary stream
  14. virtual void WriteBytes(const void *inData, size_t inNumBytes) = 0;
  15. /// Returns true if there was an IO failure
  16. virtual bool IsFailed() const = 0;
  17. /// Write a primitive (e.g. float, int, etc.) to the binary stream
  18. template <class T, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true>
  19. void Write(const T &inT)
  20. {
  21. WriteBytes(&inT, sizeof(inT));
  22. }
  23. /// Write a vector of primitives to the binary stream
  24. template <class T, class A, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true>
  25. void Write(const Array<T, A> &inT)
  26. {
  27. uint32 len = uint32(inT.size());
  28. Write(len);
  29. if (!IsFailed())
  30. {
  31. if constexpr (std::is_same_v<T, Vec3> || std::is_same_v<T, DVec3> || std::is_same_v<T, DMat44>)
  32. {
  33. // These types have unused components that we don't want to write
  34. for (typename Array<T, A>::size_type i = 0; i < len; ++i)
  35. Write(inT[i]);
  36. }
  37. else
  38. {
  39. // Write all elements at once
  40. WriteBytes(inT.data(), len * sizeof(T));
  41. }
  42. }
  43. }
  44. /// Write a string to the binary stream (writes the number of characters and then the characters)
  45. template <class Type, class Traits, class Allocator>
  46. void Write(const std::basic_string<Type, Traits, Allocator> &inString)
  47. {
  48. uint32 len = uint32(inString.size());
  49. Write(len);
  50. if (!IsFailed())
  51. WriteBytes(inString.data(), len * sizeof(Type));
  52. }
  53. /// Write a vector of primitives to the binary stream using a custom write function
  54. template <class T, class A, typename F>
  55. void Write(const Array<T, A> &inT, const F &inWriteElement)
  56. {
  57. uint32 len = uint32(inT.size());
  58. Write(len);
  59. if (!IsFailed())
  60. for (typename Array<T, A>::size_type i = 0; i < len; ++i)
  61. inWriteElement(inT[i], *this);
  62. }
  63. /// Write a Vec3 (don't write W)
  64. void Write(const Vec3 &inVec)
  65. {
  66. WriteBytes(&inVec, 3 * sizeof(float));
  67. }
  68. /// Write a DVec3 (don't write W)
  69. void Write(const DVec3 &inVec)
  70. {
  71. WriteBytes(&inVec, 3 * sizeof(double));
  72. }
  73. /// Write a DMat44 (don't write W component of translation)
  74. void Write(const DMat44 &inVec)
  75. {
  76. Write(inVec.GetColumn4(0));
  77. Write(inVec.GetColumn4(1));
  78. Write(inVec.GetColumn4(2));
  79. Write(inVec.GetTranslation());
  80. }
  81. };
  82. JPH_NAMESPACE_END